© Mark Cunningham 2020
M. CunninghamGame Programming with Code Angelhttps://doi.org/10.1007/978-1-4842-5305-2_8

8. Snapper part 3: Snapped

Mark Cunningham1 
(1)
Edinburgh, Scotland
 

Charlotte lay perfectly still, her breathing almost inaudible. All that she could hear was the sound of her own heart thumping in her chest. Her eyes scanned between the trees. Over there. Was that something moving, or was it her imagination? A cracking twig broke the silence.

Charlotte’s gaze focused. There was something. A mere silhouette, she could see a head maybe, but what was it? Too big for a rabbit. She could almost make it out now, copper-brown fur the color of scorched earth, a lighter tail only just visible.

A deer stood less than 30 meters in front of Charlotte, staring right back at her.

With the most subtle of movements, she picked up her camera and zoomed in.

So far, our game has animals appearing and then hiding, and we can control the location of the camera by moving the mouse. In order for Snapper to become a game, we need to be able to take photographs.

The final stages of the game are
  1. 7.

    Taking a photograph

     
  2. 8.

    Game over

     
  3. 9.

    Scoreboard

     

Step 7: Take a photograph

The player will take a photo by clicking the mouse button. Pygame has a MOUSEBUTTONDOWN event which registers when the mouse button is pressed.
../images/482396_1_En_8_Chapter/482396_1_En_8_Fig1_HTML.jpg
Figure 8-1

Snapper code listing 18

Add the code in Figure 8-1 at line 114. With this code, we check to see if the mouse button is clicked and if so set the variable mouse_button_pressed to True. Otherwise, it will remain False.

To find out what will be in the photo, we need to work out what can be seen through the camera’s viewfinder. The viewfinder is a rectangle, the dimensions of which we can calculate.
../images/482396_1_En_8_Chapter/482396_1_En_8_Fig2_HTML.jpg
Figure 8-2

Snapper code listing 19

Add the code from Figure 8-2 at line 145 to calculate the viewfinder rectangle.

Rectangles collide

Next, we need to write some code to take a photograph when the mouse button is pressed. We will build this code block up in smaller parts.
../images/482396_1_En_8_Chapter/482396_1_En_8_Fig3_HTML.jpg
Figure 8-3

Snapper code listing 20

Insert the code shown in Figure 8-3 at line 184. There are quite a lot of if statements here, so let’s see what each one does:
  • The if statement at line 178 is relatively straightforward – it is testing to see if a photo has been taken. A photo will have been taken if mouse_button_pressed has been set to True. Remember we set mouse_button_pressed to True when Pygame detects a MOUSEBUTTONDOWN event.

  • The if statement at line 179 checks three variables. The first two, snap_visible and miss_visible, will be covered later. The third check is to find out whether lives > 0. When the game is over, lives will be 0, so here we are checking that the game is not over.

  • The if statement on line 189 uses a really important Pygame function called colliderect. colliderect is important when writing games because it takes two rectangles and checks if they have collided with each other. We can use colliderect in games to test if a missile has hit a spaceship, if a racing car has driven over a patch of oil, or, in the case of Snapper, if the animal is in the camera viewfinder.

    colliderect takes the format rectangle_1.collide_rect(rectangle_2).

    It will return True if the rectangles have collided and False if they have not collided.

Figure 8-4 illustrates how colliderect works.
  • The if statement on line 190 tests to see if the animal is visible. If the player takes a photo when there is no animal, then it will count as a failed photo.

Snapped it!

So the player has taken a photo if all of the following are true:
  • The mouse button is pressed.

  • It is not game over.

  • The animal is in the camera’s viewfinder.

  • The animal is actually visible.

The code between lines 191 and 193 is run when these events happen:
  • Line 191 calculates how much time was left on the timer, multiplies that by the points value for the animal, and adds the result onto the game score.

  • Line 192 sets snap_visible to True. When snap_visible is True, a green tick will display in the camera viewfinder, and we can’t take another photo.

  • Line 193 plays an audio file – the camera shutter effect.
    ../images/482396_1_En_8_Chapter/482396_1_En_8_Fig4_HTML.jpg
    Figure 8-4

    Illustration of the Pygame colliderect() function

Run the program and try to take a picture. If you are successful, you will hear the camera shutter sound effect being played, and the camera screen will go black. If you are unsuccessful, well, nothing happens, not yet anyway. Let’s fix that.

Oops, missed!

Enter the code shown in Figure 8-5. Take particular care to ensure the indentation is exactly as shown in the code listing.
../images/482396_1_En_8_Chapter/482396_1_En_8_Fig5_HTML.jpg
Figure 8-5

Snapper code listing 21

The first else statement shown at line 195 goes with the statement
if animal_visible is True:
So the else statement will be reached if animal_visible is False. If the player tries to take a photograph when the animal is not visible (in other words, the animal disappeared before they could take the photo), then lines 196–198 will be run:
  • Line 196 sets miss_visible to True. miss_visible is a Boolean variable very similar to snap_visible, but it will be used to show a red cross instead of a green tick.

  • Line 197 subtracts one from the number of lives that the player has.

  • Line 198 plays a buzzer sound effect to indicate that the photo was unsuccessful.

