Reading the Keyboard


You already know how to get keyboard input from the user as a string through the raw_input() function. But reading the keyboard for individual keystrokes is another matter. Fortunately, there's a simple Screen method that lets you do just this.

Introducing the Read Key Program

The Read Key program displays the ship on the nebula background. The user can move the ship around on the background with a few, different keystrokes. When the user presses the W key, the ship moves up. When the user presses the S key, the ship moves down. When the user presses the A key, the ship moves left. When the user presses the D key, the ship moves right. The user can also press multiple keys simultaneously for a combined effect. For example, when the user presses the W and D keys simultaneously, the ship moves diagonally, up and to the right. The program is illustrated in Figure 12.3.

click to expand
Figure 12.3: The ship moves around the screen based on key presses.

Setting Up the Program

As I do with all programs that use the livewires package, I start by importing the modules I need and setting up some global constants for the screen dimensions:

 # Read Key # Demonstrates reading the keyboard # Michael Dawson 5/18/03 from livewires import games SCREEN_WIDTH = 640 SCREEN_HEIGHT = 480 

Creating the Ship Class

Next, I write a class for the ship. I start with the constructor method, which accepts a screen, x-and y-coordinates, and an image. I initialize the sprite with these values.

 class Ship(games.Sprite):     """ A moving ship. """     def __init__(self, screen, x, y, image):         """ Initialize ship sprite. """         self.init_sprite(screen = screen, x = x, y = y, image = image) 

Testing for Keystrokes

Next, I define a moved() method. First, I get the current position of the ship and assign the coordinates to x and y. Next, I check for various keystrokes and change the values associated with x and y accordingly. If the W key is pressed, I decrease the value of y by 1, moving the sprite up the screen by one pixel. If the S key is pressed, I increase the value of y by 1, moving the sprite down the screen. If the A key is pressed, I decrease the value of x by 1, moving the sprite left. If the D key is pressed, I increase the value of x by 1, moving the sprite right.

     def moved(self):         """ Move ship based on keys pressed. """         x, y = self.get_pos()         if self.screen.is_pressed(games.K_w):             y -= 1         if self.screen.is_pressed(games.K_s):             y += 1         if self.screen.is_pressed(games.K_a):             x -=1         if self.screen.is_pressed(games.K_d):             x +=1         self.move_to(x, y) 

I use the Screen method is_pressed() to test for specific keystrokes. The method returns a value that can be treated as a condition. If the key being tested for is pressed, then the value returned by is_pressed() can be treated as True; if the key is not pressed, the value can be treated as False. I use the method in a series of structures to test if any of the four keys—W, S, A, or D—is being pressed.

The games module has a set of constants that represent keys that you can use as an argument in is_pressed(). In this method, I use the games.K_w constant for the W key; games.K_s for the S key; games.K_a for the A key; and games.K_d for the D key. The naming of these constants is pretty intuitive. Here's a quick way to figure out the name of most key constants:

  • All keyboard constants begin with games.K_.

  • For alphabetic keys, add the key letter, in lowercase, to the end of the constant. For example, the constant for the A key is games.K_a.

  • For numeric keys, add the key number to the end of the constant. For example, the constant for the 1 key is games.K_1.

  • For other keys, you can often add their name, in all capital letters, to the end of the constant name. For example, the constant for the spacebar is games.K_SPACE.

For a complete list of keyboard constants, see the livewires documentation in Appendix A.

The is_pressed() method has a couple of nice features. First, it allows you to detect if a key is pressed even if the user is pressing multiple keys. As a result, keystrokes can have a combined effect. For example, if the user holds down the W and D keys simultaneously in the Read Key program, the ship moves both up and to the right. Second, uppercase and lowercase keystrokes are interpreted as the same key. So in the Read Key program, it doesn't matter if the user accidentally has Caps Lock on—if the user presses the W key, the ship will still move up the screen.

Wrapping Up the Program

Finally, I write the familiar main part of the program. I create the screen, load the nebula background image, create a ship sprite in the middle of the window, and kick everything off by invoking my_screen's mainloop() method.

 # main my_screen = games.Screen(SCREEN_WIDTH, SCREEN_HEIGHT) nebula_image = games.load_image("nebula.jpg", transparent = False) my_screen.set_background(nebula_image) ship_image = games.load_image("ship.bmp") Ship(screen = my_screen,       x = SCREEN_WIDTH / 2, y = SCREEN_HEIGHT / 2,       image = ship_image) my_screen.mainloop() 




Python Programming for the Absolute Beginner
Python Programming for the Absolute Beginner, 3rd Edition
ISBN: 1435455002
EAN: 2147483647
Year: 2003
Pages: 194

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net