Sprites That Follow a Path#
follow_path.py#
1"""
2Sprite Follow Path
3
4This example has enemy sprites follow a set path.
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.follow_path
10"""
11
12import arcade
13import math
14
15# --- Constants ---
16SPRITE_SCALING_PLAYER = 0.5
17SPRITE_SCALING_ENEMY = 0.5
18ENEMY_SPEED = 3.0
19
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22SCREEN_TITLE = "Sprite Follow Path Simple Example"
23
24
25class Enemy(arcade.Sprite):
26 """
27 This class represents the Enemy on our screen.
28 """
29
30 def __init__(self, image, scale, position_list):
31 super().__init__(image, scale)
32 self.position_list = position_list
33 self.cur_position = 0
34 self.speed = ENEMY_SPEED
35
36 def update(self):
37 """ Have a sprite follow a path """
38
39 # Where are we
40 start_x = self.center_x
41 start_y = self.center_y
42
43 # Where are we going
44 dest_x = self.position_list[self.cur_position][0]
45 dest_y = self.position_list[self.cur_position][1]
46
47 # X and Y diff between the two
48 x_diff = dest_x - start_x
49 y_diff = dest_y - start_y
50
51 # Calculate angle to get there
52 angle = math.atan2(y_diff, x_diff)
53
54 # How far are we?
55 distance = math.sqrt((self.center_x - dest_x) ** 2 + (self.center_y - dest_y) ** 2)
56
57 # How fast should we go? If we are close to our destination,
58 # lower our speed so we don't overshoot.
59 speed = min(self.speed, distance)
60
61 # Calculate vector to travel
62 change_x = math.cos(angle) * speed
63 change_y = math.sin(angle) * speed
64
65 # Update our location
66 self.center_x += change_x
67 self.center_y += change_y
68
69 # How far are we?
70 distance = math.sqrt((self.center_x - dest_x) ** 2 + (self.center_y - dest_y) ** 2)
71
72 # If we are there, head to the next point.
73 if distance <= self.speed:
74 self.cur_position += 1
75
76 # Reached the end of the list, start over.
77 if self.cur_position >= len(self.position_list):
78 self.cur_position = 0
79
80
81class MyGame(arcade.Window):
82 """ Our custom Window Class"""
83
84 def __init__(self):
85 """ Initializer """
86 # Call the parent class initializer
87 super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
88
89 # Variables that will hold sprite lists
90 self.player_list = None
91 self.enemy_list = None
92
93 # Set up the player info
94 self.player_sprite = None
95 self.score = 0
96
97 # Don't show the mouse cursor
98 self.set_mouse_visible(False)
99
100 arcade.set_background_color(arcade.color.AMAZON)
101
102 def setup(self):
103 """ Set up the game and initialize the variables. """
104
105 # Sprite lists
106 self.player_list = arcade.SpriteList()
107 self.enemy_list = arcade.SpriteList()
108
109 # Score
110 self.score = 0
111
112 # Set up the player
113 # Character image from kenney.nl
114 self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/"
115 "femalePerson_idle.png", SPRITE_SCALING_PLAYER)
116 self.player_sprite.center_x = 50
117 self.player_sprite.center_y = 50
118 self.player_list.append(self.player_sprite)
119
120 # List of points the enemy will travel too.
121 position_list = [[50, 50],
122 [700, 50],
123 [700, 500],
124 [50, 500]]
125
126 # Create the enemy
127 enemy = Enemy(":resources:images/animated_characters/robot/robot_idle.png",
128 SPRITE_SCALING_ENEMY,
129 position_list)
130
131 # Set initial location of the enemy at the first point
132 enemy.center_x = position_list[0][0]
133 enemy.center_x = position_list[0][1]
134
135 # Add the enemy to the enemy list
136 self.enemy_list.append(enemy)
137
138 def on_draw(self):
139 """ Draw everything """
140 self.clear()
141 self.enemy_list.draw()
142 self.player_list.draw()
143
144 def on_mouse_motion(self, x, y, dx, dy):
145 """ Handle Mouse Motion """
146
147 # Move the center of the player sprite to match the mouse x, y
148 self.player_sprite.center_x = x
149 self.player_sprite.center_y = y
150
151 def on_update(self, delta_time):
152 """ Movement and game logic """
153 self.enemy_list.update()
154
155
156def main():
157 """ Main function """
158 window = MyGame()
159 window.setup()
160 arcade.run()
161
162
163if __name__ == "__main__":
164 main()