--- /home/docs/checkouts/readthedocs.org/user_builds/arcade-library/checkouts/2.5.7/doc/tutorials/pymunk_platformer/pymunk_demo_platformer_11.py
+++ /home/docs/checkouts/readthedocs.org/user_builds/arcade-library/checkouts/2.5.7/doc/tutorials/pymunk_platformer/pymunk_demo_platformer_12.py
@@ -76,7 +76,9 @@
class PlayerSprite(arcade.Sprite):
""" Player Sprite """
- def __init__(self):
+ def __init__(self,
+ ladder_list: arcade.SpriteList,
+ hit_box_algorithm):
""" Init """
# Let parent initialize
super().__init__()
@@ -93,7 +95,8 @@
# main_path = ":resources:images/animated_characters/robot/robot"
# Load textures for idle standing
- self.idle_texture_pair = arcade.load_texture_pair(f"{main_path}_idle.png")
+ self.idle_texture_pair = arcade.load_texture_pair(f"{main_path}_idle.png",
+ hit_box_algorithm=hit_box_algorithm)
self.jump_texture_pair = arcade.load_texture_pair(f"{main_path}_jump.png")
self.fall_texture_pair = arcade.load_texture_pair(f"{main_path}_fall.png")
@@ -103,6 +106,13 @@
texture = arcade.load_texture_pair(f"{main_path}_walk{i}.png")
self.walk_textures.append(texture)
+ # Load textures for climbing
+ self.climbing_textures = []
+ texture = arcade.load_texture(f"{main_path}_climb0.png")
+ self.climbing_textures.append(texture)
+ texture = arcade.load_texture(f"{main_path}_climb1.png")
+ self.climbing_textures.append(texture)
+
# Set the initial texture
self.texture = self.idle_texture_pair[0]
@@ -117,6 +127,10 @@
# How far have we traveled horizontally since changing the texture
self.x_odometer = 0
+ self.y_odometer = 0
+
+ self.ladder_list = ladder_list
+ self.is_on_ladder = False
def pymunk_moved(self, physics_engine, dx, dy, d_angle):
""" Handle being moved by the pymunk engine """
@@ -129,8 +143,38 @@
# Are we on the ground?
is_on_ground = physics_engine.is_on_ground(self)
+ # Are we on a ladder?
+ if len(arcade.check_for_collision_with_list(self, self.ladder_list)) > 0:
+ if not self.is_on_ladder:
+ self.is_on_ladder = True
+ self.pymunk.gravity = (0, 0)
+ self.pymunk.damping = 0.0001
+ self.pymunk.max_vertical_velocity = PLAYER_MAX_HORIZONTAL_SPEED
+ else:
+ if self.is_on_ladder:
+ self.pymunk.damping = 1.0
+ self.pymunk.max_vertical_velocity = PLAYER_MAX_VERTICAL_SPEED
+ self.is_on_ladder = False
+ self.pymunk.gravity = None
+
# Add to the odometer how far we've moved
self.x_odometer += dx
+ self.y_odometer += dy
+
+ if self.is_on_ladder and not is_on_ground:
+ # Have we moved far enough to change the texture?
+ if abs(self.y_odometer) > DISTANCE_TO_CHANGE_TEXTURE:
+
+ # Reset the odometer
+ self.y_odometer = 0
+
+ # Advance the walking animation
+ self.cur_texture += 1
+
+ if self.cur_texture > 1:
+ self.cur_texture = 0
+ self.texture = self.climbing_textures[self.cur_texture]
+ return
# Jumping animation
if not is_on_ground:
@@ -184,10 +228,13 @@
self.bullet_list: Optional[arcade.SpriteList] = None
self.item_list: Optional[arcade.SpriteList] = None
self.moving_sprites_list: Optional[arcade.SpriteList] = None
+ self.ladder_list: Optional[arcade.SpriteList] = None
# Track the current state of what key is pressed
self.left_pressed: bool = False
self.right_pressed: bool = False
+ self.up_pressed: bool = False
+ self.down_pressed: bool = False
# Physics engine
self.physics_engine = Optional[arcade.PymunkPhysicsEngine]
@@ -207,11 +254,22 @@
my_map = arcade.tilemap.read_tmx(map_name)
# Read in the map layers
- self.wall_list = arcade.tilemap.process_layer(my_map, 'Platforms', SPRITE_SCALING_TILES)
- self.item_list = arcade.tilemap.process_layer(my_map, 'Dynamic Items', SPRITE_SCALING_TILES)
+ self.wall_list = arcade.tilemap.process_layer(my_map,
+ 'Platforms',
+ SPRITE_SCALING_TILES,
+ hit_box_algorithm="Detailed")
+ self.item_list = arcade.tilemap.process_layer(my_map,
+ 'Dynamic Items',
+ SPRITE_SCALING_TILES,
+ hit_box_algorithm="Detailed")
+ self.ladder_list = arcade.tilemap.process_layer(my_map,
+ 'Ladders',
+ SPRITE_SCALING_TILES,
+ use_spatial_hash=True,
+ hit_box_algorithm="Detailed")
# Create player sprite
- self.player_sprite = PlayerSprite()
+ self.player_sprite = PlayerSprite(self.ladder_list, hit_box_algorithm="Detailed")
# Set player location
grid_x = 1
@@ -303,11 +361,15 @@
elif key == arcade.key.RIGHT:
self.right_pressed = True
elif key == arcade.key.UP:
- # find out if player is standing on ground
- if self.physics_engine.is_on_ground(self.player_sprite):
+ self.up_pressed = True
+ # find out if player is standing on ground, and not on a ladder
+ if self.physics_engine.is_on_ground(self.player_sprite) \
+ and not self.player_sprite.is_on_ladder:
# She is! Go ahead and jump
impulse = (0, PLAYER_JUMP_IMPULSE)
self.physics_engine.apply_impulse(self.player_sprite, impulse)
+ elif key == arcade.key.DOWN:
+ self.down_pressed = True
def on_key_release(self, key, modifiers):
"""Called when the user releases a key. """
@@ -316,6 +378,10 @@
self.left_pressed = False
elif key == arcade.key.RIGHT:
self.right_pressed = False
+ elif key == arcade.key.UP:
+ self.up_pressed = False
+ elif key == arcade.key.DOWN:
+ self.down_pressed = False
def on_mouse_press(self, x, y, button, modifiers):
""" Called whenever the mouse button is clicked. """
@@ -378,7 +444,7 @@
# Update player forces based on keys pressed
if self.left_pressed and not self.right_pressed:
# Create a force to the left. Apply it.
- if is_on_ground:
+ if is_on_ground or self.player_sprite.is_on_ladder:
force = (-PLAYER_MOVE_FORCE_ON_GROUND, 0)
else:
force = (-PLAYER_MOVE_FORCE_IN_AIR, 0)
@@ -387,13 +453,28 @@
self.physics_engine.set_friction(self.player_sprite, 0)
elif self.right_pressed and not self.left_pressed:
# Create a force to the right. Apply it.
- if is_on_ground:
+ if is_on_ground or self.player_sprite.is_on_ladder:
force = (PLAYER_MOVE_FORCE_ON_GROUND, 0)
else:
force = (PLAYER_MOVE_FORCE_IN_AIR, 0)
self.physics_engine.apply_force(self.player_sprite, force)
# Set friction to zero for the player while moving
self.physics_engine.set_friction(self.player_sprite, 0)
+ elif self.up_pressed and not self.down_pressed:
+ # Create a force to the right. Apply it.
+ if self.player_sprite.is_on_ladder:
+ force = (0, PLAYER_MOVE_FORCE_ON_GROUND)
+ self.physics_engine.apply_force(self.player_sprite, force)
+ # Set friction to zero for the player while moving
+ self.physics_engine.set_friction(self.player_sprite, 0)
+ elif self.down_pressed and not self.up_pressed:
+ # Create a force to the right. Apply it.
+ if self.player_sprite.is_on_ladder:
+ force = (0, -PLAYER_MOVE_FORCE_ON_GROUND)
+ self.physics_engine.apply_force(self.player_sprite, force)
+ # Set friction to zero for the player while moving
+ self.physics_engine.set_friction(self.player_sprite, 0)
+
else:
# Player's feet are not moving. Therefore up the friction so we stop.
self.physics_engine.set_friction(self.player_sprite, 1.0)
@@ -431,10 +512,16 @@
""" Draw everything """
arcade.start_render()
self.wall_list.draw()
+ self.ladder_list.draw()
self.moving_sprites_list.draw()
self.bullet_list.draw()
self.item_list.draw()
self.player_list.draw()
+
+ # for item in self.player_list:
+ # item.draw_hit_box(arcade.color.RED)
+ # for item in self.item_list:
+ # item.draw_hit_box(arcade.color.RED)
def main():
""" Main method """