↖ Back to index

Objects

Before we go further, we need to stop a moment to organise our code a bit better. This will help us later to deal with many things moving at the same time.

A bit of theory: objects

Until now, we have had one thing moving around the screen. This thing had three qualities that defined it:

When things have several qualities, it's useful to store them together. This becomes helpful later as it makes it easier to know what belongs to what. In many programming languages, we have "objects" that help us do exactly that.

Start a new, blank program and type the following:

class Student:
    pass

s = Student()
s.name = "Ash"
s.age = "10"

The two first lines define a new "class" of "objects". You can read it as "some things in this program are going to be 'students'". It may sound a bit weird, but it'll be useful later. Don't worry too much, you don't have to understand it completely.

The next three lines read as follows:

This is not very different from creating variables for names and ages as we have done before. The thing that is different is that name and age are going to live inside s. We'll use the dot . symbol to read and write the student's name and age.

Using the dot . symbol, we can access the name and age, same as we do with any other variable. For example, add this to the program:

print("My name is")
print(s.name)
print("and I am")
print(s.age)
print("years old")

So nothing new. Only that now we are putting variables inside other variables to help us organise the program better. There's a lot more that can be done with objects, but we can do just with this for the moment.

Back to Pygame: using an object in our game

We are going to create an object that will represent our moving image. A moment ago we used an object that had a name and an age. Now we are going to have an object that has horizontal and vertical positions (x and y), and an image.

Go back to your Pygame program and try make the following changes:

If everything goes according to plan, the program should do exactly the same thing it was doing before. This was not about adding something to the program, but about organising things before we go forward.