55python -m arcade.examples.template_platformer
66"""
77import arcade
8+ from arcade .types import Color
89
910# --- Constants
1011SCREEN_TITLE = "Platformer"
11-
1212SCREEN_WIDTH = 1280
1313SCREEN_HEIGHT = 720
1414
@@ -31,28 +31,27 @@ class MyGame(arcade.Window):
3131 """
3232
3333 def __init__ (self ):
34-
3534 # Call the parent class and set up the window
36- super ().__init__ (SCREEN_WIDTH , SCREEN_HEIGHT ,
37- SCREEN_TITLE , resizable = True )
35+ super ().__init__ (SCREEN_WIDTH , SCREEN_HEIGHT , SCREEN_TITLE , resizable = True )
3836
39- # Our TileMap Object
40- self .tile_map = None
37+ self .scene = self .create_scene ()
4138
42- # Our Scene Object
43- self .scene = None
44-
45- # Separate variable that holds the player sprite
46- self . player_sprite = None
39+ # Set up the player, specifically placing it at these coordinates.
40+ self .player_sprite = arcade . Sprite (
41+ ":resources:images/animated_characters/female_adventurer/femaleAdventurer_idle.png" ,
42+ scale = CHARACTER_SCALING ,
43+ )
4744
4845 # Our physics engine
49- self .physics_engine = None
46+ self .physics_engine = arcade .PhysicsEnginePlatformer (
47+ self .player_sprite , gravity_constant = GRAVITY , walls = self .scene ["Platforms" ]
48+ )
5049
5150 # A Camera that can be used for scrolling the screen
52- self .camera_sprites = None
51+ self .camera_sprites = arcade . camera . Camera2D ()
5352
5453 # A non-scrolling camera that can be used to draw GUI elements
55- self .camera_gui = None
54+ self .camera_gui = arcade . camera . Camera2D ()
5655
5756 # Keep track of the score
5857 self .score = 0
@@ -61,75 +60,59 @@ def __init__(self):
6160 self .left_key_down = False
6261 self .right_key_down = False
6362
64- def setup (self ):
65- """Set up the game here. Call this function to restart the game."""
66-
67- # Setup the Cameras
68- self .camera_sprites = arcade .camera .Camera2D ()
69- self .camera_gui = arcade .camera .Camera2D ()
70-
71- # Name of map file to load
72- map_name = ":resources:tiled_maps/map.json"
63+ # Text object to display the score
64+ self .score_display = arcade .Text ("Score: 0" , x = 10 , y = 10 , color = arcade .csscolor .WHITE , font_size = 18 )
7365
66+ def create_scene (self ) -> arcade .Scene :
67+ """Load the tilemap and create the scene object."""
68+ # Our TileMap Object
7469 # Layer specific options are defined based on Layer names in a dictionary
7570 # Doing this will make the SpriteList for the platforms layer
76- # use spatial hashing for detection.
71+ # use spatial hashing for collision detection.
7772 layer_options = {
7873 "Platforms" : {
7974 "use_spatial_hash" : True ,
8075 },
8176 }
77+ tile_map = arcade .load_tilemap ( ":resources:tiled_maps/map.json" , TILE_SCALING , layer_options )
8278
83- # Read in the tiled map
84- self .tile_map = arcade .load_tilemap (map_name , TILE_SCALING , layer_options )
79+ # Set the window background color to the same as the map if it has one
80+ if tile_map .background_color :
81+ self .background_color = Color .from_iterable (tile_map .background_color )
8582
83+ # Our Scene Object
8684 # Initialize Scene with our TileMap, this will automatically add all layers
8785 # from the map as SpriteLists in the scene in the proper order.
88- self . scene = arcade .Scene .from_tilemap (self . tile_map )
86+ return arcade .Scene .from_tilemap (tile_map )
8987
90- # Set the background color
91- if self .tile_map .background_color :
92- self .background_color = self .tile_map .background_color
93-
94- # Keep track of the score
88+ def reset (self ):
89+ """Reset the game to the initial state."""
9590 self .score = 0
91+ # Load a fresh scene to get the coins back
92+ self .scene = self .create_scene ()
9693
97- # Set up the player, specifically placing it at these coordinates.
98- src = ":resources:images/animated_characters/female_adventurer/femaleAdventurer_idle.png"
99- self .player_sprite = arcade .Sprite (src , scale = CHARACTER_SCALING )
100- self .player_sprite .center_x = 128
101- self .player_sprite .center_y = 128
94+ # Move the player to start position
95+ self .player_sprite .position = (128 , 128 )
96+ # Add the player to the scene
10297 self .scene .add_sprite ("Player" , self .player_sprite )
10398
104- # --- Other stuff
105- # Create the 'physics engine'
106- self .physics_engine = arcade .PhysicsEnginePlatformer (
107- self .player_sprite , gravity_constant = GRAVITY , walls = self .scene ["Platforms" ]
108- )
109-
11099 def on_draw (self ):
111100 """Render the screen."""
112101
113102 # Clear the screen to the background color
114103 self .clear ()
115104
116- # Activate the game camera
117- self .camera_sprites .use ()
118-
119- # Draw our Scene
120- # Note, if you a want pixelated look, add pixelated=True to the parameters
121- self .scene .draw ()
122-
123- # Activate the GUI camera before drawing GUI elements
124- self .camera_gui .use ()
105+ # Draw the map into the sprite camera
106+ with self .camera_sprites .activate ():
107+ # Draw our Scene
108+ # Note, if you a want pixelated look, add pixelated=True to the parameters
109+ self .scene .draw ()
125110
126- # Draw our score on the screen, scrolling it with the viewport
127- score_text = f"Score: { self .score } "
128- arcade .draw_text (score_text ,
129- x = 10 ,
130- y = 10 ,
131- color = arcade .csscolor .WHITE ,
132- font_size = 18 )
111+ # Draw the score into the gui camera
112+ with self .camera_gui .activate ():
113+ # Draw our score on the screen. The camera keeps it in place.
114+ self .score_display .text = f"Score: { self .score } "
115+ self .score_display .draw ()
133116
134117 def update_player_speed (self ):
135118
@@ -174,16 +157,16 @@ def center_camera_to_player(self):
174157 screen_center_y = self .player_sprite .center_y
175158
176159 # Set some limits on how far we scroll
177- if screen_center_x - self .width / 2 < 0 :
178- screen_center_x = self .width / 2
179- if screen_center_y - self .height / 2 < 0 :
180- screen_center_y = self .height / 2
160+ if screen_center_x - self .width / 2 < 0 :
161+ screen_center_x = self .width / 2
162+ if screen_center_y - self .height / 2 < 0 :
163+ screen_center_y = self .height / 2
181164
182165 # Here's our center, move to it
183166 player_centered = screen_center_x , screen_center_y
184167 self .camera_sprites .position = arcade .math .lerp_2d (self .camera_sprites .position , player_centered , 0.1 )
185168
186- def on_update (self , delta_time ):
169+ def on_update (self , delta_time : float ):
187170 """Movement and game logic"""
188171
189172 # Move the player with the physics engine
@@ -207,14 +190,15 @@ def on_update(self, delta_time):
207190 def on_resize (self , width : int , height : int ):
208191 """ Resize window """
209192 super ().on_resize (width , height )
193+ # Update the cameras to match the new window size
210194 self .camera_sprites .match_screen (and_projection = True )
211195 self .camera_gui .match_screen (and_projection = True )
212196
213197
214198def main ():
215199 """Main function"""
216200 window = MyGame ()
217- window .setup ()
201+ window .reset ()
218202 arcade .run ()
219203
220204
0 commit comments