pymunk_demo_platformer_12.py Diff

pymunk_demo_platformer_12.py
--- /home/docs/checkouts/readthedocs.org/user_builds/arcade-library/checkouts/latest/doc/tutorials/pymunk_platformer/pymunk_demo_platformer_11.py
+++ /home/docs/checkouts/readthedocs.org/user_builds/arcade-library/checkouts/latest/doc/tutorials/pymunk_platformer/pymunk_demo_platformer_12.py
@@ -77,7 +77,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__()
@@ -94,7 +96,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")
 
@@ -104,6 +107,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]
 
@@ -118,6 +128,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 """
@@ -130,8 +144,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:
@@ -159,7 +203,6 @@
                 self.cur_texture = 0
             self.texture = self.walk_textures[self.cur_texture][self.character_face_direction]
 
-
 class BulletSprite(arcade.SpriteSolidColor):
     """ Bullet Sprite """
     def pymunk_moved(self, physics_engine, dx, dy, d_angle):
@@ -168,7 +211,6 @@
         if self.center_y < -100:
             self.remove_from_sprite_lists()
 
-
 class GameWindow(arcade.Window):
     """ Main Window """
 
@@ -187,13 +229,16 @@
         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]
+        self.physics_engine: Optional[arcade.PymunkPhysicsEngine] = None
 
         # Set background color
         arcade.set_background_color(arcade.color.AMAZON)
@@ -214,9 +259,11 @@
         # Pull the sprite layers out of the tile map
         self.wall_list = tile_map.sprite_lists["Platforms"]
         self.item_list = tile_map.sprite_lists["Dynamic Items"]
+        self.ladder_list = tile_map.sprite_lists["Ladders"]
+        self.moving_sprites_list = tile_map.sprite_lists['Moving Platforms']
 
         # Create player sprite
-        self.player_sprite = PlayerSprite()
+        self.player_sprite = PlayerSprite(self.ladder_list, hit_box_algorithm="Detailed")
 
         # Set player location
         grid_x = 1
@@ -225,11 +272,6 @@
         self.player_sprite.center_y = SPRITE_SIZE * grid_y + SPRITE_SIZE / 2
         # Add to player sprite list
         self.player_list.append(self.player_sprite)
-
-        # Moving Sprite
-        self.moving_sprites_list = arcade.tilemap.process_layer(my_map,
-                                                                'Moving Platforms',
-                                                                SPRITE_SCALING_TILES)
 
         # --- Pymunk Physics Engine Setup ---
 
@@ -308,11 +350,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. """
@@ -321,6 +367,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. """
@@ -383,7 +433,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)
@@ -392,13 +442,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)
@@ -436,10 +501,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 function """