Skip to content

Commit d60f319

Browse files
committed
Reworked and refactored example code
1 parent 33a4d1c commit d60f319

File tree

10 files changed

+1235
-1337
lines changed

10 files changed

+1235
-1337
lines changed

arcade-platformer/arcade_platformer/01_game_outline.py renamed to arcade-platformer/arcade_platformer/01_game_skeleton.py

File renamed without changes.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
# Assets path
22+
ASSETS_PATH = pathlib.Path(__file__).resolve().parent.parent / "assets"
23+
24+
25+
# Classes
26+
class Platformer(arcade.Window):
27+
"""PlatformerView class. Derived from arcade.View, provides all functionality
28+
from arcade.Window, plus managing different views for our game.
29+
"""
30+
31+
def __init__(self) -> None:
32+
"""Create the game view"""
33+
# First initialize the parent
34+
super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
35+
36+
# These lists will hold different sets of sprites
37+
self.coins_list = None
38+
self.background_list = None
39+
self.walls_list = None
40+
self.ladders_list = None
41+
self.goals_list = None
42+
self.enemies_list = 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):
68+
"""Sets up the game for the current level
69+
"""
70+
pass
71+
72+
def on_key_press(self, key: int, modifiers: int):
73+
"""Processes key presses
74+
75+
Arguments:
76+
key {int} -- Which key was pressed
77+
modifiers {int} -- Which modifiers were down at the time
78+
"""
79+
80+
def on_key_release(self, key: int, modifiers: int):
81+
"""Processes key releases
82+
83+
Arguments:
84+
key {int} -- Which key was released
85+
modifiers {int} -- Which modifiers were down at the time
86+
"""
87+
88+
def on_update(self, delta_time: float):
89+
"""Updates the position of all screen objects
90+
91+
Arguments:
92+
delta_time {float} -- How much time since the last call
93+
"""
94+
pass
95+
96+
def on_draw(self):
97+
"""Draws everything
98+
"""
99+
pass
100+
101+
102+
# Main
103+
# Main
104+
if __name__ == "__main__":
105+
window = Platformer()
106+
window.setup()
107+
arcade.run()
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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

Comments
 (0)