Email questions to the author: email@example.com
Remember to pass a tuple of two integers to
, not just two integers themselves. The
correct way to call the function is like this:
A function call like
will cause an error that
looks like this:
TypeError: argument 1 must be 2-item sequence, not int
object (we will just call them Surface objects for short) returned is
stored in a variable named
6. pygame.display.set_caption('Hello World!')
Line 6 sets the caption text that will appear at the top of the window by calling the
function. The string value
passed in this function call to make that text appear as the caption:
Game Loops and Game States
7. while True: # main game loop
8. for event in pygame.event.get():
Line 7 is a
loop that has a condition of simply the value
. This means that it never
exits due to its condition evaluating to
. The only way the program execution will ever
exit the loop is if a
statement is executed (which moves execution to the first line after the
(which terminates the program). If a loop like this was inside a function,
statement will also move execution out of the loop (as well as the function too).
The games in this book all have these
loops in them along with a comment calling
e ―main game loop‖.
A game loop (also called a main loop) is a loop where the code does
1. Handles events.
2. Updates the game state.
3. Draws the game state to the screen.
The game state is simply a way of referring to a set of values for all the variables in a game
program. In many games, the game state includes the values in the variables that tracks the
player’s health and position,
the health and position of any enemies, which marks have been
made on a board, the score, or whose turn it is. Whenever something happens like the player