PyGame Guide/2D Game Development Concepts

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Birds-eye-view: How game programs work[edit | edit source]

If you’ve taken a programming class before, you might already understand that a program runs from the top → down, line-by-line, until it hits the end of the source code file. While it’s stepping through code, it will pause when waiting for the user’s input, then resume once it has it. Once the end of the file is hit, the program automatically quits, unless you’ve added some kind of loop in there to keep it running.

If we don’t want a program to quit automatically, we will usually put a program loop somewhere within, where the same kinds of tasks are done every cycle... This could be displaying a menu, having the user select where to go, and doing some operation, and then returning back to the menu. Similarly, a game isn’t very playable if it exits immediately once you launch it. A game also has a game loop where some common events are done every cycle of the game.

A program loop; The menu has three options: (1) Add, (2) Subtract, and (3) Exit. If the user selects (1), it goes to the Add function, then returns to the menu. If the user selects (2), it goes to the Subtract function, then returns to the menu. If the user selects (3), it leaves the loop and exits the program.

During a game, the set of code it does first - before the game loop begins - is setup or initialization code. This is where variables are initialized, assets are loaded into memory, and things get prepared for the game. Once it enters the game loop, the game will always check for player input (Did the player press the “right” arrow key? Did the player click a button?), update the game objects (Move all enemies towards the player by 5 pixels), and draw to the screen (Draw the background, the world, the player, and the enemies). We will write code for each of these types of events, but PyGame will help us out.

Of course, we can also write functions to handle specific tasks, but once a function is done executing, program flow continues at the line after the function call was made.

The coordinate plane[edit | edit source]

In computer graphics, (x, y) coordinates are used to place images and text on the screen. You might remember coordinate planes from algebra. While the same idea applies here, the coordinate plane is laid out a bit differently.

In computer graphics, the origin of the coordinate plane is commonly found at (0, 0). As you move to the right, the x value increases, and as you move down, the y coordinate increases.

A program loop for a game; First, there is the SETUP section. After SETUP, we enter the game loop, which contains: “handle player input”, “update game objects”, and “draw to the screen”. If the user quits, then we will leave the loop, clean up the game, and exit the program.

Variables[edit | edit source]

An illustration of sprite placement on a computer screen. The origin (0, 0) is at the top-left corner.

Variables are locations in memory where we can store data . This data can be as simple as numbers, or a string of text, or it can be more complicated, such as a coin-object or a non-player-character-object. As an example, each of our game characters will have an (x, y) coordinate so that the game knows where to draw the character to on the screen. A character might also have a score variable, or a name variable, or many other things.

It is better to use variables rather than hard-coding values throughout your entire program. It is easier to go back and make fixes or changes if the data is stored in variables - What if your character’s starting position in the level was wrong and you had to go back and update those coordinates in every level of the game? There are also rules you have to follow when naming your variables. You cannot have spaces in a variable name, you cannot start a variable name with a number (though it may contain numbers), and it cannot be a keyword (like if).

Functions[edit | edit source]

Functions are collections of instructions that can be run. A function has a name (like “AddToScore”), which can be used over and over. This way, you don’t have to write the same code every time you want to do the task: you just call the function!

Functions can have inputs and outputs. Inputs are usually called parameters and outputs are called the return values.

For example, if we had an AddToScore function, its input might be “How much do we add to the score?” and its output could be “What is the score after we add this amount?”

In code form, it would look something like this in Python:

def AddToScore( score, amount ):  # score and amount are the inputs
  score = score + amount          # adding onto the score variable
  return score                    # return the result of the math

Functions don’t always need to have inputs or outputs, it depends on what the function’s task is.

Classes[edit | edit source]

Classes are a way that we, the programmers, can create our own variable data-types. By default, we can use variables like integers (whole numbers) and strings (text in double-quotes), but if we want to make a game character that has multiple attributes, such as name, coordinates, level, etc., we can write a class to do this.

A class can contain member functions and variables, so that we can have functionality and attributes associated with our object.

Bunny object

Cool bunny sprite


  • score (integer)
  • xCoordinate (integer)
  • yCoordinate (integer)
  • name (string)


  • Move( direction )
  • AddToScore( amount )
  • DrawToScreen( window )

The great thing about classes is that, once you’ve defined one class, you can create as many variables of that class type as you want. In other words, once you implement a Player class, you could reuse the same code to make your player1, player2, player3, and player4!