You are on page 1of 7

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
Score

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

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
Draw score
Erase the window
# draw the Game objects

Update score
When the dot touches the right side of the window update left
scoreboard
When the dot touches the left side of the window update right
scoreboard

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

Collision
Of the windows edges:
Ball is bouncing of the window edges to the opposite side

Of the paddles:
Ball bounces of the inside of the paddles to the opposite side
Ball goes through the back of the paddles
# Version 2 of Pong
# The ball start in the middle.
# The ball bounces back of the window edges
# The ball go through the back side of each paddle but bounce off the front side
of each paddles
# There is a scoreboard
# The paddles don't move
# game runs until player closes the window or when one of the player hits 11.

from uagame import Window


import time
import pygame
from pygame.locals import *

def main():
# Create our game window and game, then play
# until a game end condition is met.
window = Window('Pong', 500, 400)
window.set_auto_update(False)
game = Game(window)
game.play()
window.close()

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

# set our attribute values


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
self.ball_color = 'white'
self.ball_radius = 5
self.ball_center = [250, 200]
self.ball_velocity = [4, 1]
self.ball = Ball(self.ball_color, self.ball_radius, self.ball_center,
self.ball_velocity, self.surface)
#Paddle 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')

#score attribute
self.player_1_score = 0
self.player_2_score = 0
self.max_score = 11

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:


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

key = pygame.key.get_pressed()

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.draw_score()
self.window.update()

def update_score(self,edge):
if edge=='right':
self.player_2_score += 1
if edge=='left':
self.player_1_score += 1

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

edge = self.ball.update(self.paddle_left,self.paddle_right)
self.update_score(edge)

def draw_score(self):
# Draws the scoreboard on the window in the top left corner
#self: is the game to draw the score for

score_string1 = str(self.player_1_score)
score_string2 = str(self.player_2_score)
score_size = 70
score_color = ('white')
self.window.set_font_size(score_size)
self.window.set_font_color(score_color)
string_width = self.window.get_string_width(score_string2)
window_width = self.window.get_width()
player_2_xcoord = window_width - string_width
self.window.draw_string(score_string2,player_2_xcoord,0)
self.window.draw_string(score_string1,0,0 )

def decide_continue(self):

# Check and remember if the game should continue.


# If it should not, set continue_game to False
# - self is the Game to check

event = pygame.event.poll()
if event.type == QUIT:
self.close_clicked = True
if self.player_1_score == self.max_score or self.player_2_score ==
self.max_score:
self.continue_game = False

class Ball:

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


# this method initializes our a dot object
# - self : the object that we are initializing
# - color : the color the dot is drawn in; must be a pygame
Color object
# - radius: the distance between the center of the circle and
its edge; must be integer
# - center: the middle point of the dot; must be in list form
[x, y]
# - velocity: speed and direction of the dot; must be in list
form [x, y] where negative values mean up/left and positive values
mean down/right
# - surface: the surface on which we draw the dot
self.color = color

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

def draw(self):

# draws a dot on its specified surface


# - self : the Dot object that is being drawn

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

def update(self,left_paddle_Rect,right_paddle_Rect):

# If the dot reaches the edge of the screen, it wraps to the other
side.
# - self: the Dot object to be moved
# check if the dot is moving outside of the screen on one
# of our axes. If so, reverse its direction. Apply to all axes

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

#Ball bounce around the window edges


surface_boundaries = self.surface.get_size()
for i in range(len(self.center)):
if self.center[i] - self.radius < 0 or self.center[i] + self.radius >
surface_boundaries[i]:
self.velocity[i] *= -1

#Ball bounce of the paddles:

if self.velocity[0] < 0 and left_paddle_Rect.collidepoint(self.center[0] -


self.radius, self.center[1]):
self.velocity[0] = -self.velocity[0]
if self.velocity[0] > 0 and
right_paddle_Rect.collidepoint(self.center[0]+self.radius, self.center[1]):
self.velocity[0] = -self.velocity[0]

#
edge = ''
if self.center[0] < self.radius:
edge = 'right'
if self.center[0] + self.radius > 500:
edge = 'left'
return edge

main()

You might also like