Skip to content

Commit

Permalink
More example tweaks (#2212)
Browse files Browse the repository at this point in the history
* More example tweaks

* template tweaks
  • Loading branch information
einarf authored Jul 5, 2024
1 parent 4ac28d5 commit 0d4b5b6
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 79 deletions.
Binary file removed arcade/examples/stars.jpg
Binary file not shown.
5 changes: 3 additions & 2 deletions arcade/examples/starting_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(self, width, height, title):

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

def on_draw(self):
Expand Down Expand Up @@ -91,7 +91,8 @@ def on_mouse_release(self, x, y, button, key_modifiers):

def main():
""" Main function """
MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE).run()
game = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
game.run()


if __name__ == "__main__":
Expand Down
114 changes: 49 additions & 65 deletions arcade/examples/template_platformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
python -m arcade.examples.template_platformer
"""
import arcade
from arcade.types import Color

# --- Constants
SCREEN_TITLE = "Platformer"

SCREEN_WIDTH = 1280
SCREEN_HEIGHT = 720

Expand All @@ -31,28 +31,27 @@ class MyGame(arcade.Window):
"""

def __init__(self):

# Call the parent class and set up the window
super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT,
SCREEN_TITLE, resizable=True)
super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE, resizable=True)

# Our TileMap Object
self.tile_map = None
self.scene = self.create_scene()

# Our Scene Object
self.scene = None

# Separate variable that holds the player sprite
self.player_sprite = None
# Set up the player, specifically placing it at these coordinates.
self.player_sprite = arcade.Sprite(
":resources:images/animated_characters/female_adventurer/femaleAdventurer_idle.png",
scale=CHARACTER_SCALING,
)

# Our physics engine
self.physics_engine = None
self.physics_engine = arcade.PhysicsEnginePlatformer(
self.player_sprite, gravity_constant=GRAVITY, walls=self.scene["Platforms"]
)

# A Camera that can be used for scrolling the screen
self.camera_sprites = None
self.camera_sprites = arcade.camera.Camera2D()

# A non-scrolling camera that can be used to draw GUI elements
self.camera_gui = None
self.camera_gui = arcade.camera.Camera2D()

# Keep track of the score
self.score = 0
Expand All @@ -61,75 +60,59 @@ def __init__(self):
self.left_key_down = False
self.right_key_down = False

def setup(self):
"""Set up the game here. Call this function to restart the game."""

# Setup the Cameras
self.camera_sprites = arcade.camera.Camera2D()
self.camera_gui = arcade.camera.Camera2D()

# Name of map file to load
map_name = ":resources:tiled_maps/map.json"
# Text object to display the score
self.score_display = arcade.Text("Score: 0", x=10, y=10, color=arcade.csscolor.WHITE, font_size=18)

def create_scene(self) -> arcade.Scene:
"""Load the tilemap and create the scene object."""
# Our TileMap Object
# Layer specific options are defined based on Layer names in a dictionary
# Doing this will make the SpriteList for the platforms layer
# use spatial hashing for detection.
# use spatial hashing for collision detection.
layer_options = {
"Platforms": {
"use_spatial_hash": True,
},
}
tile_map = arcade.load_tilemap( ":resources:tiled_maps/map.json", TILE_SCALING, layer_options)

# Read in the tiled map
self.tile_map = arcade.load_tilemap(map_name, TILE_SCALING, layer_options)
# Set the window background color to the same as the map if it has one
if tile_map.background_color:
self.background_color = Color.from_iterable(tile_map.background_color)

# Our Scene Object
# Initialize Scene with our TileMap, this will automatically add all layers
# from the map as SpriteLists in the scene in the proper order.
self.scene = arcade.Scene.from_tilemap(self.tile_map)
return arcade.Scene.from_tilemap(tile_map)

# Set the background color
if self.tile_map.background_color:
self.background_color = self.tile_map.background_color

# Keep track of the score
def reset(self):
"""Reset the game to the initial state."""
self.score = 0
# Load a fresh scene to get the coins back
self.scene = self.create_scene()

# Set up the player, specifically placing it at these coordinates.
src = ":resources:images/animated_characters/female_adventurer/femaleAdventurer_idle.png"
self.player_sprite = arcade.Sprite(src, scale=CHARACTER_SCALING)
self.player_sprite.center_x = 128
self.player_sprite.center_y = 128
# Move the player to start position
self.player_sprite.position = (128, 128)
# Add the player to the scene
self.scene.add_sprite("Player", self.player_sprite)

# --- Other stuff
# Create the 'physics engine'
self.physics_engine = arcade.PhysicsEnginePlatformer(
self.player_sprite, gravity_constant=GRAVITY, walls=self.scene["Platforms"]
)

