Professional Documents
Culture Documents
Pongv 1
Pongv 1
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
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
# 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
#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:
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.
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 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
main()