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        arcade.set_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        # Read in the tiled map
 62        map_name = "pymunk_test_map.tmx"
 63        my_map = arcade.tilemap.read_tmx(map_name)
 64
 65        # Read in the map layers
 66        self.wall_list = arcade.tilemap.process_layer(my_map, 'Platforms', SPRITE_SCALING_TILES)
 67        self.item_list = arcade.tilemap.process_layer(my_map, 'Dynamic Items', SPRITE_SCALING_TILES)
 68
 69        # Create player sprite
 70        self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png",
 71                                           SPRITE_SCALING_PLAYER)
 72        # Set player location
 73        grid_x = 1
 74        grid_y = 1
 75        self.player_sprite.center_x = SPRITE_SIZE * grid_x + SPRITE_SIZE / 2
 76        self.player_sprite.center_y = SPRITE_SIZE * grid_y + SPRITE_SIZE / 2
 77        # Add to player sprite list
 78        self.player_list.append(self.player_sprite)
 79
 80    def on_key_press(self, key, modifiers):
 81        """Called whenever a key is pressed. """
 82        pass
 83
 84    def on_key_release(self, key, modifiers):
 85        """Called when the user releases a key. """
 86        pass
 87
 88    def on_update(self, delta_time):
 89        """ Movement and game logic """
 90        pass
 91
 92    def on_draw(self):
 93        """ Draw everything """
 94        arcade.start_render()
 95        self.wall_list.draw()
 96        self.bullet_list.draw()
 97        self.item_list.draw()
 98        self.player_list.draw()
 99
100def main():
101    """ Main method """
102    window = GameWindow(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
103    window.setup()
104    arcade.run()
105
106
107if __name__ == "__main__":
108    main()