def on_draw(self):
"""Render the screen."""

# Clear the screen to the background color
self.clear()

# Activate the game camera
self.camera_sprites.use()

# Draw our Scene
# Note, if you a want pixelated look, add pixelated=True to the parameters
self.scene.draw()

# Activate the GUI camera before drawing GUI elements
self.camera_gui.use()
# Draw the map into the sprite camera
with self.camera_sprites.activate():
# Draw our Scene
# Note, if you a want pixelated look, add pixelated=True to the parameters
self.scene.draw()

# Draw our score on the screen, scrolling it with the viewport
score_text = f"Score: {self.score}"
arcade.draw_text(score_text,
x=10,
y=10,
color=arcade.csscolor.WHITE,
font_size=18)
# Draw the score into the gui camera
with self.camera_gui.activate():
# Draw our score on the screen. The camera keeps it in place.
self.score_display.text = f"Score: {self.score}"
self.score_display.draw()

def update_player_speed(self):

Expand Down Expand Up @@ -174,16 +157,16 @@ def center_camera_to_player(self):
screen_center_y = self.player_sprite.center_y

# Set some limits on how far we scroll
if screen_center_x - self.width/2 < 0:
screen_center_x = self.width/2
if screen_center_y - self.height/2 < 0:
screen_center_y = self.height/2
if screen_center_x - self.width / 2 < 0:
screen_center_x = self.width / 2
if screen_center_y - self.height / 2 < 0:
screen_center_y = self.height / 2

# Here's our center, move to it
player_centered = screen_center_x, screen_center_y
self.camera_sprites.position = arcade.math.lerp_2d(self.camera_sprites.position, player_centered, 0.1)

def on_update(self, delta_time):
def on_update(self, delta_time: float):
"""Movement and game logic"""

# Move the player with the physics engine
Expand All @@ -207,14 +190,15 @@ def on_update(self, delta_time):
def on_resize(self, width: int, height: int):
""" Resize window """
super().on_resize(width, height)
# Update the cameras to match the new window size
self.camera_sprites.match_screen(and_projection=True)
self.camera_gui.match_screen(and_projection=True)


def main():
"""Main function"""
window = MyGame()
window.setup()
window.reset()
arcade.run()


Expand Down
2 changes: 1 addition & 1 deletion arcade/examples/text_loc_example_done.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Example showing how to draw text to the screen.
Example showing how to draw localized text to the screen.
If Python and Arcade are installed, this example can be run from the command line with:
python -m arcade.examples.text_loc_example_done
Expand Down
2 changes: 1 addition & 1 deletion arcade/examples/text_loc_example_start.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Example showing how to draw text to the screen.
Example showing how to draw localized text to the screen.
If Python and Arcade are installed, this example can be run from the command line with:
python -m arcade.examples.text_loc_example_start
Expand Down
10 changes: 4 additions & 6 deletions arcade/examples/timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@ def __init__(self):
font_size=100,
anchor_x="center",
)

def setup(self):
"""
Set up the application.
"""
self.background_color = arcade.color.ALABAMA_CRIMSON
self.total_time = 0.0

def reset(self):
self.total_time = 0.0

def on_draw(self):
""" Use this function to draw everything to the screen. """
# Clear all pixels in the window
Expand Down Expand Up @@ -65,7 +63,7 @@ def on_update(self, delta_time):

def main():
window = MyGame()
window.setup()
window.reset()
arcade.run()


Expand Down
2 changes: 0 additions & 2 deletions arcade/examples/transform_feedback.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,13 @@ def __init__(self, width, height, title):

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


def gen_initial_data(self, count):
for _ in range(count):
yield random.uniform(-1.2, 1.2) # pos x
yield random.uniform(-1.2, 1.2) # pos y
yield random.uniform(-.3, .3) # velocity x
yield random.uniform(-.3, .3) # velocity y


def on_draw(self):
self.clear()
self.ctx.point_size = 2 * self.get_pixel_ratio()
Expand Down
1 change: 0 additions & 1 deletion arcade/examples/turn_and_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ def on_mouse_press(self, x, y, button, key_modifiers):
def main():
""" Main function """
game = MyGame()
game.center_window()
game.setup()
arcade.run()

Expand Down
2 changes: 1 addition & 1 deletion arcade/examples/view_instructions_and_game_over.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def on_mouse_press(self, _x, _y, _button, _modifiers):

class InstructionView(arcade.View):
def on_show_view(self):
self.window.background = arcade.color.ORANGE_PEEL
self.window.background_color = arcade.color.ORANGE_PEEL

def on_draw(self):
self.clear()
Expand Down

0 comments on commit 0d4b5b6

Please sign in to comment.