按键盘移动#

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