Skip to content

Commit

Permalink
Initial Platformer Tutorial Re-Write (#1940)
Browse files Browse the repository at this point in the history
Covers Chapters 1-14 of the platformer tutorial for Arcade 3.0

Chapters 15-20 have most of their code finished, however don't have write-ups and may need code tweaks as the write-ups are finished.
  • Loading branch information
Cleptomania committed Dec 15, 2023
1 parent af45705 commit e8c0997
Show file tree
Hide file tree
Showing 52 changed files with 4,641 additions and 4,849 deletions.
13 changes: 9 additions & 4 deletions arcade/examples/platform_tutorial/01_open_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import arcade

# Constants
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 650
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
SCREEN_TITLE = "Platformer"


Expand All @@ -18,7 +18,7 @@ class MyGame(arcade.Window):

def __init__(self):

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

self.background_color = arcade.csscolor.CORNFLOWER_BLUE
Expand All @@ -30,8 +30,13 @@ def setup(self):
def on_draw(self):
"""Render the screen."""

# The clear method should always be called at the start of on_draw.
# It clears the whole screen to whatever the background color is
# set to. This ensures that you have a clean slate for drawing each
# frame of the game.
self.clear()
# Code to draw the screen goes here

# Code to draw other things will go here


def main():
Expand Down
52 changes: 9 additions & 43 deletions arcade/examples/platform_tutorial/02_draw_sprites.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@
import arcade

# Constants
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 650
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
SCREEN_TITLE = "Platformer"

# Constants used to scale our sprites from their original size
CHARACTER_SCALING = 1
TILE_SCALING = 0.5


class MyGame(arcade.Window):
"""
Expand All @@ -25,48 +21,19 @@ def __init__(self):
# Call the parent class and set up the window
super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)

# These are 'lists' that keep track of our sprites. Each sprite should
# go into a list.
self.wall_list = None
self.player_list = None
# Variable to hold our texture for our player
self.player_texture = arcade.load_texture(":resources:images/animated_characters/female_adventurer/femaleAdventurer_idle.png")

# Separate variable that holds the player sprite
self.player_sprite = None
self.player_sprite = arcade.Sprite(self.player_texture)
self.player_sprite.center_x = 64
self.player_sprite.center_y = 128

self.background_color = arcade.csscolor.CORNFLOWER_BLUE

def setup(self):
"""Set up the game here. Call this function to restart the game."""
# Create the Sprite lists
self.player_list = arcade.SpriteList()
self.wall_list = arcade.SpriteList(use_spatial_hash=True)

# Set up the player, specifically placing it at these coordinates.
image_source = ":resources:images/animated_characters/female_adventurer/femaleAdventurer_idle.png"
self.player_sprite = arcade.Sprite(image_source, CHARACTER_SCALING)
self.player_sprite.center_x = 64
self.player_sprite.center_y = 128
self.player_list.append(self.player_sprite)

# Create the ground
# This shows using a loop to place multiple sprites horizontally
for x in range(0, 1250, 64):
wall = arcade.Sprite(":resources:images/tiles/grassMid.png", TILE_SCALING)
wall.center_x = x
wall.center_y = 32
self.wall_list.append(wall)

# Put some crates on the ground
# This shows using a coordinate list to place sprites
coordinate_list = [[512, 96], [256, 96], [768, 96]]

for coordinate in coordinate_list:
# Add a crate on the ground
wall = arcade.Sprite(
":resources:images/tiles/boxCrate_double.png", TILE_SCALING
)
wall.position = coordinate
self.wall_list.append(wall)
pass

def on_draw(self):
"""Render the screen."""
Expand All @@ -75,8 +42,7 @@ def on_draw(self):
self.clear()

# Draw our sprites
self.wall_list.draw()
self.player_list.draw()
self.player_sprite.draw()


def main():
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
"""
Platformer Game
python -m arcade.examples.platform_tutorial.03_scene_object
python -m arcade.examples.platform_tutorial.03_more_sprites
"""
import arcade

# Constants
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 650
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
SCREEN_TITLE = "Platformer"

# Constants used to scale our sprites from their original size
CHARACTER_SCALING = 1
TILE_SCALING = 0.5


Expand All @@ -25,38 +24,33 @@ def __init__(self):
# Call the parent class and set up the window
super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)

# Our Scene Object
self.scene = None
# Variable to hold our texture for our player
self.player_texture = arcade.load_texture(":resources:images/animated_characters/female_adventurer/femaleAdventurer_idle.png")

# Separate variable that holds the player sprite
self.player_sprite = None

self.background_color = arcade.csscolor.CORNFLOWER_BLUE

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

# Initialize Scene
self.scene = arcade.Scene()

# Create the Sprite lists
self.scene.add_sprite_list("Player")
self.scene.add_sprite_list("Walls", use_spatial_hash=True)

# Set up the player, specifically placing it at these coordinates.
image_source = ":resources:images/animated_characters/female_adventurer/femaleAdventurer_idle.png"
self.player_sprite = arcade.Sprite(image_source, CHARACTER_SCALING)
self.player_sprite = arcade.Sprite(self.player_texture)
self.player_sprite.center_x = 64
self.player_sprite.center_y = 128
self.scene.add_sprite("Player", self.player_sprite)

# SpriteList for our player
self.player_list = arcade.SpriteList()
self.player_list.append(self.player_sprite)

# SpriteList for our boxes and ground
# Putting our ground and box Sprites in the same SpriteList
# will make it easier to perform collision detection against
# them later on. Setting the spatial hash to True will make
# collision detection much faster if the objects in this
# SpriteList do not move.
self.wall_list = arcade.SpriteList(use_spatial_hash=True)

# Create the ground
# This shows using a loop to place multiple sprites horizontally
for x in range(0, 1250, 64):
wall = arcade.Sprite(":resources:images/tiles/grassMid.png", TILE_SCALING)
wall = arcade.Sprite(":resources:images/tiles/grassMid.png", scale=0.5)
wall.center_x = x
wall.center_y = 32
self.scene.add_sprite("Walls", wall)
self.wall_list.append(wall)

# Put some crates on the ground
# This shows using a coordinate list to place sprites
Expand All @@ -65,19 +59,26 @@ def setup(self):
for coordinate in coordinate_list:
# Add a crate on the ground
wall = arcade.Sprite(
":resources:images/tiles/boxCrate_double.png", TILE_SCALING
":resources:images/tiles/boxCrate_double.png", scale=0.5
)
wall.position = coordinate
self.scene.add_sprite("Walls", wall)
self.wall_list.append(wall)

self.background_color = arcade.csscolor.CORNFLOWER_BLUE

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

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

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

# Draw our Scene
self.scene.draw()
# Draw our sprites
self.player_list.draw()
self.wall_list.draw()


def main():
Expand Down
78 changes: 41 additions & 37 deletions arcade/examples/platform_tutorial/04_user_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
import arcade

# Constants
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 650
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
SCREEN_TITLE = "Platformer"

# Constants used to scale our sprites from their original size
CHARACTER_SCALING = 1
TILE_SCALING = 0.5

# Movement speed of player, in pixels per frame
Expand All @@ -28,37 +27,33 @@ def __init__(self):
# Call the parent class and set up the window
super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)

# Our Scene Object
self.scene = None
# Variable to hold our texture for our player
self.player_texture = arcade.load_texture(":resources:images/animated_characters/female_adventurer/femaleAdventurer_idle.png")

# Separate variable that holds the player sprite
self.player_sprite = None

# Our physics engine
self.physics_engine = None

self.background_color = arcade.csscolor.CORNFLOWER_BLUE

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

# Initialize Scene
self.scene = arcade.Scene()

# Set up the player, specifically placing it at these coordinates.
image_source = ":resources:images/animated_characters/female_adventurer/femaleAdventurer_idle.png"
self.player_sprite = arcade.Sprite(image_source, CHARACTER_SCALING)
self.player_sprite = arcade.Sprite(self.player_texture)
self.player_sprite.center_x = 64
self.player_sprite.center_y = 128
self.scene.add_sprite("Player", self.player_sprite)

# SpriteList for our player
self.player_list = arcade.SpriteList()
self.player_list.append(self.player_sprite)

# SpriteList for our boxes and ground
# Putting our ground and box Sprites in the same SpriteList
# will make it easier to perform collision detection against
# them later on. Setting the spatial hash to True will make
# collision detection much faster if the objects in this
# SpriteList do not move.
self.wall_list = arcade.SpriteList(use_spatial_hash=True)

# Create the ground
# This shows using a loop to place multiple sprites horizontally
for x in range(0, 1250, 64):
wall = arcade.Sprite(":resources:images/tiles/grassMid.png", TILE_SCALING)
wall = arcade.Sprite(":resources:images/tiles/grassMid.png", scale=TILE_SCALING)
wall.center_x = x
wall.center_y = 32
self.scene.add_sprite("Walls", wall)
self.wall_list.append(wall)

# Put some crates on the ground
# This shows using a coordinate list to place sprites
Expand All @@ -67,24 +62,39 @@ def setup(self):
for coordinate in coordinate_list:
# Add a crate on the ground
wall = arcade.Sprite(
":resources:images/tiles/boxCrate_double.png", TILE_SCALING
":resources:images/tiles/boxCrate_double.png", scale=TILE_SCALING
)
wall.position = coordinate
self.scene.add_sprite("Walls", wall)
self.wall_list.append(wall)

# Create the 'physics engine'
# Create a Simple Physics Engine, this will handle moving our
# player as well as collisions between the player sprite and
# whatever SpriteList we specify for the walls.
self.physics_engine = arcade.PhysicsEngineSimple(
self.player_sprite, self.scene.get_sprite_list("Walls")
self.player_sprite, self.wall_list
)

self.background_color = arcade.csscolor.CORNFLOWER_BLUE

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

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

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

# Draw our Scene
self.scene.draw()
# Draw our sprites
self.player_list.draw()
self.wall_list.draw()

def on_update(self, delta_time):
"""Movement and Game Logic"""

# Move the player using our physics engine
self.physics_engine.update()

def on_key_press(self, key, modifiers):
"""Called whenever a key is pressed."""
Expand All @@ -99,7 +109,7 @@ def on_key_press(self, key, modifiers):
self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED

def on_key_release(self, key, modifiers):
"""Called when the user releases a key."""
"""Called whenever a key is released."""

if key == arcade.key.UP or key == arcade.key.W:
self.player_sprite.change_y = 0
Expand All @@ -110,12 +120,6 @@ def on_key_release(self, key, modifiers):
elif key == arcade.key.RIGHT or key == arcade.key.D:
self.player_sprite.change_x = 0

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

# Move the player with the physics engine
self.physics_engine.update()


def main():
"""Main function"""
Expand Down
Loading

0 comments on commit e8c0997

Please sign in to comment.