pymunk_demo_platformer_04.py Full Listing

pymunk_demo_platformer_04.py
  1"""
  2Example of Pymunk Physics Engine Platformer
  3"""
  4
  5import math
  6from typing import Optional
  7import arcade
  8
  9SCREEN_TITLE = "PyMunk Platformer"
 10
 11# How big are our image tiles?
 12SPRITE_IMAGE_SIZE = 128
 13
 14# Scale sprites up or down
 15SPRITE_SCALING_PLAYER = 0.5
 16SPRITE_SCALING_TILES = 0.5
 17
 18# Scaled sprite size for tiles
 19SPRITE_SIZE = int(SPRITE_IMAGE_SIZE * SPRITE_SCALING_PLAYER)
 20
 21# Size of grid to show on screen, in number of tiles
 22SCREEN_GRID_WIDTH = 25
 23SCREEN_GRID_HEIGHT = 15
 24
 25# Size of screen to show, in pixels
 26SCREEN_WIDTH = SPRITE_SIZE * SCREEN_GRID_WIDTH
 27SCREEN_HEIGHT = SPRITE_SIZE * SCREEN_GRID_HEIGHT
 28
 29
 30class GameWindow(arcade.Window):
 31    """Main Window"""
 32
 33    def __init__(self, width, height, title):
 34        """Create the variables"""
 35
 36        # Init the parent class
 37        super().__init__(width, height, title)
 38
 39        # Player sprite
 40        self.player_sprite: arcade.Sprite | None = None
 41
 42        # Sprite lists we need
 43        self.player_list: arcade.SpriteList | None = None
 44        self.wall_list: arcade.SpriteList | None = None
 45        self.bullet_list: arcade.SpriteList | None = None
 46        self.item_list: arcade.SpriteList | None = None
 47
 48        # Track the current state of what key is pressed
 49        self.left_pressed: bool = False
 50        self.right_pressed: bool = False
 51
 52        # Set background color
 53        self.background_color = arcade.color.AMAZON
 54
 55    def setup(self):
 56        """Set up everything with the game"""
 57
 58        # Create the sprite lists
 59        self.player_list = arcade.SpriteList()
 60        self.bullet_list = arcade.SpriteList()
 61
 62        # Map name
 63        map_name = ":resources:/tiled_maps/pymunk_test_map.json"
 64
 65        # Load in TileMap
 66        tile_map = arcade.load_tilemap(map_name, SPRITE_SCALING_TILES)
 67
 68        # Pull the sprite layers out of the tile map
 69        self.wall_list = tile_map.sprite_lists["Platforms"]
 70        self.item_list = tile_map.sprite_lists["Dynamic Items"]
 71
 72        # Create player sprite
 73        self.player_sprite = arcade.Sprite(
 74            ":resources:images/animated_characters/female_person/femalePerson_idle.png",
 75            SPRITE_SCALING_PLAYER,
 76        )
 77        # Set player location
 78        grid_x = 1
 79        grid_y = 1
 80        self.player_sprite.center_x = SPRITE_SIZE * grid_x + SPRITE_SIZE / 2
 81        self.player_sprite.center_y = SPRITE_SIZE * grid_y + SPRITE_SIZE / 2
 82        # Add to player sprite list
 83        self.player_list.append(self.player_sprite)
 84
 85    def on_key_press(self, key, modifiers):
 86        """Called whenever a key is pressed."""
 87        pass
 88
 89    def on_key_release(self, key, modifiers):
 90        """Called when the user releases a key."""
 91        pass
 92
 93    def on_update(self, delta_time):
 94        """Movement and game logic"""
 95        pass
 96
 97    def on_draw(self):
 98        """Draw everything"""
 99        self.clear()
100        self.wall_list.draw()
101        self.bullet_list.draw()
102        self.item_list.draw()
103        self.player_list.draw()
104
105
106def main():
107    """Main function"""
108    window = GameWindow(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
109    window.setup()
110    arcade.run()
111
112
113if __name__ == "__main__":
114    main()