Introduction to Computer Science
Union College
Spring 2009

Bouncing balloons with functions

(a) Download bouncing_balloon_with_functions.py (you also need red_balloon.gif), run it, and read and understand the code. Most of the code should be familiar. The only difference to what you did last week is that this code breaks the game into functions.

(b) Add a second balloon, make it fly around at different speeds than the first, and make it bounce off the edges like the first. Use this image of a green ballon for this new balloon.

Pop the balloon

Next we want to play a popping sound when the player clicks with the mouse on the red balloon.

(a) To play sound, you first need to load a file with the sound you want to play. Pygame can deal with sounds in WAV format and sounds in OGG format. Here is a popping sound for you.

You load this sound in a similar way to how you load the balloon pictures. pop_balloon.py shows you how to load the sound. (See the documentation if you want to know more about playing sounds in pygame. Pygame's resources page has links to free sound collections.)

(b) Next, we need to check whether a mouse click happened with the mouse pointer over the red balloon. pop_balloon.py already has some code for checking whether a mouse click (at any position) occurred. It also has some code for retrieving the x and y coordinates of the mouse.

Write a function called clicked_image that takes the mouse coordinates and the coordinates as well as the width and height of a rectangular image (such as the balloon) as parameters. The function should return True if the mouse coordinates are inside the images's boundaries, and False otherwise.

(c) Finally, put code into the indicated space that checks whether the mouse click was over the red balloon and if so plays a popping sound. To play a sound you use the function <name>.play(), with name being the variable name referring to the sound. (So, in our case, the name would be pop_sound.)

Note: You can use the functions <img_name>.get_width() and <img_name>.get_height(), where <img_name> is the variable name referring to the image. (So, if you use my code, that would be the name balloon for the red balloon.)

Score keeping

(a) Introduce a variable for score keeping. Every time the red balloon is clicked, the score should go up by 1.

To check whether your score variable is behaving in the right way print it out (using print) every time it changes.

Optional: Print the score to the game window

Now, let's write the score into the game window.

(a) You need to first load a font (similar to loading the balloon images or the sound file). This can be done using the following lines of code in our initialization function:

global myFont
myFont = pygame.font.Font(None,30)

(b) To write something to the screen, an image with the desired text is created. This image is then printed to the screen in the same way other images (like e.g. the image of the balloon) are printed to the screen. Here is an example of how to create a text image:

text = myfont.render("Text text", True, pygame.color.Color("magenta"))
The first argument is the text to be written, the second argument determines whether or not antialiasing should be used (if True, the letters will look smoother), and the third argument specifies the color.

(c) Finally, we need to clean up the font variable (for a reason that is not entirely clear to me). So, our done function needs the line

global myfont
myfont = None

Optional

Here are some more ideas (of differing difficulty) for how you might extend this game if you finish the other tasks quickly:

  • Have the score go down for each mouse click that occurs outside of the red balloon.
  • Have a time count down and see how often you can pop the balloon within that time limit.
  • Have the balloon fly faster every time you successfully clicked it or every time you did not successfully click it or every few seconds.
  • Have the player loose the game if he clicks the wrong balloon.
  • After clicked, move the red balloon to a random position on the screen.

Submit on Blackboard

by Kristina Striegnitz