|
| 1 | +# |
| 2 | +# Arcade Platformer |
| 3 | +# |
| 4 | +# Demonstrating the capbilities of arcade in a platformer game |
| 5 | +# Supporting the Arcade Platformer article on https://realpython.com |
| 6 | +# |
| 7 | +# All game artwork and sounds, except the tile map and victory sound, |
| 8 | +# from www.kenney.nl |
| 9 | + |
| 10 | + |
| 11 | +# Import libraries |
| 12 | +import arcade |
| 13 | +import pathlib |
| 14 | + |
| 15 | +# Game constants |
| 16 | +# Window dimensions |
| 17 | +SCREEN_WIDTH = 1000 |
| 18 | +SCREEN_HEIGHT = 650 |
| 19 | +SCREEN_TITLE = "Arcade Platformer" |
| 20 | + |
| 21 | +# Scaling Constants |
| 22 | +MAP_SCALING = 1.0 |
| 23 | + |
| 24 | +# Player constants |
| 25 | +GRAVITY = 1.0 |
| 26 | +PLAYER_START_X = 65 |
| 27 | +PLAYER_START_Y = 256 |
| 28 | + |
| 29 | +# Assets path |
| 30 | +ASSETS_PATH = pathlib.Path(__file__).resolve().parent.parent / "assets" |
| 31 | + |
| 32 | + |
| 33 | +# Classes |
| 34 | +class Platformer(arcade.Window): |
| 35 | + """PlatformerView class. Derived from arcade.View, provides all functionality |
| 36 | + from arcade.Window, plus managing different views for our game. |
| 37 | + """ |
| 38 | + |
| 39 | + def __init__(self) -> None: |
| 40 | + """Create the game view""" |
| 41 | + # First initialize the parent |
| 42 | + super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE) |
| 43 | + |
| 44 | + # These lists will hold different sets of sprites |
| 45 | + self.coins_list = None |
| 46 | + self.background_list = None |
| 47 | + self.walls_list = None |
| 48 | + self.ladders_list = None |
| 49 | + self.goals_list = None |
| 50 | + self.enemies_list = None |
| 51 | + |
| 52 | + # One sprite for the player, no more is needed |
| 53 | + self.player = None |
| 54 | + |
| 55 | + # We need a physics engine as well |
| 56 | + self.physics_engine = None |
| 57 | + |
| 58 | + # Someplace to keep score |
| 59 | + self.score = 0 |
| 60 | + |
| 61 | + # Which level are we on? |
| 62 | + self.level = 1 |
| 63 | + |
| 64 | + # Load up our sounds here |
| 65 | + self.coin_sound = arcade.load_sound( |
| 66 | + str(ASSETS_PATH / "sounds" / "coin.wav") |
| 67 | + ) |
| 68 | + self.jump_sound = arcade.load_sound( |
| 69 | + str(ASSETS_PATH / "sounds" / "jump.wav") |
| 70 | + ) |
| 71 | + self.victory_sound = arcade.load_sound( |
| 72 | + str(ASSETS_PATH / "sounds" / "victory.wav") |
| 73 | + ) |
| 74 | + |
| 75 | + def setup(self) -> None: |
| 76 | + """Sets up the game for the current level""" |
| 77 | + |
| 78 | + # Get the current map based on the level |
| 79 | + map_name = f"platform_level_{self.level:02}.tmx" |
| 80 | + map_path = ASSETS_PATH / map_name |
| 81 | + |
| 82 | + # What are the names of the layers? |
| 83 | + wall_layer = "ground" |
| 84 | + coin_layer = "coins" |
| 85 | + goal_layer = "goal" |
| 86 | + background_layer = "background" |
| 87 | + ladders_layer = "ladders" |
| 88 | + |
| 89 | + # Load the current map |
| 90 | + map = arcade.tilemap.read_tmx(str(map_path)) |
| 91 | + |
| 92 | + # Load the layers |
| 93 | + self.background_list = arcade.tilemap.process_layer( |
| 94 | + map, layer_name=background_layer, scaling=MAP_SCALING |
| 95 | + ) |
| 96 | + self.goals_list = arcade.tilemap.process_layer( |
| 97 | + map, layer_name=goal_layer, scaling=MAP_SCALING |
| 98 | + ) |
| 99 | + self.walls_list = arcade.tilemap.process_layer( |
| 100 | + map, layer_name=wall_layer, scaling=MAP_SCALING |
| 101 | + ) |
| 102 | + self.ladders_list = arcade.tilemap.process_layer( |
| 103 | + map, layer_name=ladders_layer, scaling=MAP_SCALING |
| 104 | + ) |
| 105 | + self.coins_list = arcade.tilemap.process_layer( |
| 106 | + map, layer_name=coin_layer, scaling=MAP_SCALING |
| 107 | + ) |
| 108 | + |
| 109 | + # Set the background color |
| 110 | + background_color = arcade.color.AERO_BLUE |
| 111 | + if map.background_color: |
| 112 | + background_color = map.background_color |
| 113 | + arcade.set_background_color(background_color) |
| 114 | + |
| 115 | + # Create the player sprite, if they're not already setup |
| 116 | + if not self.player: |
| 117 | + self.player = self.create_player_sprite() |
| 118 | + |
| 119 | + # If we have a player sprite, we need to move it back to the beginning |
| 120 | + self.player.center_x = PLAYER_START_X |
| 121 | + self.player.center_y = PLAYER_START_Y |
| 122 | + self.player.change_x = 0 |
| 123 | + self.player.change_y = 0 |
| 124 | + |
| 125 | + # Load the physics engine for this map |
| 126 | + self.physics_engine = arcade.PhysicsEnginePlatformer( |
| 127 | + player_sprite=self.player, |
| 128 | + platforms=self.walls_list, |
| 129 | + gravity_constant=GRAVITY, |
| 130 | + ladders=self.ladders_list, |
| 131 | + ) |
| 132 | + |
| 133 | + def on_key_press(self, key: int, modifiers: int): |
| 134 | + """Processes key presses |
| 135 | + |
| 136 | + Arguments: |
| 137 | + key {int} -- Which key was pressed |
| 138 | + modifiers {int} -- Which modifiers were down at the time |
| 139 | + """ |
| 140 | + |
| 141 | + def on_key_release(self, key: int, modifiers: int): |
| 142 | + """Processes key releases |
| 143 | + |
| 144 | + Arguments: |
| 145 | + key {int} -- Which key was released |
| 146 | + modifiers {int} -- Which modifiers were down at the time |
| 147 | + """ |
| 148 | + |
| 149 | + def on_update(self, delta_time: float): |
| 150 | + """Updates the position of all screen objects |
| 151 | + |
| 152 | + Arguments: |
| 153 | + delta_time {float} -- How much time since the last call |
| 154 | + """ |
| 155 | + pass |
| 156 | + |
| 157 | + def on_draw(self): |
| 158 | + """Draws everything |
| 159 | + """ |
| 160 | + pass |
| 161 | + |
| 162 | + |
| 163 | +# Main |
| 164 | +# Main |
| 165 | +if __name__ == "__main__": |
| 166 | + window = Platformer() |
| 167 | + window.setup() |
| 168 | + arcade.run() |
0 commit comments