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 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
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/femalePerson_idle.png", SPRITE_SCALING)
77 self.player_sprite.center_x = 50
78 self.player_sprite.center_y = 50
79 self.player_list.append(self.player_sprite)
80
81 def on_draw(self):
82 """
83 Render the screen.
84 """
85
86 # This command has to happen before we start drawing
87 arcade.start_render()
88
89 # Draw all the sprites.
90 self.player_list.draw()
91
92 def on_update(self, delta_time):
93 """ Movement and game logic """
94
95 # Move the player
96 self.player_list.update()
97
98 def on_key_press(self, key, modifiers):
99 """Called whenever a key is pressed. """
100
101 # If the player presses a key, update the speed
102 if key == arcade.key.UP:
103 self.player_sprite.change_y = MOVEMENT_SPEED
104 elif key == arcade.key.DOWN:
105 self.player_sprite.change_y = -MOVEMENT_SPEED
106 elif key == arcade.key.LEFT:
107 self.player_sprite.change_x = -MOVEMENT_SPEED
108 elif key == arcade.key.RIGHT:
109 self.player_sprite.change_x = MOVEMENT_SPEED
110
111 def on_key_release(self, key, modifiers):
112 """Called when the user releases a key. """
113
114 # If a player releases a key, zero out the speed.
115 # This doesn't work well if multiple keys are pressed.
116 # Use 'better move by keyboard' example if you need to
117 # handle this.
118 if key == arcade.key.UP or key == arcade.key.DOWN:
119 self.player_sprite.change_y = 0
120 elif key == arcade.key.LEFT or key == arcade.key.RIGHT:
121 self.player_sprite.change_x = 0
122
123
124def main():
125 """ Main method """
126 window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
127 window.setup()
128 arcade.run()
129
130
131if __name__ == "__main__":
132 main()