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

14. Golf part 2: On the green

Mark Cunningham1 
(1)
Edinburgh, Scotland
 

‘And that’s a delightful shot from Johnson there, Jim. Both players have found the green in two.’

‘It sure was, Corey. Johnson is sitting about 18 feet from the hole with Miller maybe only 10 feet away, but with a trickier downhill putt. You can cut the tension here with a knife…’

In the last chapter, we completed the setup and displayed the flag in a random position. In this chapter, we will complete the following steps:
  1. 5.

    Power meter.

     
  2. 6.

    Move the ball.

     

Step 5: Power meter

The power meter is used to determine the strength of the player’s shot. The easiest solution would have been to have the player type in a number between 1 and 30. However, we will use a power meter to make hitting a shot that bit more difficult, and the game will look much better too.

Let’s begin by updating the slider on the power meter. The code in Figure 14-1 shows how this is done.
../images/482396_1_En_14_Chapter/482396_1_En_14_Fig1_HTML.jpg
Figure 14-1

Golf code listing 6

The variable slider_timer is a timer which handles the delay as the slider moves from one bar on the meter to the next. Line 112 subtracts one from the timer each frame. If the timer reaches 0 (line 114), we do two things:
  1. 1.

    Move the slider up or down.

     
  2. 2.

    Start a new timer.

     

Let’s look at how we move the slider first of all.

Line 117 tests the slider_direction to see if it equals 'up'.

If the slider is moving up
  • Line 118 increases the shot_power by 1.

  • Line 119 checks to see if the shot_power has reached the MAX_POWER (set to 30 at the start of the program). If shot_power equals MAX_POWER, then we set slider_direction to 'down' so that the slider starts moving downward.

If the slider is not moving up, then line 123 tests slider_direction to see if it equals 'down'.

If the slider is moving down
  • Line 124 decreases the shot_power by 1.

  • Line 125 checks to see if the shot_power has reached the MIN_POWER (set to 1 at the start of the program). If shot_power equals MIN_POWER, then we change the direction of the slider to 'up'.

Irrespective of whether the slider is moving up or down, if slider_timer has reached 0, then we need a new timer. The duration of this new timer is dependent on the current value of shot_power . Remember, when the slider is at the bottom of the power meter, we want it to take a little longer moving from one bar to the next. Lines 129–132 show how we achieve this:
  • Line 129 tests to see if shot_power <= SLOW_PUTT_RANGE, which is a constant used to determine when the slider should move more slowly. We can see from line 36 that SLOW_PUTT_RANGE is 3. Therefore, if shot_power equals 1, 2, or 3, then the new slider_timer will be set to SLOW_SLIDER_SPEED (which is 20).

  • However, we can see that if the shot_power is anything above 3, the new slider_timer will be set to SLIDER_SPEED (which is 5). This means that the speed of the slider on bars 4–30 of the power meter will be four times as fast as the speed of the slider on bars 1–3.

You can change the values of SLIDER_SPEED and SLOW_SLIDER_SPEED to make the game easier or more difficult.

Of course, all of this is going on in the background. We won’t see anything happen until we display the power meter and slider. The code in Figure 14-2 shows how to do this.
../images/482396_1_En_14_Chapter/482396_1_En_14_Fig2_HTML.jpg
Figure 14-2

Golf code listing 7

First up, we only want to display the power meter if the ball has not been hit and the ball is not in the hole. Line 138 checks these conditions and if they are both met:
  • Line 139 displays the power_meter.

  • Lines 141 and 142 do some basic math to work out the y coordinate of the slider depending on the value of shot_power.

  • Line 143 blits the slider image onto the screen.

Run the program, and you should see the slider moving up and down the power meter, and it will move more slowly when it reaches the bottom.

Step 6: Move the ball

The player must judge how much power their golf shot requires. If they hit their shot while the slider is near the bottom of the power meter, the ball will go a very short distance. Hitting a shot when the slider is near the top of the power meter means the ball will go much further. The player hits the ball by tapping the spacebar.

Let’s write the code to handle the spacebar event as shown in Figure 14-3.
../images/482396_1_En_14_Chapter/482396_1_En_14_Fig3_HTML.jpg
Figure 14-3

Golf code listing 8

We are familiar with the code to test if a key has been pressed. We want to hit the ball if the following three conditions have happened:
  • The spacebar has been tapped.

  • The ball_distance is 0 (this means the ball is not already moving).

  • in_the_hole is False (the ball is not already in the hole).

If all three of the conditions are true, we write some code to prepare moving the ball. This code between lines 110 and 120 can be explained as follows:
  • Line 110 sets slider_direction to 'none' to stop the slider moving up or down.

  • Line 111 calculates the total number of pixels that the ball will move by multiplying the shot_power (the value of the slider on the power meter) by the moves_per_flag (the number of pixels between each flag position). The result is stored in the variable ball_distance which we will use when actually moving the ball.

  • Line 113 adds one onto the number of strokes in the hole. We will look at this line in a bit more depth shortly.

  • Line 115 tests to see if the ball is moving right and then calculates the final location of the ball by adding the shot power onto the current location of the ball.

  • The else statement at line 117 means that the ball_direction is not 'right', and so it must be 'left'. In this case, the final ball location is calculated by subtracting the shot power from the current ball location.

  • Lastly, line 120 plays a sound effect of the ball hitting the golf club.

