Making Simple Games in Python

The Summer Holidays are almost here. The RPi foundation has announced its first programming competition. Let’s write some games!

You can download the code and sound files for this program here.

This program includes several features that are useful when making games. Try thinking of something you’d like to do in a game and then adapt the code to create the effect you want.

I haven’t managed to get WordPress to show the indentations correctly. I’d recommend downloading the file rather than trying to cut and paste!

# keys.py
# simple game program

# import the pygame library
import pygame

# initialise the game engine
pygame.init()

# define colours
raspberry =  [135,  38,  87]
green     =  [152, 251, 152]
black     =  [  0,   0,   0]
blue      =  [  0,   0, 255]
red       =  [255,   0,   0]
yellow    =  [255, 255,   0]
colours = [blue, red, yellow, raspberry]

# set the height and width of the screen & title
size = [400, 300]
screen = pygame.display.set_mode(size)
screen_rect = screen.get_rect()

# create a surface to hold the “blob”
blob = pygame.Surface((40,40))
blob.set_colorkey(black)
blob_rect = blob.get_rect()
blob_rect.center = screen_rect.center
blob_col = 0

def change_blob(blob_col):
if blob_col == 3:
blob_col = 0
else:
blob_col += 1
boing.play()
pygame.draw.circle(blob,(colours[blob_col]), (20, 20), 20)
return blob_col

# draw initial blob
pygame.draw.circle(blob, (colours[blob_col]), (20, 20), 20)

# initial speed = 0
x_speed = 0
y_speed = 0

# load sounds
wand  = pygame.mixer.Sound(“wand.wav”)
cow   = pygame.mixer.Sound(“cow_toy.wav”)
boing = pygame.mixer.Sound(“boing_x.wav”)
frog  = pygame.mixer.Sound(“frog.wav”)
horn  = pygame.mixer.Sound(“horn.wav”)
laser = pygame.mixer.Sound(“laser.wav”)
sounds = [wand, cow, boing, frog, horn, laser]
sound_num = 0

# give the window a title
pygame.display.set_caption(“Keys”)

# loop until the user clicks the close button
done = False

# create a timer to control how often the screen updates
clock = pygame.time.Clock()

# main game loop
while done==False:

# fill the screen with a colour
screen.fill(green)

# event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
done=True

# Key-presses
if event.type == pygame.KEYDOWN:

# q for quit
if event.key == pygame.K_q:
done = True

# c to change colour
if event.key == pygame.K_c:
blob_col = change_blob(blob_col)

# play sounds if you press 1
if event.key == pygame.K_1:
sounds[sound_num].play()
if sound_num == 5:
sound_num = 0
else:
sound_num += 1

# arrow keys for movement
if event.key == pygame.K_RIGHT:
x_speed = 1
if event.key == pygame.K_LEFT:
x_speed = -1
if event.key == pygame.K_UP:
y_speed = -1
if event.key == pygame.K_DOWN:
y_speed = 1

# make speed 0 when keys released
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
x_speed = 0
if event.key == pygame.K_RIGHT:
x_speed = 0
if event.key == pygame.K_UP:
y_speed = 0
if event.key == pygame.K_DOWN:
y_speed = 0

# move the blob
blob_rect.centerx += x_speed
blob_rect.centery += y_speed

# prevent blob moving off screen
if blob_rect.top <= screen_rect.top:
blob_rect.top = screen_rect.top + 10
y_speed = 1
blob_col = change_blob(blob_col)

if blob_rect.bottom >= screen_rect.bottom:
blob_rect.bottom = screen_rect.bottom – 10
y_speed = -1
blob_col = change_blob(blob_col)

if blob_rect.left <= screen_rect.left:
blob_rect.left = screen_rect.left + 10
x_speed = 1
blob_col = change_blob(blob_col)

if blob_rect.right >= screen_rect.right:
blob_rect.right = screen_rect.right – 10
x_speed = -1
blob_col = change_blob(blob_col)

# blit the blob to the screen
screen.blit(blob, blob_rect)

# set the loop to 100 cycles per second
clock.tick(100)

#  update the display
pygame.display.flip()

# close pygame
pygame.quit()

If any of this seems unclear, please leave me a comment!

antiloquax

Advertisements

2 Responses to “Making Simple Games in Python”

  1. jimredddd9 Says:

    when i test it it comes up with an error message saying:

    ImportError: No module named pygame

    What should i do, whats wrong?

  2. antiloquax Says:

    It looks like you haven`t got pygame installed. Are you using a Raspberry Pi? If you let me know a bit more about your system, I will try to help. Or you can go to pygame.org and look for the download.
    Mark

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: