Code from the PyGame Session

Following are the code snippets from the session on PyGame I ran earlier today.

Don’t forget to stick with IDLE and Python 2.7 (unless you’ve updated your Pi) and check out these PyGame resources when you’re ready to move on to the next level.

Credits & Sprites

Of course you can use your own sprites but I’ve used the Raspberry and the Spoon from Dr Simon Monk’s code from his book Programming the Raspberry Pi: Getting Started with Python.

You can download the two sprites here:

Intro to PyGame

# Getting Started with PyGame

# First we need to import the framework:

import pygame
import sys
from pygame.locals import *

# Now we can initialize Pygame

pygame.init()

# And nothing happens!

# The Screen

# We need to define a "screen" for PyGame to draw on
# think of the Screen as your stage

# Set the dimensions in a variable
screen_dimensions = (640, 480)

# create the screen
screen = pygame.display.set_mode(screen_dimensions)

# Fill it with a solid colour
screen.fill((255, 255, 255))

# Call update - very important!
pygame.display.update()

# Set the caption (title bar)
pygame.display.set_caption('Hello Pygame')

pygame.time.wait(5000)  # sleep for 10 secs so you can see!

# Loading Sprites

# A sprit is an actor on our screen / stage

# You can load them into PyGame from files

raspberry = pygame.image.load('raspberry.jpg').convert()

spoon = pygame.image.load('spoon.jpg').convert()

# All we've done so far is load the images into variables

# Drawing / Blitting

# Drawing your sprites onto the screen is easy

screen.blit(raspberry, (300, 100))
screen.blit(spoon, (300, 200))

pygame.display.update()

pygame.time.wait(5000)

sys.exit()

Getting Keyboard Input with PyGame (WASD Keys)

import pygame
import sys
from pygame.locals import *

pygame.init()
screen_x = 640
screen_y = 480
screen = pygame.display.set_mode((screen_x, screen_y))
screen.fill((255, 255, 255))
pygame.display.update()
pygame.display.set_caption('Pygame KeyBoard Demo')

raspberry = pygame.image.load('raspberry.jpg').convert()

rasp_x = 300
rasp_y = 100

step = 1

screen.blit(raspberry, (rasp_x, rasp_y))

pygame.display.update()

run = True

while run:

    for event in pygame.event.get():
        if event.type == QUIT:
            running = False

    key = pygame.key.get_pressed()

    if key[pygame.K_d]:
        # right pressed
        rasp_x += step
    elif key[pygame.K_s]:
        # down pressed
        rasp_y += step
    elif key[pygame.K_w]:
        # up pressed
        rasp_y -= step
    elif key[pygame.K_a]:
        # left pressed
        rasp_x -= step

    if rasp_x < 0:
        rasp_x = 0

    if rasp_y < 0:
        rasp_y = 0

    if rasp_x > screen_x - 50:
        rasp_x = screen_x - 50

    if rasp_y > screen_y - 50:
        rasp_y = screen_y - 50

    screen.fill((255, 255, 255))

    screen.blit(raspberry, (rasp_x, rasp_y))

    pygame.display.update()

sys.exit()

Getting Mouse Input with PyGame

import pygame
import sys
from pygame.locals import *

pygame.init()

screen_dimensions = (640, 480)

screen = pygame.display.set_mode(screen_dimensions)

screen.fill((255, 255, 255))

pygame.display.update()

pygame.display.set_caption('Pygame Mouse Demo')

raspberry = pygame.image.load('raspberry.jpg').convert()

rasp_x = 300
rasp_y = 100

screen.blit(raspberry, (rasp_x, rasp_y))

pygame.display.update()

run = True

while run:

    for event in pygame.event.get():
        if event.type == QUIT:
            running = False

    screen.fill((255, 255, 255))
    rasp_x, ignore = pygame.mouse.get_pos()

    if rasp_x < 0:
        rasp_x = 0

    if rasp_y < 0:
        rasp_y = 0

    if rasp_x > screen_dimensions[0] - 50:
        rasp_x = screen_dimensions[0] - 50

    if rasp_y > screen_dimensions[1] - 50:
        rasp_y = screen_dimensions[1] - 50

    screen.blit(raspberry, (rasp_x, rasp_y))
    pygame.display.update()

sys.exit()

Full Raspberry Catching Game
(credit Dr. Simon Monk)

import pygame
from pygame.locals import *
from sys import exit
import random

score = 0

screen_width = 600
screen_height = 400

spoon_x = 300
spoon_y = screen_height - 100

raspberry_x = random.randint(10, screen_width)
raspberry_y = 0

clock = pygame.time.Clock()

pygame.init()

screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Raspberry Catching')

spoon = pygame.image.load('spoon.jpg').convert()
raspberry = pygame.image.load('raspberry.jpg').convert()


def update_spoon():
    global spoon_x
    global spoon_y
    spoon_x, ignore = pygame.mouse.get_pos()
    screen.blit(spoon, (spoon_x, spoon_y))


def update_raspberry():
    global raspberry_x
    global raspberry_y
    raspberry_y += 5
    if raspberry_y > spoon_y:
        raspberry_y = 0
        raspberry_x = random.randint(10, screen_width)
    raspberry_x += random.randint(-5, 5)
    if raspberry_x < 10:
        raspberry_x = 10
    if raspberry_x > screen_width - 20:
        raspberry_x = screen_width - 20
    screen.blit(raspberry, (raspberry_x, raspberry_y))


def check_for_catch():
    global score
    if raspberry_y >= spoon_y and raspberry_x >= spoon_x and raspberry_x < spoon_x + 50:
        score += 1
    display("Score: " + str(score))


def display(message):
    font = pygame.font.Font(None, 36)
    text = font.render(message, 1, (10, 10, 10))
    screen.blit(text, (0, 0))

while True:

    for event in pygame.event.get():
        if event.type == QUIT:
            exit()

    screen.fill((255, 255, 255))
    update_raspberry()
    update_spoon()
    check_for_catch()
    pygame.display.update()
    clock.tick(10)

5 thoughts on “Code from the PyGame Session

  1. Where can I download the random module because no matter how much I look I can’t find a site to download it from.

Leave a Reply