Program Arcade Games
With Python And Pygame

Language
English
Dutch - Nederlands
French
Hungarian - Magyar
Portuguese - Português
Russian - Русский
Spanish - Español
Turkish - Türkçe

Chapter 13: Introduction to Classes

Package up related variables, and give them life!

13.1 Why Learn About Classes?

Video: Why Classes?

Each character in an adventure game needs data: a name, location, strength, are they raising their arm, what direction they are headed, etc. Plus those characters do things. They run, jump, hit, and talk.

Our Python code to store this data might look like:

name = "Link"
sex = "Male"
max_hit_points = 50
current_hit_points = 50

In order to do anything with this character, we'll need to pass that data to a function:

def display_character(name, sex, max_hit_points, current_hit_points):
    print(name, sex, max_hit_points, current_hit_points)

Now imagine creating a program that has a set of variables like that for each character, monster, and item in our game. Then we need to create functions that work with those items. We've now waded into a quagmire of data. All of a sudden this doesn't sound like fun at all.

But wait, it gets worse! As our game expands, we may need to add new fields to describe our character. In this case we've added max_speed:

name = "Link"
sex = "Male"
max_hit_points = 50
current_hit_points = 50
max_speed = 10

def display_character(name, sex, max_hit_points, current_hit_points, max_speed):
    print(name, sex, max_hit_points, current_hit_points)

In example above, there is only one function. But in a large video game, we might have hundreds of functions that deal with the main character. Adding a new field to help describe what character has and can do would require us to go through each one of those functions and add it to the parameter list. That would be a lot of work. And perhaps we need to add max_speed to different types of characters like monsters. There needs to be a better way. Somehow our program needs to package up those data fields so they can be managed easily.

13.2 Defining and Creating Simple Classes

Video: Defining Simple Classes

A better way to manage multiple data attributes is to define a structure that has all of the information. Then we can give that “grouping” of information a name, like Character or Address. This can be easily done in Python and any other modern language by using a class.

For example, we can define a class representing a character in a game:

class Character():
    name = "Link"
    sex = "Male"
    max_hit_points = 50
    current_hit_points = 50
    max_speed = 10
    armor_amount = 8

Here's another example, we define a class to hold all the fields for an address:

class Address():
    name = ""
    line1 = ""
    line2 = ""
    city = ""
    state = ""
    zip = ""

In the code above, Address is the class name. The variables in the class, such as name and city, are called attributes or fields. (Note the similarities and differences between declaring a class and declaring a function.)

Unlike functions and variables, class names should begin with an upper case letter. While it is possible to begin a class with a lower case letter, it is not considered good practice.

To better visualize classes and how they relate, programmers often make diagrams. A diagram for the Address class would look like Figure 13.1. See how the class name is on top with the name of each attribute listed below. To the right of each attribute is the data type, such as string or integer.

fig.address_example_3
Figure 13.1: Class Diagram

The class code defines a class but it does not actually create an instance of one. The code told the computer what fields an address has and what the initial default values will be. We don't actually have an address yet though. We can define a class without creating one just like we can define a function without calling it. To create a class and set the fields, look at the example below:

# Create an address
homeAddress = Address()

# Set the fields in the address
homeAddress.name = "John Smith"
homeAddress.line1 = "701 N. C Street"
homeAddress.line2 = "Carver Science Building"
homeAddress.city = "Indianola"
homeAddress.state = "IA"
homeAddress.zip = "50125"

An instance of the address class is created in line 2. Note how the class Address name is used, followed by parentheses. The variable name can be anything that follows normal naming rules.

To set the fields in the class, a program must use the dot operator. This operator is the period that is between the homeAddress and the field name. See how lines 5-10 use the dot operator to set each field value.

A very common mistake when working with classes is to forget to specify which instance of the class you want to work with. If only one address is created, it is natural to assume the computer will know to use that address you are talking about. This is not the case however. See the example below:

class Address():
    name = ""
    line1 = ""
    line2 = ""
    city = ""
    state = ""
    zip = ""

# Create an address
my_address = Address()

# Alert! This does not set the address's name!
name = "Dr. Craven"

# This doesn't set the name for the address either
Address.name = "Dr. Craven"

# This does work:
my_address.name = "Dr. Craven"

A second address can be created and fields from both instances may be used. See the example below:

class Address():
    name = ""
    line1 = ""
    line2 = ""
    city = ""
    state = ""
    zip = ""

# Create an address
homeAddress = Address()

# Set the fields in the address
homeAddress.name = "John Smith"
homeAddress.line1 = "701 N. C Street"
homeAddress.line2 = "Carver Science Building"
homeAddress.city = "Indianola"
homeAddress.state = "IA"
homeAddress.zip = "50125"

# Create another address
vacationHomeAddress = Address()

# Set the fields in the address
vacationHomeAddress.name = "John Smith"
vacationHomeAddress.line1 = "1122 Main Street"
vacationHomeAddress.line2 = ""
vacationHomeAddress.city = "Panama City Beach"
vacationHomeAddress.state = "FL"
vacationHomeAddress.zip = "32407"

print("The client's main home is in " + homeAddress.city)
print("His vacation home is in " + vacationHomeAddress.city)

Line 10 creates the first instance of Address; line 21 creates the second instance. The variable homeAddress points to the first instance and vacationHomeAddress points to the second.

Lines 24-29 set the fields in this new class instance. Line 31 prints the city for the home address, because homeAddress appears before the dot operator. Line 32 prints the vacation address because vacationHomeAddress appears before the dot operator.

In the example Address is called the class because it defines a new classification for a data object. The variables homeAddress and vacationHomeAddress refer to objects because they refer to actual instances of the class Address. A simple definition of an object is that it is an instance of a class. Like “Bob” and “Nancy” are instances of a Human class.

By using www.pythontutor.com we can visualize the execution of the code (see below). There are three variables in play. One points to the class definition of Address. The other two variables point to the different address objects and their data.

fig.two_addresses
Figure 13.2: Two Addresses

Putting lots of data fields into a class makes it easy to pass data in and out of a function. In the code below, the function takes in an address as a parameter and prints it out on the screen. It is not necessary to pass parameters for each field of the address.

# Print an address to the screen
def printAddress(address):
    print(address.name)
    # If there is a line1 in the address, print it
    if( len(address.line1) > 0 ):
        print(address.line1)
    # If there is a line2 in the address, print it
    if( len(address.line2) > 0 ):
        print( address.line2 )
    print( address.city+", "+address.state+" "+address.zip )

printAddress( homeAddress )
print()
printAddress( vacationHomeAddress )

13.3 Adding Methods to Classes

Video: Methods

In addition to attributes, classes may have methods. A method is a function that exists inside of a class. Expanding the earlier example of a Dog class from the review problem 1 above, the code below adds a method for a dog barking.

class Dog():
    age = 0
    name = ""
    weight = 0

    def bark(self):
        print("Woof")

The method definition is contained in lines 6-7 above. Method definitions in a class look almost exactly like function definitions. The big difference is the addition of a parameter self on line 6. The first parameter of any method in a class must be self. This parameter is required even if the function does not use it.

Here are the important items to keep in mind when creating methods for classes:

Methods may be called in a manner similar to referencing attributes from an object. See the example code below.

myDog = Dog()

myDog.name = "Spot"
myDog.weight = 20
myDog.age = 3

myDog.bark()

Line 1 creates the dog. Lines 3-5 set the attributes of the object. Line 7 calls the bark function. Note that even through the bark function has one parameter, self, the call does not pass in anything. This is because the first parameter is assumed to be a reference to the dog object itself. Behind the scenes, Python makes a call that looks like:

# Example, not actually legal
Dog.bark(myDog)

If the bark function needs to make reference to any of the attributes, then it does so using the self reference variable. For example, we can change the Dog class so that when the dog barks, it also prints out the dog's name. In the code below, the name attribute is accessed using a dot operator and the self reference.

    def bark(self):
        print( "Woof says", self.name )

Attributes are adjectives, and methods are verbs. The drawing for the class would look like Figure 13.3.

fig.dog_2_1
Figure 13.3: Dog Class

13.3.1 Example: Ball Class

