Teach Your Kids To Code: A Parent-friendly Guide to Python Programming

(vip2019) #1
Game Programming: Coding for Fun 247

F1 key (pygame.K_F1). The code that follows this second if statement
will be our play again or new game code.

noTe You can get a full list of the Pygame keycodes, such as K_F1, at
http://www.pygame.org/docs/ref/key.html.


“Play again” means that we want to start over from the begin-
ning. For Smiley Pong, we started with 0 points, 5 lives, and the
ball coming at us at 5 pixels per frame from the top-left corner of
the screen, (0, 0). If we reset these variables, we should get the new
game effect:

points = 0
lives = 5
picx = 0
picy = 0
speedx = 5
speedy = 5

Add these lines to the if statement for the F1 key KEYDOWN
event, and you’ll be able to restart the game anytime. If you’d like
to allow restarting only when the game is already over, you can
include an additional condition that lives == 0, but we’ll leave the
if statements as they currently are in our version 2.0 so that the
user can restart anytime.

Faster and Faster


Our game lacks one final element
of game design: it doesn’t get more
challenging the longer it’s played,
so someone could play almost for-
ever, paying less and less attention.
Let’s add difficulty as the game
progresses to engage the player and
make the game more arcade-like.
We want to increase the speed
of the ball slightly as the game advances, but not too much, or the
player might get frustrated. We want to make the game just a bit
faster after each bounce. The natural place to do this is within the
code that checks for bounces. Increasing the speed means making
speedx and speedy greater so that the ball moves farther in each
direction each frame. Try changing our if statements for collision
Free download pdf