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

(vip2019) #1

252 Chapter 10


You can continue to build on the game elements in this
example (see “Programming Challenges” on page 261), or you can
use these building blocks to develop something new. Most games,
and even other apps, have features like the ones you added in this
chapter, and we usually follow a process similar to the one we used
to build Smiley Pong. First, map out the skeleton of the game, and
then build a working prototype, or a version 1.0. Once that’s work-
ing, add features until you get the final version you want. You’ll
find iterative versioning—adding features one at a time to create
new versions—useful as you build more complex apps.

a dding more features: SmileyPop v2.0


We’ll follow our iterative versioning process one more time by add-
ing features that my son Max and I wanted to see in the SmileyPop
app in Chapter 9. First, he wanted a sound effect whenever a smiley
face bubble (or balloon) was popped by a mouse click. Second, we
both wanted some kind of feedback and display (maybe how many
bubbles had been created and how many had been popped), and I
wanted some sign of progress, like the percentage of bubbles we’d
popped. The SmileyPop app was already fun, but these elements
could make it even better.
Look back at SmileyPop.py on page 226; we’ll start with this
version of the app, and we’ll build our second version (v2.0, short
for version 2.0) by adding code. The final version, SmileyPop2.py,
is shown on page 257.
We’ll begin by adding Max’s request: the popping sound.

Adding Sound with Pygame


At http://www.pygame.org/docs/, you’ll find modules, classes, and
functions that can make your games more fun to play and easier to
program. The module we need for sound effects is pygame.mixer. To
use this mixer module to add sound to your game, you first need
a sound file to use. For our popping sound effect, download the
pop.wav file from http://www.nostarch.com/teachkids/ under the
source code and files for Chapter 10.
We’ll add these two lines to the setup section of SmileyPop.py,
right below sprite_list = pygame.sprite.Group():

pygame.mixer.init() # Add sounds
pop = pygame.mixer.Sound("pop.wav")
Free download pdf