This tutorial follows on from Part 1.

Credit goes to Peter for much of the content in this tutorial.
Objects:
Whilst our previous program worked it
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
problems by:
  1. Using OO programing by using
    objects
  2. 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).
In Python, we create a object class by
using:
class <name_of_class>:
The benefit of using objects should
become apparent by the end of this tutorial.
For our program I have chosen to name
ours class Player1:
class Player1:
Object classes have a special
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:
def __init__(self, arguments,):
Although ‘self’ is not strictly
required in general it is always used as it allows us to initialise
internal variables.
Now that we have created a Player1
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:

 

class Player1:
def __init__(self, (x,y)):
self.x = x;
self.y = y;
self.img = pygame.image.load(“pygame.png”);
self.speed = 0.05;

 

def display(self):
# Blit image onto the display surface
screen.blit(self.img, (self.x,self.y));

self‘ means that we are
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.
We now need to create an object using
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:

 

Player = Player1((0,0));
Where there (0,0) represents the
initial location we want to start the player at. ‘Player’ is the name
of our Player1 object.
We now need to update some of the other
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
is:

 

# Update game state – of the player (this will need to be moved later)
keys = pygame.key.get_pressed();
if (keys[pygame.K_LEFT]):
Player.x -= Player.speed;
elif (keys[pygame.K_RIGHT]):
Player.x += Player.speed;
if (keys[pygame.K_UP]):
Player.y -= Player.speed;
elif (keys[pygame.K_DOWN]):
Player.y += Player.speed;
We also need to update the blit(0) onto
the screen surface:

 

screen.blit(player.img, (player.x,player.y));
This, however isn’t the best way to go
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
called display():
def display(self):

 

# Blit image onto the display surface
screen.blit(self.img, (self.x,self.y));
We can now replace the blit() in the
main/game loop with:

 

Player.display(); # display the player’s pos
This makes the code cleaner and more
managable later on.
If you run the program you should have
the same results as previously.
Randomness:
Randomness in a game is important as it
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:
include random;
We now have access to various functions
that generate random numbers. The functions that are most often used
are:
random() – returns a random number
between 0.0 and 1.0
uniform(a, b) – returns a random number
between a and b
randint(a, b) – returns a random
integer between a and b
choice(list) – returns a random element
from the list
shuffle(list) – rearranges the order of
the list
We will only be using randint(a,b). To
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:

 

class Particle:
def __init__(self, (x,y), size):
self.x = x;
self.y = y;
self.size = size;
self.colour = (255, 0, 0);
self.thickness = 1;

 

def display(self):
pygame.draw.circle(screen, self.colour, (self.x,self.y), self.size, self.thickness);

We also need to add the code to create
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:

 

#create some particles
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_wsize)
y = random.randint(size, screen_hsize)
particles.append(Particle((x, y), size)) #add particle to the list
The size and position of the particle
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.
To display the particles we need to add
the following code imediately below (or bfore) the call to draw the
player:
for particle in particles:

 

particle.display();
Running the program now should generate
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.

 

In the next tutorial we will look at
creating movement controlled by the computer as well as setting
bounding boxes.
Advertisements