Skip to content

Commit e5962e3

Browse files
authored
Document Simple and Platformer engines (#2185)
* Clearer names in a moving platforms example file * Use context managers for cameras as an opportunistic fix * Add type annotations in many places * Rewrite top-level docstrings for the Simple and Platformer engine * Document their attributes and properties * Explain what more of the code is doing with comments * Document Platformer-relevant attributes in Sprite (boundary_*, change_*) * Rewrite SpatialHash top-level docstring and document some attributes
1 parent 7140540 commit e5962e3

File tree

4 files changed

+433
-117
lines changed

4 files changed

+433
-117
lines changed

arcade/examples/sprite_moving_platforms.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def __init__(self, width, height, title):
4343

4444
# Drawing non-moving walls separate from moving walls improves performance.
4545
self.static_wall_list = None
46-
self.moving_wall_list = None
46+
self.moving_platform_list = None
4747

4848
self.player_list = None
4949

@@ -65,7 +65,7 @@ def setup(self):
6565

6666
# Sprite lists
6767
self.static_wall_list = arcade.SpriteList()
68-
self.moving_wall_list = arcade.SpriteList()
68+
self.moving_platform_list = arcade.SpriteList()
6969
self.player_list = arcade.SpriteList()
7070

7171
# Set up the player
@@ -91,7 +91,7 @@ def setup(self):
9191
wall.boundary_left = 2 * GRID_PIXEL_SIZE
9292
wall.boundary_right = 5 * GRID_PIXEL_SIZE
9393
wall.change_x = 2 * SPRITE_SCALING
94-
self.moving_wall_list.append(wall)
94+
self.moving_platform_list.append(wall)
9595

9696
# Create platform side to side
9797
wall = arcade.Sprite(":resources:images/tiles/grassMid.png", scale=SPRITE_SCALING)
@@ -100,7 +100,7 @@ def setup(self):
100100
wall.boundary_left = 5 * GRID_PIXEL_SIZE
101101
wall.boundary_right = 9 * GRID_PIXEL_SIZE
102102
wall.change_x = -2 * SPRITE_SCALING
103-
self.moving_wall_list.append(wall)
103+
self.moving_platform_list.append(wall)
104104

105105
# Create platform moving up and down
106106
wall = arcade.Sprite(":resources:images/tiles/grassMid.png", scale=SPRITE_SCALING)
@@ -109,7 +109,7 @@ def setup(self):
109109
wall.boundary_top = 8 * GRID_PIXEL_SIZE
110110
wall.boundary_bottom = 4 * GRID_PIXEL_SIZE
111111
wall.change_y = 2 * SPRITE_SCALING
112-
self.moving_wall_list.append(wall)
112+
self.moving_platform_list.append(wall)
113113

114114
# Create platform moving diagonally
115115
wall = arcade.Sprite(":resources:images/tiles/grassMid.png", scale=SPRITE_SCALING)
@@ -121,13 +121,15 @@ def setup(self):
121121
wall.boundary_bottom = 4 * GRID_PIXEL_SIZE
122122
wall.change_x = 2 * SPRITE_SCALING
123123
wall.change_y = 2 * SPRITE_SCALING
124-
self.moving_wall_list.append(wall)
124+
self.moving_platform_list.append(wall)
125125

126126
# 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)
127+
self.physics_engine = arcade.PhysicsEnginePlatformer(
128+
self.player_sprite,
129+
platforms=self.moving_platform_list,
130+
walls=self.static_wall_list,
131+
gravity_constant=GRAVITY
132+
)
131133

132134
# Set the background color
133135
self.background_color = arcade.color.AMAZON
@@ -143,19 +145,17 @@ def on_draw(self):
143145
self.clear()
144146

145147
# 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)
148+
with self.camera_sprites.activate():
149+
# Draw the sprites
150+
self.static_wall_list.draw()
151+
self.moving_platform_list.draw()
152+
self.player_list.draw()
153+
154+
# Update & draw our text to the screen
155+
with self.camera_gui.activate():
156+
distance = self.player_sprite.right
157+
output = f"Distance: {distance}"
158+
arcade.draw_text(output, 10, 20, arcade.color.WHITE, 14)
159159

160160
def set_x_speed(self):
161161
if self.left_down and not self.right_down:

0 commit comments

Comments
 (0)