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