pymunk_demo_platformer_04.py Full Listing#

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