This example code could be used in Python/Pygame to draw a ball. Having all the parameters contained in a class makes data management easier. The diagram for the Ball class is shown in Figure 13.4.

fig.ball_2_1
Figure 13.4: Ball Class
class Ball():
    # --- Class Attributes ---
    # Ball position
    x = 0
    y = 0

    # Ball's vector
    change_x = 0
    change_y = 0

    # Ball size
    size = 10

    # Ball color
    color = [255,255,255]

	# --- Class Methods ---
    def move(self):
        self.x += self.change_x
        self.y += self.change_y

    def draw(self, screen):
        pygame.draw.circle(screen, self.color, [self.x, self.y], self.size )

Below is the code that would go ahead of the main program loop to create a ball and set its attributes:

theBall = Ball()
theBall.x = 100
theBall.y = 100
theBall.change_x = 2
theBall.change_y = 1
theBall.color = [255,0,0]

This code would go inside the main loop to move and draw the ball:

theBall.move()
theBall.draw(screen)

13.4 References

Video: References

Here's where we separate the true programmers from the want-to-be's. Understanding class references. Take a look at the following code:

class Person:
    name = ""
    money = 0

bob = Person()
bob.name = "Bob"
bob.money = 100

nancy = Person()
nancy.name = "Nancy"

print(bob.name, "has", bob.money, "dollars.")
print(nancy.name, "has", nancy.money, "dollars.")

The code above creates two instances of the Person() class, and using www.pythontutor.com we can visualize the two classes in Figure 13.5.

fig.two_persons
Figure 13.5: Two Persons

The code above has nothing new. But the code below does:

class Person:
    name = ""
    money = 0

bob = Person()
bob.name = "Bob"
bob.money = 100

nancy = bob
nancy.name = "Nancy"

print(bob.name, "has", bob.money, "dollars.")
print(nancy.name, "has", nancy.money, "dollars.")

See the difference on line 9?

A common misconception when working with objects is to assume that the variable bob is the Person object. This is not the case. The variable bob is a reference to the Person object. That is, it stores the memory address of where the object is, and not the object itself.

If bob actually was the object, then line 9 could create a copy of the object and there would be two objects in existence. The output of the program would show both Bob and Nancy having 100 dollars. But when run, the program outputs the following instead:

Nancy has 100 dollars.
Nancy has 100 dollars.

What bob stores is a reference to the object. Besides reference, one may call this address, pointer, or handle. A reference is an address in computer memory for where the object is stored. This address is a hexidecimal number which, if printed out, might look something like 0x1e504. When line 9 is run, the address is copied rather than the entire object the address points to. See Figure 13.6.

fig.ref_example1
Figure 13.6: Class References

We can also run this in www.pythontutor.com to see how both of the variables are pointing to the same object.

fig.one_person
Figure 13.7: One Person, Two Pointers

13.4.1 Functions and References

Look at the code example below. Line 1 creates a function that takes in a number as a parameter. The variable money is a variable that contains a copy of the data that was passed in. Adding 100 to that number does not change the number that was stored in bob.money on line 10. Thus, the print statement on line 13 prints out 100, and not 200.

def giveMoney1(money):
    money += 100

class Person:
    name = ""
    money = 0

bob = Person()
bob.name = "Bob"
bob.money = 100

giveMoney1(bob.money)
print(bob.money)

Running on PythonTutor we see that there are two instances of the money variable. One is a copy and local to the giveMoney1 function.

fig.function_references_1
Figure 13.8: Function References

Look at the additional code below. This code does cause bob.money to increase and the print statement to print 200.

def giveMoney2(person):
    person.money += 100

giveMoney2(bob)
print(bob.money)

Why is this? Because person contains a copy of the memory address of the object, not the actual object itself. One can think of it as a bank account number. The function has a copy of the bank account number, not a copy of the whole bank account. So using the copy of the bank account number to deposit 100 dollars causes Bob's bank account balance to go up.

fig.function_references_2
Figure 13.9: Function References

Arrays work the same way. A function that takes in an array (list) as a parameter and modifies values in that array will be modifying the same array that the calling code created. The address of the array is copied, not the entire array.

