Move By Keyboard#
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
14from __future__ import annotations
15
16import arcade
17
18SPRITE_SCALING = 0.5
19
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22SCREEN_TITLE = "Move Sprite with Keyboard Example"
23
24MOVEMENT_SPEED = 5
25
26
27class Player(arcade.Sprite):
28 """ Player Class """
29
30 def update(self):
31 """ Move the player """
32 # Move player.
33 # Remove these lines if physics engine is moving player.
34 self.center_x += self.change_x
35 self.center_y += self.change_y
36
37 # Check for out-of-bounds
38 if self.left < 0:
39 self.left = 0
40 elif self.right > SCREEN_WIDTH - 1:
41 self.right = SCREEN_WIDTH - 1
42
43 if self.bottom < 0:
44 self.bottom = 0
45 elif self.top > SCREEN_HEIGHT - 1:
46 self.top = SCREEN_HEIGHT - 1
47
48
49class MyGame(arcade.Window):
50 """
51 Main application class.
52 """
53
54 def __init__(self, width, height, title):
55 """
56 Initializer
57 """
58
59 # Call the parent class initializer
60 super().__init__(width, height, title)
61
62 # Variables that will hold sprite lists
63 self.player_list = None
64
65 # Set up the player info
66 self.player_sprite = None
67
68 # Set the background color
69 self.background_color = arcade.color.AMAZON
70
71 def setup(self):
72 """ Set up the game and initialize the variables. """
73
74 # Sprite lists
75 self.player_list = arcade.SpriteList()
76
77 # Set up the player
78 self.player_sprite = Player(
79 ":resources:images/animated_characters/female_person/femalePerson_idle.png",
80 scale=SPRITE_SCALING,
81 )
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 self.clear()
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 # Move the player
101 self.player_list.update()
102
103 def on_key_press(self, key, modifiers):
104 """Called whenever a key is pressed. """
105
106 # If the player presses a key, update the speed
107 if key == arcade.key.UP:
108 self.player_sprite.change_y = MOVEMENT_SPEED
109 elif key == arcade.key.DOWN:
110 self.player_sprite.change_y = -MOVEMENT_SPEED
111 elif key == arcade.key.LEFT:
112 self.player_sprite.change_x = -MOVEMENT_SPEED
113 elif key == arcade.key.RIGHT:
114 self.player_sprite.change_x = MOVEMENT_SPEED
115
116 def on_key_release(self, key, modifiers):
117 """Called when the user releases a key. """
118
119 # If a player releases a key, zero out the speed.
120 # This doesn't work well if multiple keys are pressed.
121 # Use 'better move by keyboard' example if you need to
122 # handle this.
123 if key == arcade.key.UP or key == arcade.key.DOWN:
124 self.player_sprite.change_y = 0
125 elif key == arcade.key.LEFT or key == arcade.key.RIGHT:
126 self.player_sprite.change_x = 0
127
128
129def main():
130 """ Main function """
131 window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
132 window.setup()
133 arcade.run()
134
135
136if __name__ == "__main__":
137 main()