May 24th, 2008
As I promised, the next tutorial will support better user input. So let’s go.
We’re going to add three more keys to check for in our events. The 6, 4, and 2. Here’s the code:
# Add this line just above the while loop:
current_location = (0, 0)
# And this section is the new events section:
if len(events) > 0:
...
if events[0].unicode == u'6':
current_location = (current_location[0]+1, current_location[1])
if events[0].unicode == u'2':
current_location = (current_location[0], current_location[1]+1)
if events[0].unicode == u'4':
current_location = (current_location[0]-1, current_location[1])
Let’s go through this line by line.
The first line is necessary because we’re going to be keeping track of the user’s current location. As you would expect, this is just the x and y coordinates, so it starts the user at the upper left corner:
current_location = (0, 0)
Next we’re adding some more if statements. One for each of the keys. Just as you would expect, we’re just checking for the unicode of the letter they press:
if events[0].unicode == u'6':
...
if events[0].unicode == u'4':
...
if events[0].unicode == u'4':
...
Then, for each of these statements, we want to perform a certain action. We want to change the current_location so that the rest of the program knows what the new location is, and can act accordingly:
if events[0].unicode == u'6':
current_location = (current_location[0]+1, current_location[1])
if events[0].unicode == u'2':
current_location = (current_location[0], current_location[1]+1)
if events[0].unicode == u'4':
current_location = (current_location[0]-1, current_location[1])
In the first case we’re adding 1 to the first coordinate. This is the mathematical way of saying “move right”.
current_location = (current_location[0]+1, current_location[1])
In the second case we’re adding 1 to the second coordinate. This is the mathematical way of saying “move down”.
current_location = (current_location[0], current_location[1]+1)
And finally, in the third case case we’re subtracting 1 from the first coordinate. This is the mathematical way of saying “move left”.
current_location = (current_location[0]-1, current_location[1])
To test it out, add a print statement at the end of all the if statements and you’ll see the location change as you press the keys:
if events[0].unicode == u'6':
current_location = (current_location[0]+1, current_location[1])
if events[0].unicode == u'2':
current_location = (current_location[0], current_location[1]+1)
if events[0].unicode == u'4':
current_location = (current_location[0]-1, current_location[1])
print current_location
And this is what it prints:
[<Event(2-KeyDown {'key': 258, 'unicode': u'2', 'mod': 4096})>]
(0, 1)
[<Event(2-KeyDown {'key': 258, 'unicode': u'2', 'mod': 4096})>]
(0, 2)
[<Event(2-KeyDown {'key': 258, 'unicode': u'2', 'mod': 4096})>]
(0, 3)
[<Event(2-KeyDown {'key': 262, 'unicode': u'6', 'mod': 4096})>]
(1, 3)
[<Event(2-KeyDown {'key': 262, 'unicode': u'6', 'mod': 4096})>]
(2, 3)
[<Event(2-KeyDown {'key': 260, 'unicode': u'4', 'mod': 4096})>]
(1, 3)
[<Event(2-KeyDown {'key': 260, 'unicode': u'4', 'mod': 4096})>]
(0, 3)
[<Event(2-KeyDown {'key': 262, 'unicode': u'6', 'mod': 4096})>]
In the next tutorial we will draw the tetros onto our grid.