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

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

if picx + picw / 2 >= paddlex and picx + picw / 2 <= paddlex + \
paddlew:
points += 1
speedy = -speedy

# Draw text on screen
draw_string = "Lives: " + str(lives) + " Points: " + str(points)

text = font.render(draw_string, True, WHITE)
text_rect = text.get_rect()
text_rect.centerx = screen.get_rect().centerx
text_rect.y = 10
screen.blit(text, text_rect)
pygame.display.update()
timer.tick(60)

pygame.quit() # Exit

Our gameplay is nearly complete: the ball bounces off the
paddle, points are awarded, and players lose a life if they miss the
ball and it hits the bottom edge of the screen. All the basic com-
ponents are there to make this an arcade-style game. Now think
about what improvements you would like to see, work out the logic,
and try adding code to version 1.0 to make your game even more
fun. In the next section, we’ll add three more features to create a
fully interactive, video game–like experience that we can share
with others.

a dding difficulty and ending the


Game: Smiley Pong, Version 2.0


Version 1.0 of our Smiley Pong game is playable. Players can score
points, lose lives, and see their progress on the screen. One thing
we don’t have yet is an end to the game. Another is the sense of
greater challenge as the game progresses. We’ll add the following
features to Smiley Pong, version 1.0, to create a more complete
game in version 2.0: a way to show that the game is over when
the last life is lost, a way to play again or start a new game with-
out closing the program, and a way to increase the difficulty as
the game goes on. We’ll add these three features one at a time,
winding up with a fun, challenging, arcade-style game! The final
version is shown on page 250.
Free download pdf