Back

Creating your first class

Introduction
There are thousands of excellent tutorials and videos on the Internet and especially YouTube on OO programming using Python 3. We are not going to reproduce those courses, although we certainly recommend looking at a few of them and working your way through the examples that they use. What we are going to do here, however, is to go through a clear 'Getting started' set of lessons on using OO to write a Dungeons and Dragons type game. We will give you all the tools you need and clear examples for you to use so that you can get started on this type of game. We are going to do this because it is a very suitable type of game for a GCSE or A Level coursework project.

The Kingdom of Rhool
Our game is going to be based in the ancient Kingdom of Rhool. The Kingdom of Rhool has mountains to the North, The Plains to the East, the Forbidden Marshes to the South and The Great Forest to the West. Scattered across the land are various peoples in villages. There have been many strange reports recently of monsters roaming the land, cattle and sheep being killed and left half-eaten, violent storms lasting days and travellers disappearing from normally well-established and safe routes.

The monster class
Our game needs some monsters. We will start by creating a template for a monster, called a class. Our class isn't an actual, real monster, it's just the stamper that we will use to stamp out a new monster everytime we need a new one. 

class1

Creating a class
To create a class, we use the keyword class followed by the name for the class and a colon. On line 4, we use the triple quotes method to add a brief comment. On line 6, we come across the first method in the class. Most classes have this special method because it is always run when we use the class to create an object, in our class, a new monster. This particular method is called def __init__ and it is then followed by the attributes we are going to give our monster. The first attribute in any method, however, is a special extra one and should always be called self. This simply informs this method that it has to be applied to the current object.

Instantiation
Creating a new object from a class is called instantiation or instantiating an object. When we use this class to create an instance of a monster, we are going to have to pass the class a name for the monster and some ratings for their strength. wisdom and magic capabilities. When we do that, they need to be stored in some variables inside each monster object. That is what lines 7 - 10 are doing, and note once again, the use of self. Don't miss out self or you will have problems!

That's it! We now have a monster class that we can use to create monsters. To create an object we give it a name, such as monster1, monster2 etc. We then call the class we want to use and pass it the parameters we want it to have. So on line 13, we create a monster called monster1, call the class called monster, and pass it the real name of the monster (Gregor) and the values for his strength, wisdom and magic. We have also added a second monster called monster2.

Dot notation
We can access any of the individual details (called attributes) we like from any of our monsters using the dot notation. This simply means state the object's name, followed by a dot and then followed by the attribute. Lines 17 - 20 print out various attibutes from both monsters.

Printing out all the details of a monster
When you run lines 23 and 24, you will get something similar (but different) to this: 

<__main__.monster object at 0x00000000035B04A8>
<__main__.monster object at 0x00000000035FB208>

This tells us where each object is being stored in memory. It doesn't allow us to print out all of the attributes of an object. Will look at that in the next section.

Try these tasks to see if you have understood everything. There are no answers - ask friends if you struggle with any of them.
1) Create an extra monster called Skap, with strength = 7, wisdom = 2 and magic = 5.
2) Print off the extra monster's name and their wosdom rating.
3) Create another monster but make up the details yourself and print out some of them.
4) Modify your program to include an extra attribute called 'Intelligence'. You will have to modify the class, and then modify the places where you instantiated the monsters, and print out some of the monsters' intelligence ratings.
5) Create an entirely new class called Person. They should have a first name, a second name, an age and an occupation.
6) When you have created this new class, create three people.
7) Print off some the details from each of the people.
8) Explain these terms: class, object, attribute, instantiate, dot notation, triple quotes.

Back