Skip main navigation

New offer! Get 30% off your first 2 months of Unlimited Monthly. Start your subscription for just £29.99 £19.99. New subscribers only. T&Cs apply

Find out more

Packaging your code

Packaging your code
0.5
OK, so to package up my code, first of all, I’m going to need to copy and paste all of the different class files– so that’s room, character, and item– into Python files on the computer because I can’t do this from within Trinket. So I’m just going to go ahead and I’m going to make a new Python file. I’m going to put it in a folder called RPG. So all of these are going to go into the same folder. And I’m going to give it the same name as it had before. So this one is room . py. And I’m going to do exactly the same for character and for item.
34.2
Obviously, if you’ve been writing your programme already using an editor on your computer, you won’t need to do this because you will already have them. Just make sure that the files are together and they are in a folder called RPG. You don’t need to put the main file in that folder. We’re going to make that in a minute.
54
OK, so now that’s done. I’m going to make another new file, and I’m going to save this file also in that RPG folder, which we made earlier. But I’m going to call it init . py. And I’m going to put double underscore before and after the word init, just like we did when we were writing the constructor.
76.2
So there you can see I’ve got my init file and my class files. So inside the init file, I’m going to import all of my class files. So like this. I’m going to put from . and then the name of the file name, and then import the classes which are in that file. If there is more than one class in a file, such as character and enemy, that’s fine. You can just put a comma in between them. And I’ll just save that file there.
106.8
Now, I’m going to make another new file. This time, it is going to be my main file. And I’m going to save it not in the RPG folder, but the folder immediately above it. So here’s main . py. Now, in this file, be able to use my RPG classes, I just type in import RPG where RPG is the name of the folder, which I used. If you used a different folder name, than you would put something else there. But don’t use any spaces. Then I can use the classes just like I would before. But I just put RPG . in front of it. So here I’m using the character class. But I’m saying RPG . character.
142.7
Don’t forget the capital because it’s a class.
150.1
So then I can run my code. And when I have a look, tada, I haven’t had to write anything much in this main . py file. But I managed to use the class.

Congratulations, you have now created your own adventure game in object-oriented style!

One of the benefits of writing your code in this programming style is that you can share the reusable classes you have written so that other people can use them to make their own game. If you are an educator, you might want to share them with your students, allowing them to create their own games. Alternatively, you might simply want to share your code with the world!

You have probably already used Python packages by adding an import statement at the top of your script, like this:

import random

You may be surprised to learn that you can make your classes into a package like that simply by adding a single extra file. This is only possible if you are using Python on your computer, rather than on the Trinket emulator.

First, put all of your class files into one folder. Do not include your main.py file – remember we are packaging just the reusable classes and not your finished game. Make sure the folder’s name has no spaces or capital letters – we called ours rpg. In the same folder, create a new file and save it as __init__.py, with double underscores on each side of ‘init’.

Inside this file, you need to import all of your classes in the following format:

from .filename import Classname

For example, here is the __init__.py file from my game:

from .character import Character, Enemy
from .item import Item
from .room import Room

As you can see, you can import multiple classes from the same file by separating the class names with a comma.

Navigate to the folder that contains your rpg folder and create a new Python file called main.py. Inside this file, you can import your package in this format:

import rpg

You can then refer to your classes with dot notation, so the Room class would be rpg.Room, and the Character class would be rpg.Character. Doing so lets Python know from which package the class definition is coming. You do not need to specify the package when calling methods on an object, as Python learns the class definition when you create the object.

For example, here is my main.py file:

import rpg

marc = rpg.Character("Marc", "A mystical wizard with magic powers")

marc.describe()

If other people want to to use your code, they simply need to have a folder containing your rpg package, and then they can create their own Python file and import your hard work to use in their game. If you are an educator, you might also want to package up certain bits of code that your learners can use in situations in which it is not important for them to know how the code works.

This article is from the free online

Object-oriented Programming in Python: Create Your Own Adventure Game

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now