More about lists

We looked briefly at lists when we wrote Forest Bomber, but it’s time to consider them in a bit more detail.

A list in Python is a container which can hold multiple elements or objects. To create a list, we write the elements between square brackets, separated by commas.

In Golf, we use the list hole_strokes to store the number of strokes taken at each hole. We initialize the hole_strokes list like this:
hole_strokes = [0, 0, 0]

So the hole_strokes list has three elements.

Each item in a list can be identified by its index, but it should be noted that the index always starts at 0 and not 1. So the three elements in the hole_strokes list have indexes 0, 1, and 2, respectively.

Should we wish to access a list element, we place its index in square brackets.

So to access the first element in our hole_strokes list, we use
hole_strokes[0]
To access the third and last element in our hole_strokes list, we use
hole_strokes[2]
Let’s look again at the code which we used to update the number of strokes for the hole when the ball was hit. It’s at line 113:
hole_strokes[hole - 1] += 1

Assuming we are on the first hole, the hole will equal 1. We want to add 1 to the value of the first element of our list. But remember the index always starts at 0, so actually we want to add one to hole_strokes[0]. Using hole – 1 as the index means we will reference index 0 for hole 1, index 1 for hole 2, and index 2 for hole 3.

After the first shot has been hit, we add 1 onto hole_strokes[0], and so the list will become

[1, 0, 0]

Key learning

A list is a variable which can hold multiple elements.

We reference individual list items by using their index.

List indexes start at 0.

Update ball location

All we have done so far is get the ball ready to move. Now we need to move it across the screen by the distance determined by the power meter. The actual distance to be moved has already been calculated and stored in the variable ball_distance. Enter code listing 9 (Figure 14-4).
../images/482396_1_En_14_Chapter/482396_1_En_14_Fig4_HTML.jpg
Figure 14-4

Golf code listing 9

As you can see, there is quite a bit of code in Figure 14-4, so let’s take a closer look at what it does.

Line 150 checks to see if ball_distance > 0. The variable ball_distance is greater than zero when the ball is moving. It will become zero when it reaches its final destination and therefore stop moving.

Lines 152–155 update the ball’s x coordinate (stored in ball_x). If the ball is moving to the right, we add to the x coordinate, and if the ball is moving to the left, we subtract from the x coordinate. The amount the ball moves each time is held in the constant BALL_STEP. BALL_STEP was initialized to 3 at the start of the program, so the ball moves 3 pixels on each frame.

There is an if statement at line 158 which tests the ball's x coordinate to see
  • If it is greater than SCREEN_WIDTH: Which means the ball has rolled off the right-hand edge of the screen.

  • If it is less than 0: Which means the ball has rolled of the left-hand edge of the screen.

The code at lines 161–169 will handle the ball rolling off the edge of the screen:
  • Line 161 places the ball back at the tee by setting ball_x to START_BALL_X.

  • Line 162 resets ball_distance to 0, because the ball should no longer be moving.

  • Line 163 sets the ball_direction to go right.

  • Line 164 resets the shot_power on the power meter to 1.

  • Lines 167–169 reset the power meter values so that the slider starts back at the bottom.

If however the ball has not rolled off the edge of the screen, then we need to update its location each frame. The code between lines 172–195 will do this:
  • Line 173 subtracts 1 from the ball_distance, because the ball has moved 1 place closer to the hole.

  • Line 176 checks to see if ball_distance has become 0; in other words, the ball has reached its destination.

If the ball has reached its destination, we check to see if it has ended up in the hole. This will happen if the final_ball_location is equal to the flag_distance . If the ball is in the hole, we
  • Set the Boolean in_the_hole to True.

  • Add the number of strokes for the hole to the overall round_strokes.

  • Play the sound of the crowd clapping.

If the ball is not in the hole, we
  • Update the direction so that if the ball is to the left of the flag, the direction will become 'right', or if the ball is to the right of the flag, the direction will become 'left'.

  • Reset the slider so that it starts at the bottom of the power meter.

Draw the ball

For us to see all of this code in action, we need to actually draw the ball. Enter the code from Figure 14-5.
../images/482396_1_En_14_Chapter/482396_1_En_14_Fig5_HTML.jpg
Figure 14-5

Golf code listing 10

The code in Figure 14-5 is fairly straightforward. The only aspect that is worth further note is at line 220 where we add BALL_DESCENT to the ball’s y coordinate (BALL_Y) if the ball has landed in the hole. By moving the ball down the screen a few pixels, it will give the effect of the ball dropping into the hole.

Run the game and see if you can get the ball in the hole.

In the next and final chapter of the book, we will pull the remaining parts of the game together so that our Golf game runs for all three holes.

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

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