使用精灵动画移动#

sprite_move_animation.py#
1"""
2Move with a Sprite Animation
3
4Simple program to show basic sprite usage.
5
6Artwork from https://kenney.nl
7
8If Python and Arcade are installed, this example can be run from the command line with:
9python -m arcade.examples.sprite_move_animation
10"""
11import arcade
12import random
13
14SCREEN_WIDTH = 800
15SCREEN_HEIGHT = 600
16SCREEN_TITLE = "Move with a Sprite Animation Example"
17
18COIN_SCALE = 0.5
19COIN_COUNT = 50
20CHARACTER_SCALING = 1
21
22# How fast to move, and how fast to run the animation
23MOVEMENT_SPEED = 5
24UPDATES_PER_FRAME = 5
25
26# Constants used to track if the player is facing left or right
27RIGHT_FACING = 0
28LEFT_FACING = 1
29
30
31class PlayerCharacter(arcade.Sprite):
32 def __init__(self):
33 # Default to face-right
34 self.character_face_direction = RIGHT_FACING
35
36 # Used for flipping between image sequences
37 self.cur_texture = 0
38
39 # Adjust the collision box. Default includes too much empty space
40 # side-to-side. Box is centered at sprite center, (0, 0)
41 self.points = [[-22, -64], [22, -64], [22, 28], [-22, 28]]
42
43 # --- Load Textures ---
44
45 # Images from Kenney.nl's Asset Pack 3
46 main_path = ":resources:images/animated_characters/female_adventurer/femaleAdventurer"
47 # main_path = ":resources:images/animated_characters/female_person/femalePerson"
48 # main_path = ":resources:images/animated_characters/male_person/malePerson"
49 # main_path = ":resources:images/animated_characters/male_adventurer/maleAdventurer"
50 # main_path = ":resources:images/animated_characters/zombie/zombie"
51 # main_path = ":resources:images/animated_characters/robot/robot"
52
53 # Load textures for idle standing
54 self.idle_texture_pair = arcade.load_texture_pair(f"{main_path}_idle.png")
55 # Set up parent class
56 super().__init__(self.idle_texture_pair[0], scale=CHARACTER_SCALING)
57
58 # Load textures for walking
59 self.walk_textures = []
60 for i in range(8):
61 texture = arcade.load_texture_pair(f"{main_path}_walk{i}.png")
62 self.walk_textures.append(texture)
63
64 def update_animation(self, delta_time: float = 1 / 60):
65
66 # Figure out if we need to flip face left or right
67 if self.change_x < 0 and self.character_face_direction == RIGHT_FACING:
68 self.character_face_direction = LEFT_FACING
69 elif self.change_x > 0 and self.character_face_direction == LEFT_FACING:
70 self.character_face_direction = RIGHT_FACING
71
72 # Idle animation
73 if self.change_x == 0 and self.change_y == 0:
74 self.texture = self.idle_texture_pair[self.character_face_direction]
75 return
76
77 # Walking animation
78 self.cur_texture += 1
79 if self.cur_texture > 7 * UPDATES_PER_FRAME:
80 self.cur_texture = 0
81 frame = self.cur_texture // UPDATES_PER_FRAME
82 direction = self.character_face_direction
83 self.texture = self.walk_textures[frame][direction]
84
85
86class MyGame(arcade.Window):
87 """ Main application class. """
88
89 def __init__(self, width, height, title):
90 """ Set up the game and initialize the variables. """
91 super().__init__(width, height, title)
92
93 # Sprite lists
94 self.player_list = None
95 self.coin_list = None
96
97 # Set up the player
98 self.score = 0
99 self.player = None
100
101 def setup(self):
102 self.player_list = arcade.SpriteList()
103 self.coin_list = arcade.SpriteList()
104
105 # Set up the player
106 self.score = 0
107 self.player = PlayerCharacter()
108
109 self.player.center_x = SCREEN_WIDTH // 2
110 self.player.center_y = SCREEN_HEIGHT // 2
111 self.player.scale = 0.8
112
113 self.player_list.append(self.player)
114
115 for i in range(COIN_COUNT):
116 coin = arcade.Sprite(":resources:images/items/gold_1.png",
117 scale=0.5)
118 coin.center_x = random.randrange(SCREEN_WIDTH)
119 coin.center_y = random.randrange(SCREEN_HEIGHT)
120
121 self.coin_list.append(coin)
122
123 # Set the background color
124 self.background_color = arcade.color.AMAZON
125
126 def on_draw(self):
127 """
128 Render the screen.
129 """
130
131 # This command has to happen before we start drawing
132 self.clear()
133
134 # Draw all the sprites.
135 self.coin_list.draw()
136 self.player_list.draw()
137
138 # Put the text on the screen.
139 output = f"Score: {self.score}"
140 arcade.draw_text(output, 10, 20, arcade.color.WHITE, 14)
141
142 def on_key_press(self, key, modifiers):
143 """
144 Called whenever a key is pressed.
145 """
146 if key == arcade.key.UP:
147 self.player.change_y = MOVEMENT_SPEED
148 elif key == arcade.key.DOWN:
149 self.player.change_y = -MOVEMENT_SPEED
150 elif key == arcade.key.LEFT:
151 self.player.change_x = -MOVEMENT_SPEED
152 elif key == arcade.key.RIGHT:
153 self.player.change_x = MOVEMENT_SPEED
154
155 def on_key_release(self, key, modifiers):
156 """
157 Called when the user releases a key.
158 """
159 if key == arcade.key.UP or key == arcade.key.DOWN:
160 self.player.change_y = 0
161 elif key == arcade.key.LEFT or key == arcade.key.RIGHT:
162 self.player.change_x = 0
163
164 def on_update(self, delta_time):
165 """ Movement and game logic """
166
167 # Move the player
168 self.player_list.update()
169
170 # Update the players animation
171 self.player_list.update_animation()
172
173 # Generate a list of all sprites that collided with the player.
174 hit_list = arcade.check_for_collision_with_list(self.player, self.coin_list)
175
176 # Loop through each colliding sprite, remove it, and add to the score.
177 for coin in hit_list:
178 coin.remove_from_sprite_lists()
179 self.score += 1
180
181
182def main():
183 """ Main function """
184 window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
185 window.setup()
186 arcade.run()
187
188
189if __name__ == "__main__":
190 main()