This tutorial follows on from Part 1.
wasn’t overly interesting and the code wasn’t writen in a way to make
it managable once the game got larger. We will work these two
Using OO programing by using
Introduce some randomised bubbles
into the game to show the power/benefit of using objects (these will
be made interactive with our ‘player’ in a later tutorial).
become apparent by the end of this tutorial.
ours class Player1:
initialiser function which is automatically called when we create an
object using the class. The number of arguments required when we
create the object will depend on this class. In Python the
initialiser function is written like this:
required in general it is always used as it allows us to initialise
class and have the beginings of an initialiseation function we have
to start moving the variables relating to the player into the class.
The only real variables we have are x, y, speed, and the image. When
we create the player we (probably) want to initialise at least some
of theses variables. I will do this using the following:
def __init__(self, (x,y)):
self.x = x;
self.y = y;
self.img = pygame.image.load(“pygame.png”);
self.speed = 0.05;
# Blit image onto the display surface
refering to the instance of a created object. When we create a
Player1 object we need to specify its x and y coordinates as theses
are arguments of the initialisation function. The player image and
speed have been hardcoded into the class.
this class. I will creativly name my object Player. The player object
should be created before the
main/game loop as we only need to create the player once not
every loop iteration. To
create the player we simply write:
initial location we want to start the player at. ‘Player’ is the name
of our Player1 object.
code to use this object. I will start with the directional arrow
events that change the position of the player. This code will need to
be moved at a later date, but at the moment we will leave it where it
keys = pygame.key.get_pressed();
Player.x -= Player.speed;
Player.x += Player.speed;
Player.y -= Player.speed;
Player.y += Player.speed;
the screen surface:
about writing the code to display the code. Instead we should include
it in the class. Todo this, we will add a new function into the class
main/game loop with:
managable later on.
the same results as previously.
alows us to create a unique gaming eperiance instead of each level
being exactly the same every time it is played. To use random numbers
in Python (not Pygame), we need to include the random module:
that generate random numbers. The functions that are most often used
between 0.0 and 1.0
between a and b
integer between a and b
from the list
demonstrate its use and develop a further appreciation of objects we
will create a new object wich will be called Particle, the code is
quite similar to the Player1 class:
def __init__(self, (x,y), size):
self.x = x;
self.y = y;
self.size = size;
self.colour = (255, 0, 0);
self.thickness = 1;
pygame.draw.circle(screen, self.colour, (self.x,self.y), self.size, self.thickness);
a Particle object. To make things more interesting we will create
several of these objects. I have chosen 7, but you can choose
whatever and even experiment with some randomised number:
number_of_particles = 7;
particles =  # create an empty list
for n in range(number_of_particles):
size = random.randint(10, 20)
x = random.randint(size, screen_w–size)
y = random.randint(size, screen_h–size)
particles.append(Particle((x, y), size)) #add particle to the list
has been ranomised. The position is anywhere on the screen, offset by
the size of the particle so that it is fully visible. The particles
will be managed by creating a list of the Particle objects.
the following code imediately below (or bfore) the call to draw the
7 particles positioned randomly around the screen. If the program is
exited and rerun the particles locations and sizes should change.
With 8 different x and y coordinates, hopefully the advantage of
using objects/classes is clear.
creating movement controlled by the computer as well as setting