Skip to content

Commit 0d4b5b6

Browse files
authored
More example tweaks (#2212)
* More example tweaks * template tweaks
1 parent 4ac28d5 commit 0d4b5b6

9 files changed

Lines changed: 59 additions & 79 deletions

arcade/examples/stars.jpg

-373 KB
Binary file not shown.

arcade/examples/starting_template.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def __init__(self, width, height, title):
3333

3434
def reset(self):
3535
"""Reset the game to the initial state."""
36-
# Create your sprites and sprite lists here
36+
# Do changes needed to restart the game here if you want to support that
3737
pass
3838

3939
def on_draw(self):
@@ -91,7 +91,8 @@ def on_mouse_release(self, x, y, button, key_modifiers):
9191

9292
def main():
9393
""" Main function """
94-
MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE).run()
94+
game = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
95+
game.run()
9596

9697

9798
if __name__ == "__main__":

arcade/examples/template_platformer.py

Lines changed: 49 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
python -m arcade.examples.template_platformer
66
"""
77
import arcade
8+
from arcade.types import Color
89

910
# --- Constants
1011
SCREEN_TITLE = "Platformer"
11-
1212
SCREEN_WIDTH = 1280
1313
SCREEN_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

214198
def main():
215199
"""Main function"""
216200
window = MyGame()
217-
window.setup()
201+
window.reset()
218202
arcade.run()
219203

220204

arcade/examples/text_loc_example_done.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Example showing how to draw text to the screen.
2+
Example showing how to draw localized text to the screen.
33
44
If Python and Arcade are installed, this example can be run from the command line with:
55
python -m arcade.examples.text_loc_example_done

arcade/examples/text_loc_example_start.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Example showing how to draw text to the screen.
2+
Example showing how to draw localized text to the screen.
33
44
If Python and Arcade are installed, this example can be run from the command line with:
55
python -m arcade.examples.text_loc_example_start

arcade/examples/timer.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,12 @@ def __init__(self):
2727
font_size=100,
2828
anchor_x="center",
2929
)
30-
31-
def setup(self):
32-
"""
33-
Set up the application.
34-
"""
3530
self.background_color = arcade.color.ALABAMA_CRIMSON
3631
self.total_time = 0.0
3732

33+
def reset(self):
34+
self.total_time = 0.0
35+
3836
def on_draw(self):
3937
""" Use this function to draw everything to the screen. """
4038
# Clear all pixels in the window
@@ -65,7 +63,7 @@ def on_update(self, delta_time):
6563

6664
def main():
6765
window = MyGame()
68-
window.setup()
66+
window.reset()
6967
arcade.run()
7068

7169

arcade/examples/transform_feedback.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,13 @@ def __init__(self, width, height, title):
112112

113113
self.ctx.enable_only() # Ensure no context flags are set
114114

115-
116115
def gen_initial_data(self, count):
117116
for _ in range(count):
118117
yield random.uniform(-1.2, 1.2) # pos x
119118
yield random.uniform(-1.2, 1.2) # pos y
120119
yield random.uniform(-.3, .3) # velocity x
121120
yield random.uniform(-.3, .3) # velocity y
122121

123-
124122
def on_draw(self):
125123
self.clear()
126124
self.ctx.point_size = 2 * self.get_pixel_ratio()

arcade/examples/turn_and_move.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ def on_mouse_press(self, x, y, button, key_modifiers):
150150
def main():
151151
""" Main function """
152152
game = MyGame()
153-
game.center_window()
154153
game.setup()
155154
arcade.run()
156155

arcade/examples/view_instructions_and_game_over.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def on_mouse_press(self, _x, _y, _button, _modifiers):
4444

4545
class InstructionView(arcade.View):
4646
def on_show_view(self):
47-
self.window.background = arcade.color.ORANGE_PEEL
47+
self.window.background_color = arcade.color.ORANGE_PEEL
4848

4949
def on_draw(self):
5050
self.clear()

0 commit comments

Comments
 (0)