# Breakout
# Joel Longanecker, 2007
# Made with python 2.5
# system and pygame modules required
import sys, pygame
# this is a static class with all the game variables
class gameVars():
# the paddle sub-class
class paddle():
x = 0
y = 0
w = 128
h = 16
# the ball sub-class
class ball():
x = 0
y = 0
vx = 0
vy = 0
radius = 8
# the arena sub-class
class arena():
w = 512
h = 640
score = 0
lives = 0
round = 0
balls = [] # The balls are like bricks, only simpler, a tuple of 4 (x, y, rad, (r,g,b))
# this class contains all the code of the game.
class game():
# The constructor initializes the game and game elements
def __init__(self):
# Initalize the screen
pygame.init()
self.screen = pygame.display.set_mode((gamestuff.arena.width, gamestuff.arena.height))
# Initalize the ball (position and velocity)
gamestuff.ball.x = 256.0
gamestuff.ball.y = 384.0
gamestuff.ball.vx = -1.25
gamestuff.ball.vy = -1.33
# Initialize the paddle
gamestuff.paddle.x = 64.0
gamestuff.paddle.y = gamestuff.arena.height - 32.0
# It's important to have a start time, get the intial value of the timer.
self.oldTime = pygame.time.get_ticks()
# loop defines what's going on durring each pass
def loop(self):
# Calculate the change in time since the last time loop was called.
# If this is the first pass, the oldTime was seeded in the constructor.
newTime = pygame.time.get_ticks()
dTime = (float(newTime - self.oldTime) / 1000.0) * 125.0
self.oldTime = newTime
# Poll for the events and what keys are currently pressed.
e = pygame.event.poll()
keys = pygame.key.get_pressed()
# Make a local copy of the game variables.
# These are shallow coppies, so any changes made here are made in the originals :D
paddle = gamestuff.paddle
ball = gamestuff.ball
arena = gamestuff.arena
# If an event has been found, process it!
if not(e == pygame.NOEVENT):
# If the event is quit, then quit
if e.type == pygame.QUIT:
sys.exit()
# Even though key presses aren't being checked through the event pump, they
# still generate an event when pressed or held down. Checking through
# get_keyPressed allows for rapid input response.
if keys[pygame.K_LEFT]:
paddle.x -= 10
# Move the paddle and check the position.
if paddle.x < 0:
paddle.x = 0
if keys[ pygame.K_RIGHT]:
paddle.x += 10
# Again, move the paddle and check the position.
if (paddle.x + paddle.w)>= arena.w:
paddle.x = arena.w - paddle.w
# Do some physics magic with the ball to make it move around the screen.
# The great thing is that the ball should move at a frame-rate independent
# from the system.
ball.x += ball.vx * dTime
ball.y += ball.vy * dTime
if(ball.x < 0):
ball.vx = -ball.vx
ball.x = 0
if(ball.x > arena.w):
ball.vx = -ball.vx
ball.x = arena.w
if(ball.y < 0):
ball.vy = -ball.vy
ball.y = 0
if(ball.y > arena.h):
ball.vy = -ball.vy
ball.y = arena.h
if(ball.y > paddle.y) and ((ball.y) < (paddle.y + paddle.h)) and ball.vy > 0.0:
if((ball.x + ball.radius) > paddle.x) and ((ball.x - ball.radius) < paddle.x+ paddle.w):
ball.vy = -ball.vy
ball.y = paddle.y-ball.radius