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

(vip2019) #1

254 Chapter 10


make these changes and hear the result, make sure you’ve down-
loaded the pop.wav sound file into the same folder as your revised
SmileyPop.py, turn your speakers to a reasonable volume, and
pop away!

Tracking and Displaying Player Progress


The next feature we want to add is some way to help the user feel
like they’re making progress. The sound effects added one fun kind
of feedback (the user hears a popping sound only if they actually
clicked a smiley sprite), but let’s also track how many bubbles the
user has created and popped and what percentage of the smileys
they’ve popped.
To build the logic for keeping track of the number of smileys
the user has created and the number they’ve popped, we’ll begin by
adding a font variable and two counter variables, count_smileys and
count_popped, to the setup section of our app:

font = pygame.font.SysFont("Arial", 24)
WHITE = (255,255,255)
count_smileys = 0
count_popped = 0

We set our font variable to the Arial font face, at a size of
24 points. We want to draw text on the screen in white letters, so
we add a color variable WHITE and set it to the RGB triplet for white,
(255,255,255). Our count_smileys and count_popped variables will store
the number of created and popped smileys, which both start at
zero when the app first loads.

Smileys Created and Popped
First, let’s count smileys as they’re added to the sprite_list. To do
that, we go almost to the bottom of our SmileyPop.py code, where
the if mousedown statement checks whether the mouse is being
dragged with the mouse button pressed and adds smileys to our
sprite_list. Add just the last line to that if statement:

if mousedown:
speedx = random.randint(-5, 5)
speedy = random.randint(-5, 5)
newSmiley = Smiley(pygame.mouse.get_pos(), speedx, speedy)
sprite_list.add(newSmiley)
count_smileys += 1
Free download pdf