Sprite Move By Keyboard#
sprite_move_keyboard.py#
1"""
2Move Sprite With Keyboard
3
4Simple program to show moving a sprite with the keyboard.
5The sprite_move_keyboard_better.py example is slightly better
6in how it works, but also slightly more complex.
7
8Artwork from https://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
12"""
13
14import arcade
15
16SPRITE_SCALING = 0.5
17
18SCREEN_WIDTH = 800
19SCREEN_HEIGHT = 600
20SCREEN_TITLE = "Move Sprite with Keyboard Example"
21
22MOVEMENT_SPEED = 5
23
24
25class Player(arcade.Sprite):
26 """ Player Class """
27
28 def update(self):
29 """ Move the player """
30 # Move player.
31 # Remove these lines if physics engine is moving player.
32 self.center_x += self.change_x
33 self.center_y += self.change_y
34
35 # Check for out-of-bounds
36 if self.left < 0:
37 self.left = 0
38 elif self.right > SCREEN_WIDTH - 1:
39 self.right = SCREEN_WIDTH - 1
40
41 if self.bottom < 0:
42 self.bottom = 0
43 elif self.top > SCREEN_HEIGHT - 1:
44 self.top = SCREEN_HEIGHT - 1
45
46
47class MyGame(arcade.Window):
48 """
49 Main application class.
50 """
51
52 def __init__(self, width, height, title):
53 """
54 Initializer
55 """
56
57 # Call the parent class initializer
58 super().__init__(width, height, title)
59
60 # Variables that will hold sprite lists
61 self.player_list = None
62
63 # Set up the player info
64 self.player_sprite = None
65
66 # Set the background color
67 arcade.set_background_color(arcade.color.AMAZON)
68
69 def setup(self):
70 """ Set up the game and initialize the variables. """
71
72 # Sprite lists
73 self.player_list = arcade.SpriteList()
74
75 # Set up the player
76 self.player_sprite = Player(":resources:images/animated_characters/female_person/"
77 "femalePerson_idle.png", SPRITE_SCALING)
78 self.player_sprite.center_x = 50
79 self.player_sprite.center_y = 50
80 self.player_list.append(self.player_sprite)
81
82 def on_draw(self):
83 """
84 Render the screen.
85 """
86
87 # This command has to happen before we start drawing
88 self.clear()
89
90 # Draw all the sprites.
91 self.player_list.draw()
92
93 def on_update(self, delta_time):
94 """ Movement and game logic """
95
96 # Move the player
97 self.player_list.update()
98
99 def on_key_press(self, key, modifiers):
100 """Called whenever a key is pressed. """
101
102 # If the player presses a key, update the speed
103 if key == arcade.key.UP:
104 self.player_sprite.change_y = MOVEMENT_SPEED
105 elif key == arcade.key.DOWN:
106 self.player_sprite.change_y = -MOVEMENT_SPEED
107 elif key == arcade.key.LEFT:
108 self.player_sprite.change_x = -MOVEMENT_SPEED
109 elif key == arcade.key.RIGHT:
110 self.player_sprite.change_x = MOVEMENT_SPEED
111
112 def on_key_release(self, key, modifiers):
113 """Called when the user releases a key. """
114
115 # If a player releases a key, zero out the speed.
116 # This doesn't work well if multiple keys are pressed.
117 # Use 'better move by keyboard' example if you need to
118 # handle this.
119 if key == arcade.key.UP or key == arcade.key.DOWN:
120 self.player_sprite.change_y = 0
121 elif key == arcade.key.LEFT or key == arcade.key.RIGHT:
122 self.player_sprite.change_x = 0
123
124
125def main():
126 """ Main function """
127 window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
128 window.setup()
129 arcade.run()
130
131
132if __name__ == "__main__":
133 main()