The second else statement at line 200 goes with the statement
if viewfinder_rect.colliderect(animal_rect):

This else statement will be reached if the viewfinder rectangle did not collide with the animal rectangle. In other words, the player missed the animal when they took their photograph. If this happens, lines 201–203 will be run, which as we can see do exactly the same thing as lines 196–198.

Whether the photo was successful or not, we run lines 206–208 to hide the animal, cancel the animal timer, and start a no animal timer.

Hit or miss?

Finally, we want to use the Boolean variables snap_visible and hit_visible in order to display a green tick or a red cross over the viewfinder to indicate whether or not the photograph was successful.
../images/482396_1_En_8_Chapter/482396_1_En_8_Fig6_HTML.jpg
Figure 8-6

Snapper code listing 22

Insert the Figure 8-6 code at line 237. Take particular care with lines 239–240. This is actually one line of code split over two lines. Key it in exactly as shown, and when you reach the comma at the end of line 239, hit return and continue typing line 240:
  • Lines 238–240 work out the dimensions of the rectangle which will hold the snap_image or the miss_image. The tick/cross image will be positioned in the middle of the camera viewfinder.

  • Lines 242–246 will display either the snap_image (green tick) if snap_visible is True or the miss_image (red cross) if miss_visible is True. Of course, if neither is True, then neither will be displayed.

Run the game again to see this code in action.

Key learning

Pygame has an event MOUSEBUTTONDOWN which is set when the mouse button is clicked.

The Pygame function colliderect is used to determine if two rectangles have collided.

Step 8: Game over

In Snapper, the game will be over if the player’s lives reach zero. At the moment, we have a variable lives which is initialized with three at the start of the game and is reduced by one each time the player takes a photo but misses the animal. However, there is nothing in the program code to explain what to do when all the lives have run out. Let’s deal with that next. Enter code listing 23 (Figure 8-7).
../images/482396_1_En_8_Chapter/482396_1_En_8_Fig7_HTML.jpg
Figure 8-7

Snapper code listing 23

Add the game over code at line 119. Let’s take a look at how it works:
  • Line 120 gets details of any key presses from the Pygame get_pressed() function and stores them in the variable key_pressed,

  • Line 121 checks for two things:
    • To see if the key pressed was RETURN

    • To see if the number of lives remaining equals zero (in other words, is it game over)

The remaining block of code will run if the game is over and the player has pressed RETURN:
  • Lines 122–123 check if the score has beaten the current high score and, if so, set the high score to the game score. We saw this code used in Forest Bomber.

  • Lines 125–126 reset the number of lives and the score.

  • Line 128 uses our get_random_animal function to get a new random animal.

  • Lines 129–130 set up the animal_timer and the no_animal_timer.

  • Finally, lines 132–135 ensure that the animal will be visible and the snap_image and miss_image are both hidden.

And that’s it. A new game is set up ready to go. Run the code to test it.

The only thing that’s missing is a scoreboard.

Step 9: Scoreboard

In Forest Bomber, we saw how to write code to display a scoreboard. We will use a similar process in Snapper, but this time the scoreboard will be displayed at the bottom of the screen.

In Snapper, we will break our scoreboard code down into two functions:
  • display_scoreboard_data to display the text on the scoreboard

  • display_game_over to display the game over message at the end of the game

../images/482396_1_En_8_Chapter/482396_1_En_8_Fig8_HTML.jpg
Figure 8-8

Snapper code listing 24

First, add the display_scoreboard_data function shown in Figure 8-8 at line 276.
../images/482396_1_En_8_Chapter/482396_1_En_8_Fig9_HTML.jpg
Figure 8-9

Snapper code listing 25

Next, add the display_game_over function at line 291 as shown in Figure 8-9. Note that there are two blank lines separating each function. While the number of blank lines does not affect the program, it makes the program that little easier to read.

We won’t look at the code held in these functions in any detail, other than to say that they use math to align and display any text and rectangular boxes on the screen.

It is worth noting that the display_scoreboard_data function takes in two parameters:
  • Some text which will be stored in the parameter scoreboard_text.

  • The alignment setting for the text, which will be stored in the parameter alignment. The alignment is a string which can be either ‘Left’ or ‘Centre’, and this function will align the text accordingly.

A final block of code is needed to make use of these functions and complete the Snapper game.

Add the code from Figure 8-10 at line 265.
../images/482396_1_En_8_Chapter/482396_1_En_8_Fig10_HTML.jpg
Figure 8-10

Snapper code listing 26

Notice that we are calling our display_scoreboard_data function at line 267 and again at line 270. We call the display_game_over function at line 279.

Well done! You have now programmed two games. Don’t spend too long playing Snapper though – I hear that planet earth is about be hit by an alien invasion…

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.16.184