Section 2 – moving the paddles

Now we finally get to write the code that will make our paddles appear on the screen and allow us to control the paddle.

This is where we get the chance to use the logic and loops that we learned about in earlier chapters. In a game such as Tiny Tennis, many decisions are made very quickly. Computers are great at making fast decisions based on our instructions. Here are the parts of the code that will be in the next section:

  • Creating the while loop
  • Key events

We will code these next pieces step by step, and then test the code by running it to check whether there are any errors. It is suggested that you read through this whole section before you start coding so that you know what to expect. Once you have read through everything, the fun starts!

Pre-loop actions

Before we actually create the while loop, there are two actions that we will code. The first is to ensure that the cursor disappears when it goes over the game screen, so it is not an interruption. There is a special function for this behavior in pygame:

  pygame.mouse.set_visible(0)

By setting the visibility to 0, we make the mouse/cursor invisible to the game. Since we do not need the mouse in the game, it is okay for us to do this.

The second action is to set the global variable for our while loop. We are going to call our main game loop variable do_main. We will set do_main = True:

  do_main = True

Tip

Remember that syntax and case (uppercase or lowercase) are important. Notice the CAPITAL letter T, and make sure to copy it exactly as it is. Remember, True is a Boolean that needs to be written with a capital T. Now, we are ready to write our while loop.

Creating the while loop

Our game loop will be a while loop. We will use do_main as our True statement. So, you will have another line of code that looks like this:

  while do_main:

Make sure you place a colon (:) at the end of the line. Also, all of the other lines of code in the game loop will be indented at least once because they all need to be INSIDE of the loop to run. Here is a screenshot of the while loop:

Creating the while loop

Moving the paddles – keyboard events

The first set of events in the while loop are keyboard events. These events take place when a key or set of keys get pressed. The events use the if/elif logic. All of them are indented on at least one tab, and some are indented on two tabs or more. Remember that indents are an organizational tool in Python and help us keep track of when certain code should be run.

Notice the code on line 54 of the screenshot. In line 54, we will create the pressed variable, which we will set equal to the pygame.key.get_pressed() function. This will give us a shorter reference to the function. Type this code in line 54:

    pressed = pygame.key.get_pressed()

In line 55, we use the pygame.key.set_repeat() function. This tells the computer that once a key is pressed, the action that the key performs should continue until the user lets the key go. Type the next line of code into line 55 of your tiny.py file:

    pygame.key.set_repeat()

Now that we have set the variable and characteristics for the keyboard events, we will create our first loop, a for loop that looks for the player to quit. Using a for loop, we will loop over each event that is found using the pygame.event.get() function. If the event is a QUIT event, then the while loop will automatically end. You will notice that we also use our if logic here so that we can tell the computer to make a decision if it finds the quit event. To make this for loop, you will write the following lines of code, starting on line 56 of your code file:

    for event in pygame.event.get():
      if event.type == pygame.QUIT:
        do_main = False

Now that we have told the computer how and when to end the while loop, we can tell the computer what to do when certain keys are pressed. For our Tiny Tennis game, we need to assign keys to exit the game as well as ones to control paddle 1 and paddle 2.

Note

Want to choose different keys than the ones we use in this book? You can find an entire list of how to use every keyboard key on the pygame website at http://www.pygame.org/docs/ref/key.html.

Exiting the game – escape key

To exit the game, we will use the Esc key. You will notice that we use our pressed variable followed by the key code for the Esc key. Starting from line 60, type these two lines of code:

    if pressed[pygame.K_ESCAPE]:
      do_main = False

The lines of code tell the computer that if the Esc key is pressed, then the do_main global variable should be set to False. When do_main is set to Boolean False, then the while loop stops. We will write the code that ends the game a bit later.

Paddle control – player 1

For player 1's paddle to go up, we will use the W key. For player 1's paddle to go down, we will use the S key. These are very typical keys to use for computer game controls. Notice which letters are uppercase and which are lowercase, and be sure to copy them exactly, starting from line 63:

    if pressed[pygame.K_w]:
      paddle1_y -= 5
    elif pressed[pygame.K_s]:
      paddle1_y += 5

Paddle control – player 2

Player 2 also needs to have keyboard controls that work to move his/her paddle up and down at the same time as player 1. This means that we must assign different keys for the second paddle. For this game, we are using the up arrow key to move paddle 2 up and the down arrow key to move paddle 2 down. Type the following lines of code into your tiny.py file, starting from line 68:

    if pressed[pygame.K_UP]:
      paddle2_y -= 5
    elif pressed[pygame.K_DOWN]:
      paddle2_y += 5

Tip

Save your work!

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

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