Professional Documents
Culture Documents
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
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.
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.
#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.
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):
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:
self.color = pygame.Color(color)
self.radius = radius
self.center = center
self.velocity = velocity
self.surface = surface
def draw(self):
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
#
edge = ''
if self.center[0] < self.radius:
edge = 'right'
if self.center[0] + self.radius > 500:
edge = 'left'
return edge
main()