Section 1 – imports, globals, and drawings

In this first section, we will write all of the code to set up the different parts of our game. This includes importing libraries, defining all of our global variables, and telling the computer how to draw the screen, ball, and paddles.

Importing libraries

The first lines of code we write will be used to import the necessary libraries into the game, including pygame. We will be using three libraries in the game: pygame, math, and random. pygame, as we discussed in the previous chapter, allows us to have visual elements in our game. The random library, included with Python, gives us the ability to select and use random numbers in our game. The math library, also included with Python, allows for mathematics with floating point numbers. To use these modules and libraries in your code, type the following lines into your tiny.py file underneath the #imports, globals, and drawing comment:

import pygame
import random
import time

Tip

Make sure to save your tiny.py file now that you have added some new lines. Be in the habit of saving your code as OFTEN as you can.

You can make comments in your code if it helps you to organize your thoughts. Now, we will also initialize pygame so that we are able to use all of the capabilities, including starting our screen, drawing graphics, and running our game loop. To initialize pygame, we use the init() function. To initialize it, type these two lines of code below your imports:

  # initialize pygame
  pygame.init()

Using pygame.init() starts the pygame process, and the pygame process will keep running until the program stops running when the player quits pygame. This allows us to access everything inside of pygame throughout the game. You will see how important this is as we continue to write our game. Right now, save your tiny.py file again:

Importing libraries

Introducing globals

Now that we have imported the libraries that we need, we will be making globals for some parts of the game. As a reminder, globals, or global variables, are variables that we can use throughout the entire file. We will set global variables for all of the colors that we wish to use. We also set global variables for the screen, paddles, and ball.

Defining a color

First, we will make globals for each color. Colors, as we learned in Chapter 8, pygame, are represented by three different numbers listed in parentheses, also called a tuple. Instead of having to write these numbers repeatedly, we will make a global variable for each color so that we can use the names of all of the colors throughout the game.

Depending on what colors you like, you might want to make global variables for all the colors or only for a few. It is really up to you to decide what colors to add to your code. Here is a list of common colors that you may wish to use in your game. You should add the code for each color exactly as it appears here:

  red = (255, 0, 0)
  orange = (255, 127, 0)
  yellow = (255, 255, 0)
  green = (0, 255, 0)
  blue = (0, 0, 255)
  violet = (127, 0, 255)
  brown = (102, 51, 0)
  black = (0, 0, 0)
  white = (255, 255, 255)

The preceding list shows the basic colors that you can include in your game code. If you want to include more advanced colors, you can search for rgb color codes chart in an Internet search engine, such as Google, and you will find that there are different variations for each color that you can change to your liking, such as light blue or dark blue. Once you have changed all the colors to your liking, make sure that you save your work:

Defining a color

Adjusting the screen size

We will also use globals to define the parts of our screen display. This lets us show the size, color, and text for the main screen. Here are the color globals; we will add these lines of code for the width and height of the screen:

  # screen globals
  screen_width = 600
  screen_height = 400

Now that we have made the screen_width and screen_height variables, we can use these variables throughout our code, which makes our code easier to read. Also, if we do decide to change the screen width or screen height, we can change it one at a time in this global variable, and all of our code will still run properly.

Drawing the screen

So, the screen_width and screen_height variables are the basic information that pygame needs so that it can set up the actual game screen. pygame has a function called pygame.display.set_mode() that takes the variables of screen_width and screen_height to make the screen display. Now, writing pygame.display.set_mode ((screen_width, screen_height)) is really long, especially if we keep doing it. Instead, we are going to set this to a global variable called game_screen:

  game_screen = pygame.display.set_mode((screen_width, screen_height))

Creating screen labels

The next set of functions that we use will set the text for the top of the screen and the font for the game screen. The first line of code defines what string of text we want to see, and in the following line, we define the font and size. If the font and the size are not available, the font will, by default, use whatever is originally set on your system. This is true for Windows, Mac, and Linux systems:

  pygame.display.set_caption("Tiny Tennis")
  font = pygame.font.SysFont("monospace", 75)

So, we have now set all of the basic variables needed to create a game screen. Save your work and, when you are ready, move on to making the global variables that we will need for the ball, paddles, and scoring. Your screen code should look like this code sample:

Creating screen labels

Ball – the starting location

In Tiny Tennis, the ball is one of the most important parts of the game. We have a lot to do to make it work. First, we need to give the ball some global characteristics so that it can be drawn and redrawn to create the illusion of movement.

First, we need to set the x, y coordinates of the ball. By making a global variable for this, we can tell the computer where to redraw the ball without having to write special code for each movement of the ball. We will set the default value of x and y so that the ball starts in the center of the screen. Write the next lines into your tiny.py file:

  # ball globals
  ball_x = int(screen_width / 2)
  ball_y = int(screen_height / 2)

Ball – setting the speed and direction

Now that we have told the ball to start in the center of the screen as default, we need to tell the ball how far to move by giving it x and y coordinates for movement:

  ball_xv = 3
  ball_yv = 3

The ball_xv = 3 means that the ball will move 3 pixels along the x axis each time it is redrawn. The ball_yv = 3 means that the ball will move 3 pixels along the y axis each time the screen redraws. This is great as it will help us keep the ball going in the speed and direction that we like. Here, v = velocity which is the magnitude (speed) and direction (x,y) of the ball. So, when we say ball_xv = 3, we are really saying that the ball moves along the x axis at a speed of 3 pixels each time the screen is redrawn.

Ball – setting the size

The final thing that we will define about the ball is its radius. The radius is half of the total width of the ball, as represented in pixels. By setting the radius, we set the size. Write the following line of code into your tiny.py file to represent the ball radius:

  ball_r = 20

Now that we have defined the characteristics of the ball, make sure to save the file. Nobody wants to rewrite lines of working code! Take a look at an example of this section of code:

Ball – setting the size

Paddles – starting location and size

In our game, we will have two paddles. Recall that in the beginning of this chapter, it was said that there is more than one way to do some of the things that we are doing. There are more advanced ways to make the paddles, but it is important that you understand each part of the paddle, so we are going to break our code down very simply. Later, once you have completed this game, you can do some research on creating objects and try creating paddles as objects.

We will give our paddle four qualities: a starting location on the x axis, a starting location on the y axis, a width, and a height. Each of these numbers is a representation in pixels. Below the ball globals, on line 34, add the next five lines to your tiny.py file:

  # draw paddle 1
  paddle1_x = 10
  paddle1_y = 10
  paddle1_w = 25
  paddle1_h = 100

You probably noticed that the code we wrote is for paddle1. There are two paddles required for Tiny Tennis. We want to give each player a fair start, so we will create paddle2 so that it is equal in size, but it's located opposite paddle1. To make the second paddle, start on line 40 and write the next five lines of code:

  # draw paddle 2
  paddle2_x = screen_width - 35
  paddle2_y = 10
  paddle2_w = 25
  paddle2_h = 100

You will notice that the x coordinate for paddle 2 combines the screen_width variable, which is the maximum x coordinate number (600) and then subtracts the width of the paddle (25) + the x coordinate value of paddle 1 (10) as well. This math allows us to make sure that the paddle is the same distance from the right-hand side of the screen as it is from the left-hand side of the screen. If you are confused, copy the code into your file and save it. You can play with the numbers and see how your paddles change based on each value:

Paddles – starting location and size
..................Content has been hidden....................

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