Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Main algorithms:

Create window
Create game
Play game
Close window

Class Game
Instance Attributes
window # the window on which to draw
pause_time # pause time between drawing frames
close_clicked # indicates if close button was clicked
# add attributes as required
Ball
paddles

Instance Methods/Blocks
initialize instance
initialize/create all instance attributes
initialize Ball
Initialize paddles

play game
while not close_clicked
# ‘play’ a single frame
handle next event
draw the current frame
if continue_game:
update all game objects
pause before next iteration/frame

handle event
get next event from the event queue
if event type == QUIT
close_clicked = True

update game objects


# update Game objects to new position in next frame
update Ball

draw frame
erase the window
# draw the Game objects
draw Ball
Draw paddles

Class Ball
Instance Attributes
Color, radius, velocity, center, surface

Instance Methods
initializer
Create a new instance of Dot, setting
all attributes to values supplied by
arguments

draw
Display dot to window, using the attributes of the dot (color, radius, center)

update
Change the center of the dot based on its velocity

Version 1 of pong Code:

# V1 version of Pong
# The dot start in the middle.
#The dot go through the paddles and wraps around the window edges
# There is no scoreboard
# The paddles don't move
# game runs until player closes the window

from uagame import Window


import time
import pygame
from pygame.locals import *

#User-defined functions
def main():
window = Window('Pong', 500, 400)
window.set_auto_update(False)
game = Game(window)
game.play()
window.close()
#User-defined classes
class Game:

# An object in this class represents a complete game.

def __init__(self, window):


# Initialize a Game.
# - self is the Game to initialize
# - window is the uagame window object

self.window = window
self.bg_color = pygame.Color('black')
self.pause_time = 0.01
self.close_clicked = False
self.continue_game = True
self.surface = window.get_surface()

#Ball attributes
ball_color = 'white'
ball_radius = 5
ball_center = [250, 200]
ball_velocity = [4, 1]
self.ball = Ball(ball_color, ball_radius, ball_center, ball_velocity, self.surface)

#Paddles attribute
self.paddle_left = pygame.Rect(100,175,10,40)
self.paddle_right = pygame.Rect(400,175,10,40)
self.Paddles_color = pygame.Color('white')

def play(self):
# Play the game until the player presses the close box.
# - self is the Game that should be continued or not.

while not self.close_clicked:


#Play frame
self.handle_event()
self.draw()
if self.continue_game:
self.update()
self.decide_continue()
time.sleep(self.pause_time)

def handle_event(self):
# Handle each user event by changing the game state
# appropriately.
# - self is the Game whose events will be handled.

event = pygame.event.poll()
if event.type == QUIT:
self.close_clicked = True

def draw(self):
# Draw all game objects.
# - self is the Game to draw

self.window.clear()
self.ball.draw()
pygame.draw.rect(self.surface, self.Paddles_color, self.paddle_left)
pygame.draw.rect(self.surface, self.Paddles_color, self.paddle_right)
self.window.update()

def update(self):
# Update the game objects.
# - self is the Game to update
# we need to update the position of the dot

self.ball.update()

def decide_continue(self):
# Check and remember if the game should continue
# - self is the Game to check
# check if there is a 'close window' event

event = pygame.event.poll()
if event.type == QUIT:
self.close_clicked = True

class Ball:

def __init__(self, color, radius, center, velocity, surface):


self.color = pygame.Color(color)
self.radius = radius
self.center = center
self.velocity = velocity
self.surface = surface

def draw(self):
pygame.draw.circle(self.surface, self.color, self.center, self.radius)

def update(self):
# move the dot to a new position, based on its velocity. For
Version 1,
# If the dot reaches the edge of the screen, it wraps to the other
side.
# - self: the Dot object to be moved

window_width = 500
window_height = 400

# update our x-coordinate and and wrap along x-axis


self.center[0] = (self.center[0] + self.velocity[0]) % window_width
# update our y-coordinate and wrap along y-axis
self.center[1] = (self.center[1] + self.velocity[1]) % window_height

main()

You might also like