04_add_gravity.py Full Listing

04_add_gravity.py
  1"""
  2Platformer Game
  3"""
  4import arcade
  5
  6# Constants
  7SCREEN_WIDTH = 1000
  8SCREEN_HEIGHT = 650
  9SCREEN_TITLE = "Platformer"
 10
 11# Constants used to scale our sprites from their original size
 12CHARACTER_SCALING = 1
 13TILE_SCALING = 0.5
 14COIN_SCALING = 0.5
 15
 16# Movement speed of player, in pixels per frame
 17PLAYER_MOVEMENT_SPEED = 5
 18GRAVITY = 1
 19PLAYER_JUMP_SPEED = 20
 20
 21
 22class MyGame(arcade.Window):
 23    """
 24    Main application class.
 25    """
 26
 27    def __init__(self):
 28
 29        # Call the parent class and set up the window
 30        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
 31
 32        # These are 'lists' that keep track of our sprites. Each sprite should
 33        # go into a list.
 34        self.coin_list = None
 35        self.wall_list = None
 36        self.player_list = None
 37
 38        # Separate variable that holds the player sprite
 39        self.player_sprite = None
 40
 41        # Our physics engine
 42        self.physics_engine = None
 43
 44        arcade.set_background_color(arcade.csscolor.CORNFLOWER_BLUE)
 45
 46    def setup(self):
 47        """ Set up the game here. Call this function to restart the game. """
 48        # Create the Sprite lists
 49        self.player_list = arcade.SpriteList()
 50        self.wall_list = arcade.SpriteList(use_spatial_hash=True)
 51        self.coin_list = arcade.SpriteList(use_spatial_hash=True)
 52
 53        # Set up the player, specifically placing it at these coordinates.
 54        image_source = ":resources:images/animated_characters/female_adventurer/femaleAdventurer_idle.png"
 55        self.player_sprite = arcade.Sprite(image_source, CHARACTER_SCALING)
 56        self.player_sprite.center_x = 64
 57        self.player_sprite.center_y = 128
 58        self.player_list.append(self.player_sprite)
 59
 60        # Create the ground
 61        # This shows using a loop to place multiple sprites horizontally
 62        for x in range(0, 1250, 64):
 63            wall = arcade.Sprite(":resources:images/tiles/grassMid.png", TILE_SCALING)
 64            wall.center_x = x
 65            wall.center_y = 32
 66            self.wall_list.append(wall)
 67
 68        # Put some crates on the ground
 69        # This shows using a coordinate list to place sprites
 70        coordinate_list = [[512, 96],
 71                           [256, 96],
 72                           [768, 96]]
 73
 74        for coordinate in coordinate_list:
 75            # Add a crate on the ground
 76            wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png", TILE_SCALING)
 77            wall.position = coordinate
 78            self.wall_list.append(wall)
 79
 80        # Create the 'physics engine'
 81        self.physics_engine = arcade.PhysicsEnginePlatformer(self.player_sprite,
 82                                                             self.wall_list,
 83                                                             GRAVITY)
 84
 85    def on_draw(self):
 86        """ Render the screen. """
 87
 88        # Clear the screen to the background color
 89        arcade.start_render()
 90
 91        # Draw our sprites
 92        self.wall_list.draw()
 93        self.coin_list.draw()
 94        self.player_list.draw()
 95
 96    def on_key_press(self, key, modifiers):
 97        """Called whenever a key is pressed. """
 98
 99        if key == arcade.key.UP or key == arcade.key.W:
100            if self.physics_engine.can_jump():
101                self.player_sprite.change_y = PLAYER_JUMP_SPEED
102        elif key == arcade.key.LEFT or key == arcade.key.A:
103            self.player_sprite.change_x = -PLAYER_MOVEMENT_SPEED
104        elif key == arcade.key.RIGHT or key == arcade.key.D:
105            self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED
106
107    def on_key_release(self, key, modifiers):
108        """Called when the user releases a key. """
109
110        if key == arcade.key.LEFT or key == arcade.key.A:
111            self.player_sprite.change_x = 0
112        elif key == arcade.key.RIGHT or key == arcade.key.D:
113            self.player_sprite.change_x = 0
114
115    def on_update(self, delta_time):
116        """ Movement and game logic """
117
118        # Move the player with the physics engine
119        self.physics_engine.update()
120
121
122def main():
123    """ Main method """
124    window = MyGame()
125    window.setup()
126    arcade.run()
127
128
129if __name__ == "__main__":
130    main()