13.4.2 Review Questions

  1. Create a class called Cat. Give it attributes for name, color, and weight. Give it a method called meow.
  2. Create an instance of the cat class, set the attributes, and call the meow method.
  3. Create a class called Monster. Give it an attribute for name and an integer attribute for health. Create a method called decreaseHealth that takes in a parameter amount and decreases the health by that much. Inside that method, print that the animal died if health goes below zero.

13.5 Constructors

Video: Constructors

There's a terrible problem with our class for Dog listed below. When we create a dog, by default the dog has no name. Dogs should have names! We should not allow dogs to be born and then never be given a name. Yet the code below allows this to happen, and that dog will never have a name.

class Dog()
    name = ""

myDog = Dog()

Python doesn't want this to happen. That's why Python classes have a special function that is called any time an instance of that class is created. By adding a function called a constructor, a programmer can add code that is automatically run each time an instance of the class is created. See the example constructor code below:

class Dog():
    name=""

    # Constructor
    # Called when creating an object of this type
    def __init__(self):
        print("A new dog is born!")

# This creates the dog
myDog = Dog()

The constructor starts on line 6. It must be named __init__. There are two underscores before the init, and two underscores after. A common mistake is to only use one.

The constructor must take in self as the first parameter just like other methods in a class. When the program is run, it will print:
A new dog is born!
When a Dog object is created on line 10, the __init__ function is automatically called and the message is printed to the screen.

A constructor can be used for initializing and setting data for the object. The example Dog class above still allows the name attribute to be left blank after the creation of the dog object. How do we keep this from happening? Many objects need to have values right when they are created. The constructor function can be used to make this happen. See the code below:

class Dog():
    name = ""

    # Constructor
    # Called when creating an object of this type
    def __init__(self, newName):
        self.name = newName

# This creates the dog
myDog = Dog("Spot")

# Print the name to verify it was set
print(myDog.name)

# This line will give an error because
# a name is not passed in.
herDog = Dog()

On line 6 the constructor function now has an additional parameter named newName. The value of this parameter is used to set the name attribute in the Dog class on line 7. It is no longer possible to create a Dog class without a name. The code on line 17 tries this. It will cause a Python error and it will not run. A common mistake is to name the parameter of the __init__ function the same as the attribute and assume that the values will automatically synchronize. This does not happen.

In the next example, there are two different variables that are both called name. They are printed on lines 8 and 10. The first variable name was created as a method parameter on line 6. That method variable goes away as soon as the method is done running. The second variable is the name attribute created (also known as instance variable) on line 2. It is a completely different variable from the one created on line 6. The variable self.name refers to attribute of this particular instance of the Dog class. That attribute will exist as long as this instance of the Dog class does.

class Dog():
    name = "Rover"

    # Constructor
    # Called when creating an object of this type
    def __init__(self, name):
        # This will print "Rover"
        print(self.name)
        # This will print "Spot"
        print(name)

# This creates the dog
myDog = Dog("Spot")

13.5.1 Review Questions

  1. Should class names begin with an upper or lower case letter?
  2. Should method names begin with an upper or lower case letter?
  3. Should attribute names begin with an upper or lower case letter?
  4. Which should be listed first in a class, attributes or methods?
  5. What are other names for a reference?
  6. What is another name for instance variable?
  7. What is the name for an instance of a class?
  8. Create a class called Star that will print out “A star is born!” every time it is created.
  9. Create a class called Monster with attributes for health and a name. Add a constructor to the class that sets the health and name of the object with data passed in as parameters.

13.6 Inheritance

Video: Inheritance

Another powerful feature of using classes and objects is the ability to make use of inheritance. It is possible to create a class and inherit all of the attributes and methods of a parent class.

For example, a program may create a class called Boat which has all the attributes needed to represent a boat in a game:

class Boat():
    tonnage = 0
    name = ""
    isDocked = True

    def dock(self):
        if self.isDocked:
            print("You are already docked.")
        else:
            self.isDocked = True
            print("Docking")

    def undock(self):
        if not self.isDocked:
            print("You aren't docked.")
        else:
            self.isDocked = False
            print("Undocking")

To test out our code:

b = Boat()

