May 23rd, 2008
The core of every game has a game loop. Ours is no different. The loop will be a while loop:
run_game = True
while run_game:
for x in range(5):
for y in range(5):
if grid[y][x] == 'x':
screen.fill(red, (x*25, y*25, 25, 25))
pygame.draw.rect(screen, white, (x*25, y*25, 25, 25), 1)
pygame.display.flip()
Notice that we’ve taken the two for loops and put them inside the while loop. Also, we’ve put the flip command at the very end. As you probably already know, the two for loops and the flip command both combine to draw our entire screen. Now that we’ve put all the drawing code under the while loop, we are telling the computer that we want to draw the screen every time run_game == True, which, in this case, ends up being forever.
Not really forever, though. You can still force quit the program, which you will have to do for this particular bit of code. But let’s say we want to let the user quit more gracefully by hitting the 'q' button. Let’s start adding some code to do that.
First we need to add this to the very beginning of the file:
# Initialize the libraries import pygame, time from pygame.locals import *
You don’t need to know exactly what this does. Just know that it will give us all the different types of user input.
Next we want to start seeing what the user is actually doing. So adding this line at the end of the while loop.
while True: for x in range(5): ... ... events = pygame.event.get(KEYDOWN) print events
These two lines will give us the current user’s input for each run of the loop. Run this code and watch how fast your screen will fill up. Type some letters on the keyboard to get an idea how many times this loop is actually running each second.
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[<Event(2-KeyDown {'key': 115, 'unicode': u's', 'mod': 4096})>]
[]
[]
[]
[]
So you’ll see that most of the time we didn’t get any input from the user, except for that one time I pressed the 's' key. You don’t need to worry about 'key' or 'mod' for the moment, but they might prove useful later.
Because we got all those empty lists, we should write some code to filter them out. That will help us make sure we have an actual event to look at at.
events = pygame.event.get(KEYDOWN) # Only print out the events if we have more than zero if len(events) > 0: print events
The len function is used to tell us the length of the event list. So this filter will give us lists that have more than zero items. Or, in other words, all lists that actually have contents.
Now try running the game again. You’ll see that the screen doesn’t get flooded nearly as fast:
[<Event(2-KeyDown {'key': 115, 'unicode': u's', 'mod': 4096})>]
[<Event(2-KeyDown {'key': 101, 'unicode': u'e', 'mod': 4096})>]
[<Event(2-KeyDown {'key': 103, 'unicode': u'g', 'mod': 4096})>]
[<Event(2-KeyDown {'key': 109, 'unicode': u'm', 'mod': 4096})>]
[<Event(2-KeyDown {'key': 118, 'unicode': u'v', 'mod': 4096})>]
[<Event(2-KeyDown {'key': 111, 'unicode': u'o', 'mod': 4096})>]
[<Event(2-KeyDown {'key': 119, 'unicode': u'w', 'mod': 4096})>]
[<Event(2-KeyDown {'key': 103, 'unicode': u'g', 'mod': 4096})>]
[<Event(2-KeyDown {'key': 104, 'unicode': u'h', 'mod': 4096})>]
So, as I promised, we will watch all these events and check for the letter 'q', and then quit. Here’s the code:
events = pygame.event.get(KEYDOWN)
if len(events) > 0:
if events[0].unicode == u'q':
run_game = False
Basically the if statement says that we want the unicode of first event. And if that unicode is a 'q', then we quit. Pygame gives us the unicode of any key we press. You can use unicodes of anything sequence of letters in Python by putting a 'u' in the front of it:
# One letter u'a' # Or a word u'foobar' # Or a phrase u'This is unicode'
We’ll put the user input to better use in the next tutorial.