Moving Platforms#
sprite_moving_platforms.py#
1"""
2Sprite with Moving Platforms
3
4Artwork from https://kenney.nl
5
6If Python and Arcade are installed, this example can be run from the command line with:
7python -m arcade.examples.sprite_moving_platforms
8"""
9import arcade
10from pyglet.math import Vec2
11
12SPRITE_SCALING = 0.5
13
14SCREEN_WIDTH = 1000
15SCREEN_HEIGHT = 600
16SCREEN_TITLE = "Sprite with Moving Platforms Example"
17SPRITE_PIXEL_SIZE = 128
18GRID_PIXEL_SIZE = (SPRITE_PIXEL_SIZE * SPRITE_SCALING)
19
20# How many pixels to keep as a minimum margin between the character
21# and the edge of the screen.
22VIEWPORT_MARGIN = SPRITE_PIXEL_SIZE * SPRITE_SCALING
23RIGHT_MARGIN = 4 * SPRITE_PIXEL_SIZE * SPRITE_SCALING
24
25# Physics
26MOVEMENT_SPEED = 10 * SPRITE_SCALING
27JUMP_SPEED = 28 * SPRITE_SCALING
28GRAVITY = .9 * SPRITE_SCALING
29
30# How fast the camera pans to the player. 1.0 is instant.
31CAMERA_SPEED = 0.1
32
33
34class MyGame(arcade.Window):
35 """ Main application class. """
36
37 def __init__(self, width, height, title):
38 """ Initializer """
39
40 # Call the parent init
41 super().__init__(width, height, title)
42
43 # Sprite lists
44
45 # Drawing non-moving walls separate from moving walls improves performance.
46 self.static_wall_list = None
47 self.moving_wall_list = None
48
49 self.player_list = None
50
51 # Set up the player
52 self.player_sprite = None
53 self.physics_engine = None
54 self.game_over = False
55
56 # Create the cameras. One for the GUI, one for the sprites.
57 # We scroll the 'sprite world' but not the GUI.
58 self.camera_sprites = arcade.SimpleCamera()
59 self.camera_gui = arcade.SimpleCamera()
60
61 self.left_down = False
62 self.right_down = False
63
64 def setup(self):
65 """ Set up the game and initialize the variables. """
66
67 # Sprite lists
68 self.static_wall_list = arcade.SpriteList()
69 self.moving_wall_list = arcade.SpriteList()
70 self.player_list = arcade.SpriteList()
71
72 # Set up the player
73 self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/"
74 "femalePerson_idle.png",
75 SPRITE_SCALING)
76 self.player_sprite.center_x = 2 * GRID_PIXEL_SIZE
77 self.player_sprite.center_y = 3 * GRID_PIXEL_SIZE
78 self.player_list.append(self.player_sprite)
79
80 # Create floor
81 for i in range(30):
82 wall = arcade.Sprite(":resources:images/tiles/grassMid.png", SPRITE_SCALING)
83 wall.bottom = 0
84 wall.center_x = i * GRID_PIXEL_SIZE
85 self.static_wall_list.append(wall)
86
87 # Create platform side to side
88 wall = arcade.Sprite(":resources:images/tiles/grassMid.png", SPRITE_SCALING)
89 wall.center_y = 3 * GRID_PIXEL_SIZE
90 wall.center_x = 3 * GRID_PIXEL_SIZE
91 wall.boundary_left = 2 * GRID_PIXEL_SIZE
92 wall.boundary_right = 5 * GRID_PIXEL_SIZE
93 wall.change_x = 2 * SPRITE_SCALING
94 self.moving_wall_list.append(wall)
95
96 # Create platform side to side
97 wall = arcade.Sprite(":resources:images/tiles/grassMid.png", SPRITE_SCALING)
98 wall.center_y = 3 * GRID_PIXEL_SIZE
99 wall.center_x = 7 * GRID_PIXEL_SIZE
100 wall.boundary_left = 5 * GRID_PIXEL_SIZE
101 wall.boundary_right = 9 * GRID_PIXEL_SIZE
102 wall.change_x = -2 * SPRITE_SCALING
103 self.moving_wall_list.append(wall)
104
105 # Create platform moving up and down
106 wall = arcade.Sprite(":resources:images/tiles/grassMid.png", SPRITE_SCALING)
107 wall.center_y = 5 * GRID_PIXEL_SIZE
108 wall.center_x = 5 * GRID_PIXEL_SIZE
109 wall.boundary_top = 8 * GRID_PIXEL_SIZE
110 wall.boundary_bottom = 4 * GRID_PIXEL_SIZE
111 wall.change_y = 2 * SPRITE_SCALING
112 self.moving_wall_list.append(wall)
113
114 # Create platform moving diagonally
115 wall = arcade.Sprite(":resources:images/tiles/grassMid.png", SPRITE_SCALING)
116 wall.center_y = 5 * GRID_PIXEL_SIZE
117 wall.center_x = 8 * GRID_PIXEL_SIZE
118 wall.boundary_left = 7 * GRID_PIXEL_SIZE
119 wall.boundary_right = 9 * GRID_PIXEL_SIZE
120 wall.boundary_top = 8 * GRID_PIXEL_SIZE
121 wall.boundary_bottom = 4 * GRID_PIXEL_SIZE
122 wall.change_x = 2 * SPRITE_SCALING
123 wall.change_y = 2 * SPRITE_SCALING
124 self.moving_wall_list.append(wall)
125
126 # Create our physics engine
127 self.physics_engine = \
128 arcade.PhysicsEnginePlatformer(self.player_sprite,
129 [self.static_wall_list, self.moving_wall_list],
130 gravity_constant=GRAVITY)
131
132 # Set the background color
133 arcade.set_background_color(arcade.color.AMAZON)
134
135 self.game_over = False
136
137 def on_draw(self):
138 """
139 Render the screen.
140 """
141
142 # This command has to happen before we start drawing
143 self.clear()
144
145 # Select the camera we'll use to draw all our sprites
146 self.camera_sprites.use()
147
148 # Draw the sprites.
149 self.static_wall_list.draw()
150 self.moving_wall_list.draw()
151 self.player_list.draw()
152
153 self.camera_gui.use()
154
155 # Put the text on the screen.
156 distance = self.player_sprite.right
157 output = f"Distance: {distance}"
158 arcade.draw_text(output, 10, 20, arcade.color.WHITE, 14)
159
160 def set_x_speed(self):
161 if self.left_down and not self.right_down:
162 self.player_sprite.change_x = -MOVEMENT_SPEED
163 elif self.right_down and not self.left_down:
164 self.player_sprite.change_x = MOVEMENT_SPEED
165 else:
166 self.player_sprite.change_x = 0
167
168 def on_key_press(self, key, modifiers):
169 """ Called whenever the mouse moves. """
170 if key == arcade.key.UP:
171 if self.physics_engine.can_jump():
172 self.player_sprite.change_y = JUMP_SPEED
173 elif key == arcade.key.LEFT:
174 self.left_down = True
175 self.set_x_speed()
176 elif key == arcade.key.RIGHT:
177 self.right_down = True
178 self.set_x_speed()
179
180 def on_key_release(self, key, modifiers):
181 """ Called when the user presses a mouse button. """
182 if key == arcade.key.LEFT:
183 self.left_down = False
184 self.set_x_speed()
185 elif key == arcade.key.RIGHT:
186 self.right_down = False
187 self.set_x_speed()
188
189 def on_update(self, delta_time):
190 """ Movement and game logic """
191
192 # Call update on all sprites
193 self.physics_engine.update()
194
195 # Scroll the screen to the player
196 self.scroll_to_player()
197
198 def scroll_to_player(self):
199 """
200 Scroll the window to the player.
201
202 if CAMERA_SPEED is 1, the camera will immediately move to the desired position.
203 Anything between 0 and 1 will have the camera move to the location with a smoother
204 pan.
205 """
206
207 position = Vec2(self.player_sprite.center_x - self.width / 2,
208 self.player_sprite.center_y - self.height / 2)
209 self.camera_sprites.move_to(position, CAMERA_SPEED)
210
211
212def main():
213 """ Main function """
214 window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
215 window.setup()
216 arcade.run()
217
218
219if __name__ == "__main__":
220 main()