Better Move By Keyboard¶
If a player presses the left key, the sprite should move left. If the player hits both left and right, the player should stop. If the player lets off the left key, keeping the right key down, the player should move right.
The simpler method of handling keystrokes will not handle this correctly. This code tracks which key is down or up, and handles it properly.
See the highlighted sections.
1"""
2Better Move Sprite With Keyboard
3
4Simple program to show moving a sprite with the keyboard.
5This is slightly better than sprite_move_keyboard.py example
6in how it works, but also slightly more complex.
7
8Artwork from http://kenney.nl
9
10If Python and Arcade are installed, this example can be run from the command line with:
11python -m arcade.examples.sprite_move_keyboard_better
12"""
13
14import arcade
15
16SPRITE_SCALING = 0.5
17
18SCREEN_WIDTH = 800
19SCREEN_HEIGHT = 600
20SCREEN_TITLE = "Better Move Sprite with Keyboard Example"
21
22MOVEMENT_SPEED = 5
23
24
25class Player(arcade.Sprite):
26
27 def update(self):
28 """ Move the player """
29 # Move player.
30 # Remove these lines if physics engine is moving player.
31 self.center_x += self.change_x
32 self.center_y += self.change_y
33
34 # Check for out-of-bounds
35 if self.left < 0:
36 self.left = 0
37 elif self.right > SCREEN_WIDTH - 1:
38 self.right = SCREEN_WIDTH - 1
39
40 if self.bottom < 0:
41 self.bottom = 0
42 elif self.top > SCREEN_HEIGHT - 1:
43 self.top = SCREEN_HEIGHT - 1
44
45
46class MyGame(arcade.Window):
47 """
48 Main application class.
49 """
50
51 def __init__(self, width, height, title):
52 """
53 Initializer
54 """
55
56 # Call the parent class initializer
57 super().__init__(width, height, title)
58
59 # Variables that will hold sprite lists
60 self.player_list = None
61
62 # Set up the player info
63 self.player_sprite = None
64
65 # Track the current state of what key is pressed
66 self.left_pressed = False
67 self.right_pressed = False
68 self.up_pressed = False
69 self.down_pressed = False
70
71 # Set the background color
72 arcade.set_background_color(arcade.color.AMAZON)
73
74 def setup(self):
75 """ Set up the game and initialize the variables. """
76
77 # Sprite lists
78 self.player_list = arcade.SpriteList()
79
80 # Set up the player
81 self.player_sprite = Player(":resources:images/animated_characters/female_person/femalePerson_idle.png", SPRITE_SCALING)
82 self.player_sprite.center_x = 50
83 self.player_sprite.center_y = 50
84 self.player_list.append(self.player_sprite)
85
86 def on_draw(self):
87 """
88 Render the screen.
89 """
90
91 # This command has to happen before we start drawing
92 arcade.start_render()
93
94 # Draw all the sprites.
95 self.player_list.draw()
96
97 def on_update(self, delta_time):
98 """ Movement and game logic """
99
100 # Calculate speed based on the keys pressed
101 self.player_sprite.change_x = 0
102 self.player_sprite.change_y = 0
103
104 if self.up_pressed and not self.down_pressed:
105 self.player_sprite.change_y = MOVEMENT_SPEED
106 elif self.down_pressed and not self.up_pressed:
107 self.player_sprite.change_y = -MOVEMENT_SPEED
108 if self.left_pressed and not self.right_pressed:
109 self.player_sprite.change_x = -MOVEMENT_SPEED
110 elif self.right_pressed and not self.left_pressed:
111 self.player_sprite.change_x = MOVEMENT_SPEED
112
113 # Call update to move the sprite
114 # If using a physics engine, call update player to rely on physics engine
115 # for movement, and call physics engine here.
116 self.player_list.update()
117
118 def on_key_press(self, key, modifiers):
119 """Called whenever a key is pressed. """
120
121 if key == arcade.key.UP:
122 self.up_pressed = True
123 elif key == arcade.key.DOWN:
124 self.down_pressed = True
125 elif key == arcade.key.LEFT:
126 self.left_pressed = True
127 elif key == arcade.key.RIGHT:
128 self.right_pressed = True
129
130 def on_key_release(self, key, modifiers):
131 """Called when the user releases a key. """
132
133 if key == arcade.key.UP:
134 self.up_pressed = False
135 elif key == arcade.key.DOWN:
136 self.down_pressed = False
137 elif key == arcade.key.LEFT:
138 self.left_pressed = False
139 elif key == arcade.key.RIGHT:
140 self.right_pressed = False
141
142
143def main():
144 """ Main method """
145 window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
146 window.setup()
147 arcade.run()
148
149
150if __name__ == "__main__":
151 main()