Game Programming: Coding for Fun 255
Adding 1 to count_smileys every time a new smiley is added
to the sprite_list will help us keep track of the total number of
smileys drawn.
We’ll add similar logic to the if statement that plays our
popping sound whenever one or more smileys have been clicked,
but we won’t just add 1 to count_popped—we’ll add the real num-
ber of smileys that were clicked. Remember that our user could
have clicked the screen over two or more smiley sprites that are
overlapping the same point. In our event handler for the right-
click event, we gathered all these colliding smileys as the list
clicked_smileys. To find out how many points to add to count_popped,
we just use the len() function again to get the correct number of
smileys the user popped with this right-click. Add this line to the
if statement you wrote for the popping sound:
if len(clicked_smileys) > 0:
pop.play()
count_popped += len(clicked_smileys)
By adding len(clicked_smileys) to count_popped, we’ll always have
the correct number of popped smileys at any point in time. Now,
we just have to add the code to our game loop that will display the
number of smileys created, the number popped, and the percentage
popped to measure the user’s progress.
Just like in our Smiley Pong display, we’ll create a string of
text to draw on the screen, and we’ll show the numbers as strings
with the str() function. Add these lines to your game loop right
before pygame.display.update():
draw_string = "Bubbles created: " + str(count_smileys)
draw_string += " - Bubbles popped: " + str(count_popped)