Computer Programming Home Page

All document links can be viewed with this link... the sharing service I've been using has changed their policy as of March 15th, direct download links are no longer possible.




##  pygame_standard_setup.py

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

##  BUILD CUSTOM SPRITE CLASSES HERE

class Wall(pygame.sprite.Sprite):
    """A wall that the player cannot cross"""
    def __init__(self, color, x, y, width, height):
        super().__init__()
        # make a surface
        self.image = pygame.Surface((width, height))
        self.image.fill(color)
        # make Rect from surface, move into position
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y


class Player(pygame.sprite.Sprite):
    """Represents the player"""
    def __init__(self, color, x, y):
        super().__init__()
        # make a surface sixe 20 x 20
        self.image = pygame.Surface((20, 20))
        self.image.fill(color)
        # make a Rect, move into position
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        # variables to hold speed
        self.changex = 0
        self.changey = 0
        # holds walls in game (None to start with, build later)
        self.walls = None

    def changespeed(self, x, y):
        self.changex = self.changex + x
        self.changey = self.changey + y

    def update(self):
        # move left / right
        self.rect.x = self.rect.x + self.changex
        # did we hit a wall? don't delete the wall
        block_hit_list = pygame.sprite.spritecollide(self, self.walls, False)
        for block in block_hit_list:
            # if moving right, set right side of player to be the
            # left side of the item we hit
            if self.changex > 0:
                self.rect.right = block.rect.left
            else:
                # were moving left, so set the left side of player
                # to be right side of the item we hit
                self.rect.left = block.rect.right
        
        # move up / down
        self.rect.y = self.rect.y + self.changey
        # did we hit a wall? Don't delete the wall
        block_hit_list = pygame.sprite.spritecollide(self, self.walls, False)
        for block in block_hit_list:
            # if moving down, set botom of player to be the
            # up side of the item we hit
            if self.changey > 0:
                self.rect.bottom = block.rect.top
            else:
                # were moving right, so set the right side of the player
                # to be the left side of the item we hit
                self.rect.top = block.rect.bottom

        

pygame.init()

##  Hide the cursor while in window
pygame.mouse.set_visible(0)

APPWIDTH = 800
APPHEIGHT = 600
APPNAME = 'Window title'

DISPLAYSURF = pygame.display.set_mode((APPWIDTH, APPHEIGHT))
pygame.display.set_caption(APPNAME)
FPS = 60
fpsClock = pygame.time.Clock()

WHITE  = (255,  255,  255)
BLACK  = (0,    0,    0)
RED    = (255,  0,    0)
GREEN  = (0,    255,  0)
BLUE   = (0,    0,    255)



##  BUILD SPRITE GROUPS HERE
wall_list = pygame.sprite.Group()
all_sprite_list = pygame.sprite.Group()


##  BUILD SPRITES HERE
w = Wall(BLUE, 0, 0, 10, 600)
wall_list.add(w)
all_sprite_list.add(w)

w = Wall(BLUE, 10, 0, 790, 10)
wall_list.add(w)
all_sprite_list.add(w)
 
w = Wall(BLUE, 10, 200, 100, 10)
wall_list.add(w)
all_sprite_list.add(w)

w = Wall(BLUE, 300, 200, 10, 200)
wall_list.add(w)
all_sprite_list.add(w)


# Build the Player
player = Player(RED, 50, 50)
player.walls = wall_list
all_sprite_list.add(player)


while True: # the main game loop

    ## HANDLE EVENTS BELOW HERE
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
            
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                player.changespeed(-3, 0)
            elif event.key == pygame.K_RIGHT:
                player.changespeed(3, 0)
            elif event.key == pygame.K_UP:
                player.changespeed(0, -3)
            elif event.key == pygame.K_DOWN:
                player.changespeed(0, 3)
 
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                player.changespeed(3, 0)
            elif event.key == pygame.K_RIGHT:
                player.changespeed(-3, 0)
            elif event.key == pygame.K_UP:
                player.changespeed(0, 3)
            elif event.key == pygame.K_DOWN:
                player.changespeed(0, -3)


    ##  HANDLE GAME LOGIC BELOW HERE

    ##  REDRAW SCREEN BELOW HERE
    all_sprite_list.update()
    DISPLAYSURF.fill(BLACK)
    all_sprite_list.draw(DISPLAYSURF)
    pygame.display.update()
    fpsClock.tick(FPS)