Skip to content

Commit 9008a7e

Browse files
authored
Merge pull request #102 from realpython/arcade-platformer
Code to support Arcade Platformer article
2 parents 0b39a95 + 3303211 commit 9008a7e

File tree

347 files changed

+8449
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

347 files changed

+8449
-0
lines changed

arcade-platformer/.directory

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[Dolphin]
2+
Timestamp=2020,3,2,17,37,38
3+
Version=4
4+
ViewMode=1

arcade-platformer/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Arcade Platformer
2+
3+
Built to support the Arcade Platfomer article at https://realpython.com
4+
5+
Assets downloaded from:
6+
7+
- Tileset: https://opengameart.org/content/platformer-pack-redux-360-assets
8+
- Sounds: https://opengameart.org/content/63-digital-sound-effects-lasers-phasers-space-etc and https://opengameart.org/content/51-ui-sound-effects-buttons-switches-and-clicks
9+
- Credit for all assets: https://www.kenney.nl. Donations are suggested.
10+
11+
Tilemaps created with https://www.mapeditor.org/, donation suggested.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 from www.kenney.nl
8+
Game sounds and tile maps by author
9+
"""
10+
11+
import arcade
12+
13+
14+
class Platformer(arcade.Window):
15+
def __init__(self):
16+
pass
17+
18+
def setup(self):
19+
"""Sets up the game for the current level"""
20+
pass
21+
22+
def on_key_press(self, key: int, modifiers: int):
23+
"""Processes key presses
24+
25+
Arguments:
26+
key {int} -- Which key was pressed
27+
modifiers {int} -- Which modifiers were down at the time
28+
"""
29+
30+
def on_key_release(self, key: int, modifiers: int):
31+
"""Processes key releases
32+
33+
Arguments:
34+
key {int} -- Which key was released
35+
modifiers {int} -- Which modifiers were down at the time
36+
"""
37+
38+
def on_update(self, delta_time: float):
39+
"""Updates the position of all game objects
40+
41+
Arguments:
42+
delta_time {float} -- How much time since the last call
43+
"""
44+
pass
45+
46+
def on_draw(self):
47+
pass
48+
49+
50+
if __name__ == "__main__":
51+
window = Platformer()
52+
window.setup()
53+
arcade.run()
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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 from www.kenney.nl
8+
Game sounds and tile maps by author
9+
"""
10+
11+
import arcade
12+
import pathlib
13+
14+
# Game constants
15+
# Window dimensions
16+
SCREEN_WIDTH = 1000
17+
SCREEN_HEIGHT = 650
18+
SCREEN_TITLE = "Arcade Platformer"
19+
20+
# Assets path
21+
ASSETS_PATH = pathlib.Path(__file__).resolve().parent.parent / "assets"
22+
23+
24+
class Platformer(arcade.Window):
25+
def __init__(self) -> None:
26+
super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
27+
28+
# These lists will hold different sets of sprites
29+
self.coins = None
30+
self.background = None
31+
self.walls = None
32+
self.ladders = None
33+
self.goals = None
34+
self.enemies = None
35+
36+
# One sprite for the player, no more is needed
37+
self.player = None
38+
39+
# We need a physics engine as well
40+
self.physics_engine = None
41+
42+
# Someplace to keep score
43+
self.score = 0
44+
45+
# Which level are we on?
46+
self.level = 1
47+
48+
# Load up our sounds here
49+
self.coin_sound = arcade.load_sound(
50+
str(ASSETS_PATH / "sounds" / "coin.wav")
51+
)
52+
self.jump_sound = arcade.load_sound(
53+
str(ASSETS_PATH / "sounds" / "jump.wav")
54+
)
55+
self.victory_sound = arcade.load_sound(
56+
str(ASSETS_PATH / "sounds" / "victory.wav")
57+
)
58+
59+
def setup(self):
60+
"""Sets up the game for the current level"""
61+
pass
62+
63+
def on_key_press(self, key: int, modifiers: int):
64+
"""Arguments:
65+
key {int} -- Which key was pressed
66+
modifiers {int} -- Which modifiers were down at the time
67+
"""
68+
69+
def on_key_release(self, key: int, modifiers: int):
70+
"""Arguments:
71+
key {int} -- Which key was released
72+
modifiers {int} -- Which modifiers were down at the time
73+
"""
74+
75+
def on_update(self, delta_time: float):
76+
"""Updates the position of all game objects
77+
78+
Arguments:
79+
delta_time {float} -- How much time since the last call
80+
"""
81+
pass
82+
83+
def on_draw(self):
84+
pass
85+
86+
87+
if __name__ == "__main__":
88+
window = Platformer()
89+
window.setup()
90+
arcade.run()
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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 from www.kenney.nl
8+
Game sounds and tile maps by author
9+
"""
10+
11+
import arcade
12+
import pathlib
13+
14+
# Game constants
15+
# Window dimensions
16+
SCREEN_WIDTH = 1000
17+
SCREEN_HEIGHT = 650
18+
SCREEN_TITLE = "Arcade Platformer"
19+
20+
# Scaling Constants
21+
MAP_SCALING = 1.0
22+
23+
# Player constants
24+
GRAVITY = 1.0
25+
PLAYER_START_X = 65
26+
PLAYER_START_Y = 256
27+
28+
# Assets path
29+
ASSETS_PATH = pathlib.Path(__file__).resolve().parent.parent / "assets"
30+
31+
32+
class Platformer(arcade.Window):
33+
def __init__(self) -> None:
34+
super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
35+
36+
# These lists will hold different sets of sprites
37+
self.coins = None
38+
self.background = None
39+
self.walls = None
40+
self.ladders = None
41+
self.goals = None
42+
self.enemies = None
43+
44+
# One sprite for the player, no more is needed
45+
self.player = None
46+
47+
# We need a physics engine as well
48+
self.physics_engine = None
49+
50+
# Someplace to keep score
51+
self.score = 0
52+
53+
# Which level are we on?
54+
self.level = 1
55+
56+
# Load up our sounds here
57+
self.coin_sound = arcade.load_sound(
58+
str(ASSETS_PATH / "sounds" / "coin.wav")
59+
)
60+
self.jump_sound = arcade.load_sound(
61+
str(ASSETS_PATH / "sounds" / "jump.wav")
62+
)
63+
self.victory_sound = arcade.load_sound(
64+
str(ASSETS_PATH / "sounds" / "victory.wav")
65+
)
66+
67+
def setup(self) -> None:
68+
"""Sets up the game for the current level"""
69+
70+
# Get the current map based on the level
71+
map_name = f"platform_level_{self.level:02}.tmx"
72+
map_path = ASSETS_PATH / map_name
73+
74+
# What are the names of the layers?
75+
wall_layer = "ground"
76+
coin_layer = "coins"
77+
goal_layer = "goal"
78+
background_layer = "background"
79+
ladders_layer = "ladders"
80+
81+
# Load the current map
82+
game_map = arcade.tilemap.read_tmx(str(map_path))
83+
84+
# Load the layers
85+
self.background = arcade.tilemap.process_layer(
86+
game_map, layer_name=background_layer, scaling=MAP_SCALING
87+
)
88+
self.goals = arcade.tilemap.process_layer(
89+
game_map, layer_name=goal_layer, scaling=MAP_SCALING
90+
)
91+
self.walls = arcade.tilemap.process_layer(
92+
game_map, layer_name=wall_layer, scaling=MAP_SCALING
93+
)
94+
self.ladders = arcade.tilemap.process_layer(
95+
game_map, layer_name=ladders_layer, scaling=MAP_SCALING
96+
)
97+
self.coins = arcade.tilemap.process_layer(
98+
game_map, layer_name=coin_layer, scaling=MAP_SCALING
99+
)
100+
101+
# Set the background color
102+
background_color = arcade.color.FRESH_AIR
103+
if game_map.background_color:
104+
background_color = game_map.background_color
105+
arcade.set_background_color(background_color)
106+
107+
# Create the player sprite, if they're not already setup
108+
if not self.player:
109+
self.player = self.create_player_sprite()
110+
111+
# Move the player sprite back to the beginning
112+
self.player.center_x = PLAYER_START_X
113+
self.player.center_y = PLAYER_START_Y
114+
self.player.change_x = 0
115+
self.player.change_y = 0
116+
117+
# Load the physics engine for this map
118+
self.physics_engine = arcade.PhysicsEnginePlatformer(
119+
player_sprite=self.player,
120+
platforms=self.walls,
121+
gravity_constant=GRAVITY,
122+
ladders=self.ladders,
123+
)
124+
125+
def on_key_press(self, key: int, modifiers: int):
126+
"""Arguments:
127+
key {int} -- Which key was pressed
128+
modifiers {int} -- Which modifiers were down at the time
129+
"""
130+
131+
def on_key_release(self, key: int, modifiers: int):
132+
"""Arguments:
133+
key {int} -- Which key was released
134+
modifiers {int} -- Which modifiers were down at the time
135+
"""
136+
137+
def on_update(self, delta_time: float):
138+
"""Updates the position of all game objects
139+
140+
Arguments:
141+
delta_time {float} -- How much time since the last call
142+
"""
143+
pass
144+
145+
def on_draw(self):
146+
pass
147+
148+
149+
if __name__ == "__main__":
150+
window = Platformer()
151+
window.setup()
152+
arcade.run()

0 commit comments

Comments
 (0)