b.dock()
b.undock()
b.undock()
b.dock()
b.dock()

The outputs:

You are already docked.
Undocking
You aren't docked.
Docking
You are already docked.

(If you watch the video for this section of the class, you'll note that the "Boat" class in the video doesn't actually run. The code above has been corrected but I haven't fixed the video. Use this as a reminder, no matter how simple the code and how experienced the developer, test your code before you deliver it!)

Our program also needs a submarine. Our submarine can do everything a boat can, plus we need a command for submerge. Without inheritance we have two options.

Luckily, there is a better way. Our program can create child classes that will inherit all the attributes and methods of the parent class. The child classes may then add fields and methods that correspond to their needs. For example:

class Submarine(Boat):
    def submerge(self):
        print("Submerge!")

Line 1 is the important part. Just by putting Boat in between the parentheses during the class declaration, we have automatically picked up every attribute and method that is in the Boat class. If we update Boat, then the child class Submarine will automatically get these updates. Inheritance is that easy!

The next code example is diagrammed out in Figure 13.10.

fig.person1
Figure 13.10: Class Diagram
class Person():
    name = ""

class Employee(Person):
    job_title = ""

class Customer(Person):
    email = ""

johnSmith = Person()
johnSmith.name = "John Smith"

janeEmployee = Employee()
janeEmployee.name = "Jane Employee"
janeEmployee.job_title = "Web Developer"

bobCustomer = Customer()
bobCustomer.name = "Bob Customer"
bobCustomer.email = "send_me@spam.com"

By placing Person between the parentheses on lines 4 and 7, the programmer has told the computer that Person is a parent class to both Employee and Customer. This allows the program to set the name attribute on lines 14 and 18.

Methods are also inherited. The code below will print out “Person created” three times because the employee and customer classes inherit the constructor from the parent class:

class Person():
    name = ""

    def __init__(self):
        print("Person created")

class Employee(Person):
    job_title = ""

class Customer(Person):
    email = ""

johnSmith = Person()
janeEmployee = Employee()
bobCustomer = Customer()

Methods may be overridden by a child class to provide different functionality. In the code below, the child classes have their own constructors, so the parent's class will not be run:

class Person():
    name = ""

    def __init__(self):
        print("Person created")

class Employee(Person):
    job_title = ""

    def __init__(self):
        print("Employee created")

class Customer(Person):
    email = ""

    def __init__(self):
        print("Customer created")

johnSmith = Person()
janeEmployee = Employee()
bobCustomer = Customer()

If the programmer desires to run both the parent and the child class's method, the child may explicitly call the parent's method:

class Person():
    name = ""

    def __init__(self):
        print("Person created")

class Employee(Person):
    job_title = ""

    def __init__(self):
        Person.__init__(self)
        print("Employee created")

class Customer(Person):
    email = ""

    def __init__(self):
        Person.__init__(self)
        print("Customer created")

johnSmith = Person()
janeEmployee = Employee()
bobCustomer = Customer()

13.6.1 Is-A and Has-A Relationships

Classes have two main types of relationships. They are “is a” and “has a” relationships.

A parent class should always be a more general, abstract version of the child class. This type of child to parent relationship is called an is a relationship. For example, a parent class Animal could have a child class Dog. The Dog class could have a child class Poodle. Another example, a dolphin is a mammal. It does not work the other way, a mammal is not necessarily a dolphin. So the class Dolphin should never be a parent to a class Mammal. Likewise a class Table should not be a parent to a class Chair because a chair is not a table.

The other type of relationship is the has a relationship. These relationships are implemented in code by class attributes. A dog has a name, and so the Dog class has an attribute for name. Likewise a person could have a dog, and that would be implemented by having the Person class have an attribute for Dog. The Person class would not derive from Dog because that would be some kind of insult.

Looking at the prior code example we can see:

13.7 Review

13.7.1 Multiple Choice Quiz

Click here for a multiple-choice quiz.

13.7.2 Short Answer Worksheet

Click here for the chapter worksheet.

13.7.3 Lab

Click here for the chapter lab.


You are not logged in. Log in here and track your progress.

About Buy the Book Help Translate My College My Twitter Updates