clone and modify arcade to stop text scale and speed up text display
43
arcade/examples/platform_tutorial/01_open_window.py
Normal file
@ -0,0 +1,43 @@
|
||||
"""
|
||||
Platformer Game
|
||||
"""
|
||||
import arcade
|
||||
|
||||
# Constants
|
||||
SCREEN_WIDTH = 1000
|
||||
SCREEN_HEIGHT = 650
|
||||
SCREEN_TITLE = "Platformer"
|
||||
|
||||
|
||||
class MyGame(arcade.Window):
|
||||
"""
|
||||
Main application class.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
|
||||
# Call the parent class and set up the window
|
||||
super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
|
||||
|
||||
arcade.set_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. """
|
||||
|
||||
arcade.start_render()
|
||||
# Code to draw the screen goes here
|
||||
|
||||
|
||||
def main():
|
||||
""" Main method """
|
||||
window = MyGame()
|
||||
window.setup()
|
||||
arcade.run()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
91
arcade/examples/platform_tutorial/02_draw_sprites.py
Normal file
@ -0,0 +1,91 @@
|
||||
"""
|
||||
Platformer Game
|
||||
"""
|
||||
import arcade
|
||||
|
||||
# Constants
|
||||
SCREEN_WIDTH = 1000
|
||||
SCREEN_HEIGHT = 650
|
||||
SCREEN_TITLE = "Platformer"
|
||||
|
||||
# Constants used to scale our sprites from their original size
|
||||
CHARACTER_SCALING = 1
|
||||
TILE_SCALING = 0.5
|
||||
COIN_SCALING = 0.5
|
||||
|
||||
|
||||
class MyGame(arcade.Window):
|
||||
"""
|
||||
Main application class.
|
||||
"""
|
||||
|
||||
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.coin_list = None
|
||||
self.wall_list = None
|
||||
self.player_list = None
|
||||
|
||||
# Separate variable that holds the player sprite
|
||||
self.player_sprite = None
|
||||
|
||||
arcade.set_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()
|
||||
self.coin_list = arcade.SpriteList()
|
||||
|
||||
# Set up the player, specifically placing it at these coordinates.
|
||||
self.player_sprite = arcade.Sprite("images/player_1/player_stand.png", CHARACTER_SCALING)
|
||||
self.player_sprite.center_x = 64
|
||||
self.player_sprite.center_y = 120
|
||||
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("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("images/tiles/boxCrate_double.png", TILE_SCALING)
|
||||
wall.position = coordinate
|
||||
self.wall_list.append(wall)
|
||||
|
||||
def on_draw(self):
|
||||
""" Render the screen. """
|
||||
|
||||
# Clear the screen to the background color
|
||||
arcade.start_render()
|
||||
|
||||
# Draw our sprites
|
||||
self.wall_list.draw()
|
||||
self.coin_list.draw()
|
||||
self.player_list.draw()
|
||||
|
||||
|
||||
def main():
|
||||
""" Main method """
|
||||
window = MyGame()
|
||||
window.setup()
|
||||
arcade.run()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
131
arcade/examples/platform_tutorial/03_user_control.py
Normal file
@ -0,0 +1,131 @@
|
||||
"""
|
||||
Platformer Game
|
||||
"""
|
||||
import arcade
|
||||
|
||||
# Constants
|
||||
SCREEN_WIDTH = 1000
|
||||
SCREEN_HEIGHT = 650
|
||||
SCREEN_TITLE = "Platformer"
|
||||
|
||||
# Constants used to scale our sprites from their original size
|
||||
CHARACTER_SCALING = 1
|
||||
TILE_SCALING = 0.5
|
||||
COIN_SCALING = 0.5
|
||||
|
||||
# Movement speed of player, in pixels per frame
|
||||
PLAYER_MOVEMENT_SPEED = 5
|
||||
|
||||
|
||||
class MyGame(arcade.Window):
|
||||
"""
|
||||
Main application class.
|
||||
"""
|
||||
|
||||
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.coin_list = None
|
||||
self.wall_list = None
|
||||
self.player_list = None
|
||||
|
||||
# Separate variable that holds the player sprite
|
||||
self.player_sprite = None
|
||||
|
||||
# Our physics engine
|
||||
self.physics_engine = None
|
||||
|
||||
arcade.set_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()
|
||||
self.coin_list = arcade.SpriteList()
|
||||
|
||||
# Set up the player, specifically placing it at these coordinates.
|
||||
self.player_sprite = arcade.Sprite("images/player_1/player_stand.png", CHARACTER_SCALING)
|
||||
self.player_sprite.center_x = 64
|
||||
self.player_sprite.center_y = 120
|
||||
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("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("images/tiles/boxCrate_double.png", TILE_SCALING)
|
||||
wall.position = coordinate
|
||||
self.wall_list.append(wall)
|
||||
|
||||
# Create the 'physics engine'
|
||||
self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite, self.wall_list)
|
||||
|
||||
def on_draw(self):
|
||||
""" Render the screen. """
|
||||
|
||||
# Clear the screen to the background color
|
||||
arcade.start_render()
|
||||
|
||||
# Draw our sprites
|
||||
self.wall_list.draw()
|
||||
self.coin_list.draw()
|
||||
self.player_list.draw()
|
||||
|
||||
def on_key_press(self, key, modifiers):
|
||||
"""Called whenever a key is pressed. """
|
||||
|
||||
if key == arcade.key.UP or key == arcade.key.W:
|
||||
self.player_sprite.change_y = PLAYER_MOVEMENT_SPEED
|
||||
elif key == arcade.key.DOWN or key == arcade.key.S:
|
||||
self.player_sprite.change_y = -PLAYER_MOVEMENT_SPEED
|
||||
elif key == arcade.key.LEFT or key == arcade.key.A:
|
||||
self.player_sprite.change_x = -PLAYER_MOVEMENT_SPEED
|
||||
elif key == arcade.key.RIGHT or key == arcade.key.D:
|
||||
self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED
|
||||
|
||||
def on_key_release(self, key, modifiers):
|
||||
"""Called when the user releases a key. """
|
||||
|
||||
if key == arcade.key.UP or key == arcade.key.W:
|
||||
self.player_sprite.change_y = 0
|
||||
elif key == arcade.key.DOWN or key == arcade.key.S:
|
||||
self.player_sprite.change_y = 0
|
||||
elif key == arcade.key.LEFT or key == arcade.key.A:
|
||||
self.player_sprite.change_x = 0
|
||||
elif key == arcade.key.RIGHT or key == arcade.key.D:
|
||||
self.player_sprite.change_x = 0
|
||||
|
||||
def update(self, delta_time):
|
||||
""" Movement and game logic """
|
||||
|
||||
# Call update on all sprites (The sprites don't do much in this
|
||||
# example though.)
|
||||
self.physics_engine.update()
|
||||
|
||||
|
||||
def main():
|
||||
""" Main method """
|
||||
window = MyGame()
|
||||
window.setup()
|
||||
arcade.run()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
130
arcade/examples/platform_tutorial/04_add_gravity.py
Normal file
@ -0,0 +1,130 @@
|
||||
"""
|
||||
Platformer Game
|
||||
"""
|
||||
import arcade
|
||||
|
||||
# Constants
|
||||
SCREEN_WIDTH = 1000
|
||||
SCREEN_HEIGHT = 650
|
||||
SCREEN_TITLE = "Platformer"
|
||||
|
||||
# Constants used to scale our sprites from their original size
|
||||
CHARACTER_SCALING = 1
|
||||
TILE_SCALING = 0.5
|
||||
COIN_SCALING = 0.5
|
||||
|
||||
# Movement speed of player, in pixels per frame
|
||||
PLAYER_MOVEMENT_SPEED = 5
|
||||
GRAVITY = 1
|
||||
PLAYER_JUMP_SPEED = 15
|
||||
|
||||
|
||||
class MyGame(arcade.Window):
|
||||
"""
|
||||
Main application class.
|
||||
"""
|
||||
|
||||
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.coin_list = None
|
||||
self.wall_list = None
|
||||
self.player_list = None
|
||||
|
||||
# Separate variable that holds the player sprite
|
||||
self.player_sprite = None
|
||||
|
||||
# Our physics engine
|
||||
self.physics_engine = None
|
||||
|
||||
arcade.set_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()
|
||||
self.coin_list = arcade.SpriteList()
|
||||
|
||||
# Set up the player, specifically placing it at these coordinates.
|
||||
self.player_sprite = arcade.Sprite("images/player_1/player_stand.png", CHARACTER_SCALING)
|
||||
self.player_sprite.center_x = 64
|
||||
self.player_sprite.center_y = 96
|
||||
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("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("images/tiles/boxCrate_double.png", TILE_SCALING)
|
||||
wall.position = coordinate
|
||||
self.wall_list.append(wall)
|
||||
|
||||
# Create the 'physics engine'
|
||||
self.physics_engine = arcade.PhysicsEnginePlatformer(self.player_sprite,
|
||||
self.wall_list,
|
||||
GRAVITY)
|
||||
|
||||
def on_draw(self):
|
||||
""" Render the screen. """
|
||||
|
||||
# Clear the screen to the background color
|
||||
arcade.start_render()
|
||||
|
||||
# Draw our sprites
|
||||
self.wall_list.draw()
|
||||
self.coin_list.draw()
|
||||
self.player_list.draw()
|
||||
|
||||
def on_key_press(self, key, modifiers):
|
||||
"""Called whenever a key is pressed. """
|
||||
|
||||
if key == arcade.key.UP or key == arcade.key.W:
|
||||
if self.physics_engine.can_jump():
|
||||
self.player_sprite.change_y = PLAYER_JUMP_SPEED
|
||||
elif key == arcade.key.LEFT or key == arcade.key.A:
|
||||
self.player_sprite.change_x = -PLAYER_MOVEMENT_SPEED
|
||||
elif key == arcade.key.RIGHT or key == arcade.key.D:
|
||||
self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED
|
||||
|
||||
def on_key_release(self, key, modifiers):
|
||||
"""Called when the user releases a key. """
|
||||
|
||||
if key == arcade.key.LEFT or key == arcade.key.A:
|
||||
self.player_sprite.change_x = 0
|
||||
elif key == arcade.key.RIGHT or key == arcade.key.D:
|
||||
self.player_sprite.change_x = 0
|
||||
|
||||
def update(self, delta_time):
|
||||
""" Movement and game logic """
|
||||
|
||||
# Call update on all sprites (The sprites don't do much in this
|
||||
# example though.)
|
||||
self.physics_engine.update()
|
||||
|
||||
|
||||
def main():
|
||||
""" Main method """
|
||||
window = MyGame()
|
||||
window.setup()
|
||||
arcade.run()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
188
arcade/examples/platform_tutorial/05_scrolling.py
Normal file
@ -0,0 +1,188 @@
|
||||
"""
|
||||
Platformer Game
|
||||
"""
|
||||
import arcade
|
||||
|
||||
# Constants
|
||||
SCREEN_WIDTH = 1000
|
||||
SCREEN_HEIGHT = 650
|
||||
SCREEN_TITLE = "Platformer"
|
||||
|
||||
# Constants used to scale our sprites from their original size
|
||||
CHARACTER_SCALING = 1
|
||||
TILE_SCALING = 0.5
|
||||
COIN_SCALING = 0.5
|
||||
|
||||
# Movement speed of player, in pixels per frame
|
||||
PLAYER_MOVEMENT_SPEED = 5
|
||||
GRAVITY = 1
|
||||
PLAYER_JUMP_SPEED = 15
|
||||
|
||||
# How many pixels to keep as a minimum margin between the character
|
||||
# and the edge of the screen.
|
||||
LEFT_VIEWPORT_MARGIN = 150
|
||||
RIGHT_VIEWPORT_MARGIN = 150
|
||||
BOTTOM_VIEWPORT_MARGIN = 50
|
||||
TOP_VIEWPORT_MARGIN = 100
|
||||
|
||||
|
||||
class MyGame(arcade.Window):
|
||||
"""
|
||||
Main application class.
|
||||
"""
|
||||
|
||||
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.coin_list = None
|
||||
self.wall_list = None
|
||||
self.player_list = None
|
||||
|
||||
# Separate variable that holds the player sprite
|
||||
self.player_sprite = None
|
||||
|
||||
# Our physics engine
|
||||
self.physics_engine = None
|
||||
|
||||
# Used to keep track of our scrolling
|
||||
self.view_bottom = 0
|
||||
self.view_left = 0
|
||||
|
||||
arcade.set_background_color(arcade.csscolor.CORNFLOWER_BLUE)
|
||||
|
||||
def setup(self):
|
||||
""" Set up the game here. Call this function to restart the game. """
|
||||
|
||||
# Used to keep track of our scrolling
|
||||
self.view_bottom = 0
|
||||
self.view_left = 0
|
||||
|
||||
# Create the Sprite lists
|
||||
self.player_list = arcade.SpriteList()
|
||||
self.wall_list = arcade.SpriteList()
|
||||
self.coin_list = arcade.SpriteList()
|
||||
|
||||
# Set up the player, specifically placing it at these coordinates.
|
||||
self.player_sprite = arcade.Sprite("images/player_1/player_stand.png", CHARACTER_SCALING)
|
||||
self.player_sprite.center_x = 64
|
||||
self.player_sprite.center_y = 96
|
||||
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("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("images/tiles/boxCrate_double.png", TILE_SCALING)
|
||||
wall.position = coordinate
|
||||
self.wall_list.append(wall)
|
||||
|
||||
# Create the 'physics engine'
|
||||
self.physics_engine = arcade.PhysicsEnginePlatformer(self.player_sprite,
|
||||
self.wall_list,
|
||||
GRAVITY)
|
||||
|
||||
def on_draw(self):
|
||||
""" Render the screen. """
|
||||
|
||||
# Clear the screen to the background color
|
||||
arcade.start_render()
|
||||
|
||||
# Draw our sprites
|
||||
self.wall_list.draw()
|
||||
self.coin_list.draw()
|
||||
self.player_list.draw()
|
||||
|
||||
def on_key_press(self, key, modifiers):
|
||||
"""Called whenever a key is pressed. """
|
||||
|
||||
if key == arcade.key.UP or key == arcade.key.W:
|
||||
if self.physics_engine.can_jump():
|
||||
self.player_sprite.change_y = PLAYER_JUMP_SPEED
|
||||
elif key == arcade.key.LEFT or key == arcade.key.A:
|
||||
self.player_sprite.change_x = -PLAYER_MOVEMENT_SPEED
|
||||
elif key == arcade.key.RIGHT or key == arcade.key.D:
|
||||
self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED
|
||||
|
||||
def on_key_release(self, key, modifiers):
|
||||
"""Called when the user releases a key. """
|
||||
|
||||
if key == arcade.key.LEFT or key == arcade.key.A:
|
||||
self.player_sprite.change_x = 0
|
||||
elif key == arcade.key.RIGHT or key == arcade.key.D:
|
||||
self.player_sprite.change_x = 0
|
||||
|
||||
def update(self, delta_time):
|
||||
""" Movement and game logic """
|
||||
|
||||
# Call update on all sprites (The sprites don't do much in this
|
||||
# example though.)
|
||||
self.physics_engine.update()
|
||||
|
||||
# --- Manage Scrolling ---
|
||||
|
||||
# Track if we need to change the viewport
|
||||
|
||||
changed = False
|
||||
|
||||
# Scroll left
|
||||
left_boundary = self.view_left + LEFT_VIEWPORT_MARGIN
|
||||
if self.player_sprite.left < left_boundary:
|
||||
self.view_left -= left_boundary - self.player_sprite.left
|
||||
changed = True
|
||||
|
||||
# Scroll right
|
||||
right_boundary = self.view_left + SCREEN_WIDTH - RIGHT_VIEWPORT_MARGIN
|
||||
if self.player_sprite.right > right_boundary:
|
||||
self.view_left += self.player_sprite.right - right_boundary
|
||||
changed = True
|
||||
|
||||
# Scroll up
|
||||
top_boundary = self.view_bottom + SCREEN_HEIGHT - TOP_VIEWPORT_MARGIN
|
||||
if self.player_sprite.top > top_boundary:
|
||||
self.view_bottom += self.player_sprite.top - top_boundary
|
||||
changed = True
|
||||
|
||||
# Scroll down
|
||||
bottom_boundary = self.view_bottom + BOTTOM_VIEWPORT_MARGIN
|
||||
if self.player_sprite.bottom < bottom_boundary:
|
||||
self.view_bottom -= bottom_boundary - self.player_sprite.bottom
|
||||
changed = True
|
||||
|
||||
if changed:
|
||||
# Only scroll to integers. Otherwise we end up with pixels that
|
||||
# don't line up on the screen
|
||||
self.view_bottom = int(self.view_bottom)
|
||||
self.view_left = int(self.view_left)
|
||||
|
||||
# Do the scrolling
|
||||
arcade.set_viewport(self.view_left,
|
||||
SCREEN_WIDTH + self.view_left,
|
||||
self.view_bottom,
|
||||
SCREEN_HEIGHT + self.view_bottom)
|
||||
|
||||
|
||||
def main():
|
||||
""" Main method """
|
||||
window = MyGame()
|
||||
window.setup()
|
||||
arcade.run()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
212
arcade/examples/platform_tutorial/06_coins_and_sound.py
Normal file
@ -0,0 +1,212 @@
|
||||
"""
|
||||
Platformer Game
|
||||
"""
|
||||
import arcade
|
||||
|
||||
# Constants
|
||||
SCREEN_WIDTH = 1000
|
||||
SCREEN_HEIGHT = 650
|
||||
SCREEN_TITLE = "Platformer"
|
||||
|
||||
# Constants used to scale our sprites from their original size
|
||||
CHARACTER_SCALING = 1
|
||||
TILE_SCALING = 0.5
|
||||
COIN_SCALING = 0.5
|
||||
|
||||
# Movement speed of player, in pixels per frame
|
||||
PLAYER_MOVEMENT_SPEED = 5
|
||||
GRAVITY = 1
|
||||
PLAYER_JUMP_SPEED = 15
|
||||
|
||||
# How many pixels to keep as a minimum margin between the character
|
||||
# and the edge of the screen.
|
||||
LEFT_VIEWPORT_MARGIN = 150
|
||||
RIGHT_VIEWPORT_MARGIN = 150
|
||||
BOTTOM_VIEWPORT_MARGIN = 50
|
||||
TOP_VIEWPORT_MARGIN = 100
|
||||
|
||||
|
||||
class MyGame(arcade.Window):
|
||||
"""
|
||||
Main application class.
|
||||
"""
|
||||
|
||||
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.coin_list = None
|
||||
self.wall_list = None
|
||||
self.player_list = None
|
||||
|
||||
# Separate variable that holds the player sprite
|
||||
self.player_sprite = None
|
||||
|
||||
# Our physics engine
|
||||
self.physics_engine = None
|
||||
|
||||
# Used to keep track of our scrolling
|
||||
self.view_bottom = 0
|
||||
self.view_left = 0
|
||||
|
||||
# Load sounds
|
||||
self.collect_coin_sound = arcade.load_sound("sounds/coin1.wav")
|
||||
self.jump_sound = arcade.load_sound("sounds/jump1.wav")
|
||||
|
||||
arcade.set_background_color(arcade.csscolor.CORNFLOWER_BLUE)
|
||||
|
||||
def setup(self):
|
||||
""" Set up the game here. Call this function to restart the game. """
|
||||
|
||||
# Used to keep track of our scrolling
|
||||
self.view_bottom = 0
|
||||
self.view_left = 0
|
||||
|
||||
# Create the Sprite lists
|
||||
self.player_list = arcade.SpriteList()
|
||||
self.wall_list = arcade.SpriteList()
|
||||
self.coin_list = arcade.SpriteList()
|
||||
|
||||
# Set up the player, specifically placing it at these coordinates.
|
||||
self.player_sprite = arcade.Sprite("images/player_1/player_stand.png", CHARACTER_SCALING)
|
||||
self.player_sprite.center_x = 64
|
||||
self.player_sprite.center_y = 96
|
||||
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("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("images/tiles/boxCrate_double.png", TILE_SCALING)
|
||||
wall.position = coordinate
|
||||
self.wall_list.append(wall)
|
||||
|
||||
# Use a loop to place some coins for our character to pick up
|
||||
for x in range(128, 1250, 256):
|
||||
coin = arcade.Sprite("images/items/coinGold.png", COIN_SCALING)
|
||||
coin.center_x = x
|
||||
coin.center_y = 96
|
||||
self.coin_list.append(coin)
|
||||
|
||||
# Create the 'physics engine'
|
||||
self.physics_engine = arcade.PhysicsEnginePlatformer(self.player_sprite,
|
||||
self.wall_list,
|
||||
GRAVITY)
|
||||
|
||||
def on_draw(self):
|
||||
""" Render the screen. """
|
||||
|
||||
# Clear the screen to the background color
|
||||
arcade.start_render()
|
||||
|
||||
# Draw our sprites
|
||||
self.wall_list.draw()
|
||||
self.coin_list.draw()
|
||||
self.player_list.draw()
|
||||
|
||||
def on_key_press(self, key, modifiers):
|
||||
"""Called whenever a key is pressed. """
|
||||
|
||||
if key == arcade.key.UP or key == arcade.key.W:
|
||||
if self.physics_engine.can_jump():
|
||||
self.player_sprite.change_y = PLAYER_JUMP_SPEED
|
||||
arcade.play_sound(self.jump_sound)
|
||||
elif key == arcade.key.LEFT or key == arcade.key.A:
|
||||
self.player_sprite.change_x = -PLAYER_MOVEMENT_SPEED
|
||||
elif key == arcade.key.RIGHT or key == arcade.key.D:
|
||||
self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED
|
||||
|
||||
def on_key_release(self, key, modifiers):
|
||||
"""Called when the user releases a key. """
|
||||
|
||||
if key == arcade.key.LEFT or key == arcade.key.A:
|
||||
self.player_sprite.change_x = 0
|
||||
elif key == arcade.key.RIGHT or key == arcade.key.D:
|
||||
self.player_sprite.change_x = 0
|
||||
|
||||
def update(self, delta_time):
|
||||
""" Movement and game logic """
|
||||
|
||||
# Call update on all sprites (The sprites don't do much in this
|
||||
# example though.)
|
||||
self.physics_engine.update()
|
||||
|
||||
# See if we hit any coins
|
||||
coin_hit_list = arcade.check_for_collision_with_list(self.player_sprite,
|
||||
self.coin_list)
|
||||
|
||||
# Loop through each coin we hit (if any) and remove it
|
||||
for coin in coin_hit_list:
|
||||
# Remove the coin
|
||||
coin.remove_from_sprite_lists()
|
||||
# Play a sound
|
||||
arcade.play_sound(self.collect_coin_sound)
|
||||
# Add one to the score
|
||||
|
||||
# --- Manage Scrolling ---
|
||||
|
||||
# Track if we need to change the viewport
|
||||
|
||||
changed = False
|
||||
|
||||
# Scroll left
|
||||
left_boundary = self.view_left + LEFT_VIEWPORT_MARGIN
|
||||
if self.player_sprite.left < left_boundary:
|
||||
self.view_left -= left_boundary - self.player_sprite.left
|
||||
changed = True
|
||||
|
||||
# Scroll right
|
||||
right_boundary = self.view_left + SCREEN_WIDTH - RIGHT_VIEWPORT_MARGIN
|
||||
if self.player_sprite.right > right_boundary:
|
||||
self.view_left += self.player_sprite.right - right_boundary
|
||||
changed = True
|
||||
|
||||
# Scroll up
|
||||
top_boundary = self.view_bottom + SCREEN_HEIGHT - TOP_VIEWPORT_MARGIN
|
||||
if self.player_sprite.top > top_boundary:
|
||||
self.view_bottom += self.player_sprite.top - top_boundary
|
||||
changed = True
|
||||
|
||||
# Scroll down
|
||||
bottom_boundary = self.view_bottom + BOTTOM_VIEWPORT_MARGIN
|
||||
if self.player_sprite.bottom < bottom_boundary:
|
||||
self.view_bottom -= bottom_boundary - self.player_sprite.bottom
|
||||
changed = True
|
||||
|
||||
if changed:
|
||||
# Only scroll to integers. Otherwise we end up with pixels that
|
||||
# don't line up on the screen
|
||||
self.view_bottom = int(self.view_bottom)
|
||||
self.view_left = int(self.view_left)
|
||||
|
||||
# Do the scrolling
|
||||
arcade.set_viewport(self.view_left,
|
||||
SCREEN_WIDTH + self.view_left,
|
||||
self.view_bottom,
|
||||
SCREEN_HEIGHT + self.view_bottom)
|
||||
|
||||
|
||||
def main():
|
||||
""" Main method """
|
||||
window = MyGame()
|
||||
window.setup()
|
||||
arcade.run()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
224
arcade/examples/platform_tutorial/07_score.py
Normal file
@ -0,0 +1,224 @@
|
||||
"""
|
||||
Platformer Game
|
||||
"""
|
||||
import arcade
|
||||
|
||||
# Constants
|
||||
SCREEN_WIDTH = 1000
|
||||
SCREEN_HEIGHT = 650
|
||||
SCREEN_TITLE = "Platformer"
|
||||
|
||||
# Constants used to scale our sprites from their original size
|
||||
CHARACTER_SCALING = 1
|
||||
TILE_SCALING = 0.5
|
||||
COIN_SCALING = 0.5
|
||||
|
||||
# Movement speed of player, in pixels per frame
|
||||
PLAYER_MOVEMENT_SPEED = 5
|
||||
GRAVITY = 1
|
||||
PLAYER_JUMP_SPEED = 15
|
||||
|
||||
# How many pixels to keep as a minimum margin between the character
|
||||
# and the edge of the screen.
|
||||
LEFT_VIEWPORT_MARGIN = 150
|
||||
RIGHT_VIEWPORT_MARGIN = 150
|
||||
BOTTOM_VIEWPORT_MARGIN = 50
|
||||
TOP_VIEWPORT_MARGIN = 100
|
||||
|
||||
|
||||
class MyGame(arcade.Window):
|
||||
"""
|
||||
Main application class.
|
||||
"""
|
||||
|
||||
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.coin_list = None
|
||||
self.wall_list = None
|
||||
self.player_list = None
|
||||
|
||||
# Separate variable that holds the player sprite
|
||||
self.player_sprite = None
|
||||
|
||||
# Our physics engine
|
||||
self.physics_engine = None
|
||||
|
||||
# Used to keep track of our scrolling
|
||||
self.view_bottom = 0
|
||||
self.view_left = 0
|
||||
|
||||
# Keep track of the score
|
||||
self.score = 0
|
||||
|
||||
# Load sounds
|
||||
self.collect_coin_sound = arcade.load_sound("sounds/coin1.wav")
|
||||
self.jump_sound = arcade.load_sound("sounds/jump1.wav")
|
||||
|
||||
arcade.set_background_color(arcade.csscolor.CORNFLOWER_BLUE)
|
||||
|
||||
def setup(self):
|
||||
""" Set up the game here. Call this function to restart the game. """
|
||||
|
||||
# Used to keep track of our scrolling
|
||||
self.view_bottom = 0
|
||||
self.view_left = 0
|
||||
|
||||
# Keep track of the score
|
||||
self.score = 0
|
||||
|
||||
# Create the Sprite lists
|
||||
self.player_list = arcade.SpriteList()
|
||||
self.wall_list = arcade.SpriteList()
|
||||
self.coin_list = arcade.SpriteList()
|
||||
|
||||
# Set up the player, specifically placing it at these coordinates.
|
||||
self.player_sprite = arcade.Sprite("images/player_1/player_stand.png", CHARACTER_SCALING)
|
||||
self.player_sprite.center_x = 64
|
||||
self.player_sprite.center_y = 96
|
||||
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("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("images/tiles/boxCrate_double.png", TILE_SCALING)
|
||||
wall.position = coordinate
|
||||
self.wall_list.append(wall)
|
||||
|
||||
# Use a loop to place some coins for our character to pick up
|
||||
for x in range(128, 1250, 256):
|
||||
coin = arcade.Sprite("images/items/coinGold.png", COIN_SCALING)
|
||||
coin.center_x = x
|
||||
coin.center_y = 96
|
||||
self.coin_list.append(coin)
|
||||
|
||||
# Create the 'physics engine'
|
||||
self.physics_engine = arcade.PhysicsEnginePlatformer(self.player_sprite,
|
||||
self.wall_list,
|
||||
GRAVITY)
|
||||
|
||||
def on_draw(self):
|
||||
""" Render the screen. """
|
||||
|
||||
# Clear the screen to the background color
|
||||
arcade.start_render()
|
||||
|
||||
# Draw our sprites
|
||||
self.wall_list.draw()
|
||||
self.coin_list.draw()
|
||||
self.player_list.draw()
|
||||
|
||||
# Draw our score on the screen, scrolling it with the viewport
|
||||
score_text = f"Score: {self.score}"
|
||||
arcade.draw_text(score_text, 10 + self.view_left, 10 + self.view_bottom,
|
||||
arcade.csscolor.WHITE, 18)
|
||||
|
||||
def on_key_press(self, key, modifiers):
|
||||
"""Called whenever a key is pressed. """
|
||||
|
||||
if key == arcade.key.UP or key == arcade.key.W:
|
||||
if self.physics_engine.can_jump():
|
||||
self.player_sprite.change_y = PLAYER_JUMP_SPEED
|
||||
arcade.play_sound(self.jump_sound)
|
||||
elif key == arcade.key.LEFT or key == arcade.key.A:
|
||||
self.player_sprite.change_x = -PLAYER_MOVEMENT_SPEED
|
||||
elif key == arcade.key.RIGHT or key == arcade.key.D:
|
||||
self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED
|
||||
|
||||
def on_key_release(self, key, modifiers):
|
||||
"""Called when the user releases a key. """
|
||||
|
||||
if key == arcade.key.LEFT or key == arcade.key.A:
|
||||
self.player_sprite.change_x = 0
|
||||
elif key == arcade.key.RIGHT or key == arcade.key.D:
|
||||
self.player_sprite.change_x = 0
|
||||
|
||||
def update(self, delta_time):
|
||||
""" Movement and game logic """
|
||||
|
||||
# Call update on all sprites (The sprites don't do much in this
|
||||
# example though.)
|
||||
self.physics_engine.update()
|
||||
|
||||
# See if we hit any coins
|
||||
coin_hit_list = arcade.check_for_collision_with_list(self.player_sprite,
|
||||
self.coin_list)
|
||||
|
||||
# Loop through each coin we hit (if any) and remove it
|
||||
for coin in coin_hit_list:
|
||||
# Remove the coin
|
||||
coin.remove_from_sprite_lists()
|
||||
# Play a sound
|
||||
arcade.play_sound(self.collect_coin_sound)
|
||||
# Add one to the score
|
||||
self.score += 1
|
||||
|
||||
# --- Manage Scrolling ---
|
||||
|
||||
# Track if we need to change the viewport
|
||||
|
||||
changed = False
|
||||
|
||||
# Scroll left
|
||||
left_boundary = self.view_left + LEFT_VIEWPORT_MARGIN
|
||||
if self.player_sprite.left < left_boundary:
|
||||
self.view_left -= left_boundary - self.player_sprite.left
|
||||
changed = True
|
||||
|
||||
# Scroll right
|
||||
right_boundary = self.view_left + SCREEN_WIDTH - RIGHT_VIEWPORT_MARGIN
|
||||
if self.player_sprite.right > right_boundary:
|
||||
self.view_left += self.player_sprite.right - right_boundary
|
||||
changed = True
|
||||
|
||||
# Scroll up
|
||||
top_boundary = self.view_bottom + SCREEN_HEIGHT - TOP_VIEWPORT_MARGIN
|
||||
if self.player_sprite.top > top_boundary:
|
||||
self.view_bottom += self.player_sprite.top - top_boundary
|
||||
changed = True
|
||||
|
||||
# Scroll down
|
||||
bottom_boundary = self.view_bottom + BOTTOM_VIEWPORT_MARGIN
|
||||
if self.player_sprite.bottom < bottom_boundary:
|
||||
self.view_bottom -= bottom_boundary - self.player_sprite.bottom
|
||||
changed = True
|
||||
|
||||
if changed:
|
||||
# Only scroll to integers. Otherwise we end up with pixels that
|
||||
# don't line up on the screen
|
||||
self.view_bottom = int(self.view_bottom)
|
||||
self.view_left = int(self.view_left)
|
||||
|
||||
# Do the scrolling
|
||||
arcade.set_viewport(self.view_left,
|
||||
SCREEN_WIDTH + self.view_left,
|
||||
self.view_bottom,
|
||||
SCREEN_HEIGHT + self.view_bottom)
|
||||
|
||||
|
||||
def main():
|
||||
""" Main method """
|
||||
window = MyGame()
|
||||
window.setup()
|
||||
arcade.run()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
222
arcade/examples/platform_tutorial/08_load_map.py
Normal file
@ -0,0 +1,222 @@
|
||||
"""
|
||||
Platformer Game
|
||||
"""
|
||||
import arcade
|
||||
|
||||
# Constants
|
||||
SCREEN_WIDTH = 1000
|
||||
SCREEN_HEIGHT = 650
|
||||
SCREEN_TITLE = "Platformer"
|
||||
|
||||
# Constants used to scale our sprites from their original size
|
||||
CHARACTER_SCALING = 1
|
||||
TILE_SCALING = 0.5
|
||||
COIN_SCALING = 0.5
|
||||
SPRITE_PIXEL_SIZE = 128
|
||||
GRID_PIXEL_SIZE = (SPRITE_PIXEL_SIZE * TILE_SCALING)
|
||||
|
||||
# Movement speed of player, in pixels per frame
|
||||
PLAYER_MOVEMENT_SPEED = 10
|
||||
GRAVITY = 1
|
||||
PLAYER_JUMP_SPEED = 15
|
||||
|
||||
# How many pixels to keep as a minimum margin between the character
|
||||
# and the edge of the screen.
|
||||
LEFT_VIEWPORT_MARGIN = 150
|
||||
RIGHT_VIEWPORT_MARGIN = 150
|
||||
BOTTOM_VIEWPORT_MARGIN = 100
|
||||
TOP_VIEWPORT_MARGIN = 100
|
||||
|
||||
|
||||
class MyGame(arcade.Window):
|
||||
"""
|
||||
Main application class.
|
||||
"""
|
||||
|
||||
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.coin_list = None
|
||||
self.wall_list = None
|
||||
self.player_list = None
|
||||
|
||||
# Separate variable that holds the player sprite
|
||||
self.player_sprite = None
|
||||
|
||||
# Our physics engine
|
||||
self.physics_engine = None
|
||||
|
||||
# Used to keep track of our scrolling
|
||||
self.view_bottom = 0
|
||||
self.view_left = 0
|
||||
|
||||
# Keep track of the score
|
||||
self.score = 0
|
||||
|
||||
# Load sounds
|
||||
self.collect_coin_sound = arcade.load_sound("sounds/coin1.wav")
|
||||
self.jump_sound = arcade.load_sound("sounds/jump1.wav")
|
||||
|
||||
arcade.set_background_color(arcade.csscolor.CORNFLOWER_BLUE)
|
||||
|
||||
def setup(self):
|
||||
""" Set up the game here. Call this function to restart the game. """
|
||||
|
||||
# Used to keep track of our scrolling
|
||||
self.view_bottom = 0
|
||||
self.view_left = 0
|
||||
|
||||
# Keep track of the score
|
||||
self.score = 0
|
||||
|
||||
# Create the Sprite lists
|
||||
self.player_list = arcade.SpriteList()
|
||||
self.wall_list = arcade.SpriteList()
|
||||
self.coin_list = arcade.SpriteList()
|
||||
|
||||
# Set up the player, specifically placing it at these coordinates.
|
||||
self.player_sprite = arcade.Sprite("images/player_1/player_stand.png", CHARACTER_SCALING)
|
||||
self.player_sprite.center_x = 64
|
||||
self.player_sprite.center_y = 96
|
||||
self.player_list.append(self.player_sprite)
|
||||
|
||||
# --- Load in a map from the tiled editor ---
|
||||
|
||||
# Name of map file to load
|
||||
map_name = "map.tmx"
|
||||
# Name of the layer in the file that has our platforms/walls
|
||||
platforms_layer_name = 'Platforms'
|
||||
# Name of the layer that has items for pick-up
|
||||
coins_layer_name = 'Coins'
|
||||
|
||||
# Read in the tiled map
|
||||
my_map = arcade.tilemap.read_tmx(map_name)
|
||||
|
||||
# -- Platforms
|
||||
self.wall_list = arcade.tilemap.process_layer(my_map, platforms_layer_name, TILE_SCALING)
|
||||
|
||||
# -- Coins
|
||||
self.coin_list = arcade.tilemap.process_layer(my_map, coins_layer_name, TILE_SCALING)
|
||||
|
||||
# --- Other stuff
|
||||
# Set the background color
|
||||
if my_map.background_color:
|
||||
arcade.set_background_color(my_map.background_color)
|
||||
|
||||
# Create the 'physics engine'
|
||||
self.physics_engine = arcade.PhysicsEnginePlatformer(self.player_sprite,
|
||||
self.wall_list,
|
||||
GRAVITY)
|
||||
|
||||
def on_draw(self):
|
||||
""" Render the screen. """
|
||||
|
||||
# Clear the screen to the background color
|
||||
arcade.start_render()
|
||||
|
||||
# Draw our sprites
|
||||
self.wall_list.draw()
|
||||
self.coin_list.draw()
|
||||
self.player_list.draw()
|
||||
|
||||
# Draw our score on the screen, scrolling it with the viewport
|
||||
score_text = f"Score: {self.score}"
|
||||
arcade.draw_text(score_text, 10 + self.view_left, 10 + self.view_bottom,
|
||||
arcade.csscolor.WHITE, 18)
|
||||
|
||||
def on_key_press(self, key, modifiers):
|
||||
"""Called whenever a key is pressed. """
|
||||
|
||||
if key == arcade.key.UP or key == arcade.key.W:
|
||||
if self.physics_engine.can_jump():
|
||||
self.player_sprite.change_y = PLAYER_JUMP_SPEED
|
||||
arcade.play_sound(self.jump_sound)
|
||||
elif key == arcade.key.LEFT or key == arcade.key.A:
|
||||
self.player_sprite.change_x = -PLAYER_MOVEMENT_SPEED
|
||||
elif key == arcade.key.RIGHT or key == arcade.key.D:
|
||||
self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED
|
||||
|
||||
def on_key_release(self, key, modifiers):
|
||||
"""Called when the user releases a key. """
|
||||
|
||||
if key == arcade.key.LEFT or key == arcade.key.A:
|
||||
self.player_sprite.change_x = 0
|
||||
elif key == arcade.key.RIGHT or key == arcade.key.D:
|
||||
self.player_sprite.change_x = 0
|
||||
|
||||
def update(self, delta_time):
|
||||
""" Movement and game logic """
|
||||
|
||||
# Call update on all sprites (The sprites don't do much in this
|
||||
# example though.)
|
||||
self.physics_engine.update()
|
||||
|
||||
# See if we hit any coins
|
||||
coin_hit_list = arcade.check_for_collision_with_list(self.player_sprite,
|
||||
self.coin_list)
|
||||
|
||||
# Loop through each coin we hit (if any) and remove it
|
||||
for coin in coin_hit_list:
|
||||
# Remove the coin
|
||||
coin.remove_from_sprite_lists()
|
||||
# Play a sound
|
||||
arcade.play_sound(self.collect_coin_sound)
|
||||
# Add one to the score
|
||||
self.score += 1
|
||||
|
||||
# --- Manage Scrolling ---
|
||||
|
||||
# Track if we need to change the viewport
|
||||
|
||||
changed = False
|
||||
|
||||
# Scroll left
|
||||
left_boundary = self.view_left + LEFT_VIEWPORT_MARGIN
|
||||
if self.player_sprite.left < left_boundary:
|
||||
self.view_left -= left_boundary - self.player_sprite.left
|
||||
changed = True
|
||||
|
||||
# Scroll right
|
||||
right_boundary = self.view_left + SCREEN_WIDTH - RIGHT_VIEWPORT_MARGIN
|
||||
if self.player_sprite.right > right_boundary:
|
||||
self.view_left += self.player_sprite.right - right_boundary
|
||||
changed = True
|
||||
|
||||
# Scroll up
|
||||
top_boundary = self.view_bottom + SCREEN_HEIGHT - TOP_VIEWPORT_MARGIN
|
||||
if self.player_sprite.top > top_boundary:
|
||||
self.view_bottom += self.player_sprite.top - top_boundary
|
||||
changed = True
|
||||
|
||||
# Scroll down
|
||||
bottom_boundary = self.view_bottom + BOTTOM_VIEWPORT_MARGIN
|
||||
if self.player_sprite.bottom < bottom_boundary:
|
||||
self.view_bottom -= bottom_boundary - self.player_sprite.bottom
|
||||
changed = True
|
||||
|
||||
if changed:
|
||||
# Only scroll to integers. Otherwise we end up with pixels that
|
||||
# don't line up on the screen
|
||||
self.view_bottom = int(self.view_bottom)
|
||||
self.view_left = int(self.view_left)
|
||||
|
||||
# Do the scrolling
|
||||
arcade.set_viewport(self.view_left,
|
||||
SCREEN_WIDTH + self.view_left,
|
||||
self.view_bottom,
|
||||
SCREEN_HEIGHT + self.view_bottom)
|
||||
|
||||
|
||||
def main():
|
||||
""" Main method """
|
||||
window = MyGame()
|
||||
window.setup()
|
||||
arcade.run()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
306
arcade/examples/platform_tutorial/09_endgame.py
Normal file
@ -0,0 +1,306 @@
|
||||
"""
|
||||
Platformer Game
|
||||
"""
|
||||
import arcade
|
||||
|
||||
# Constants
|
||||
SCREEN_WIDTH = 1000
|
||||
SCREEN_HEIGHT = 650
|
||||
SCREEN_TITLE = "Platformer"
|
||||
|
||||
# Constants used to scale our sprites from their original size
|
||||
CHARACTER_SCALING = 1
|
||||
TILE_SCALING = 0.5
|
||||
COIN_SCALING = 0.5
|
||||
SPRITE_PIXEL_SIZE = 128
|
||||
GRID_PIXEL_SIZE = (SPRITE_PIXEL_SIZE * TILE_SCALING)
|
||||
|
||||
# Movement speed of player, in pixels per frame
|
||||
PLAYER_MOVEMENT_SPEED = 10
|
||||
GRAVITY = 1
|
||||
PLAYER_JUMP_SPEED = 20
|
||||
|
||||
# How many pixels to keep as a minimum margin between the character
|
||||
# and the edge of the screen.
|
||||
LEFT_VIEWPORT_MARGIN = 200
|
||||
RIGHT_VIEWPORT_MARGIN = 200
|
||||
BOTTOM_VIEWPORT_MARGIN = 150
|
||||
TOP_VIEWPORT_MARGIN = 100
|
||||
|
||||
PLAYER_START_X = 64
|
||||
PLAYER_START_Y = 225
|
||||
|
||||
|
||||
class MyGame(arcade.Window):
|
||||
"""
|
||||
Main application class.
|
||||
"""
|
||||
|
||||
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.coin_list = None
|
||||
self.wall_list = None
|
||||
self.foreground_list = None
|
||||
self.background_list = None
|
||||
self.dont_touch_list = None
|
||||
self.player_list = None
|
||||
|
||||
# Separate variable that holds the player sprite
|
||||
self.player_sprite = None
|
||||
|
||||
# Our physics engine
|
||||
self.physics_engine = None
|
||||
|
||||
# Used to keep track of our scrolling
|
||||
self.view_bottom = 0
|
||||
self.view_left = 0
|
||||
|
||||
# Keep track of the score
|
||||
self.score = 0
|
||||
|
||||
# Where is the right edge of the map?
|
||||
self.end_of_map = 0
|
||||
|
||||
# Level
|
||||
self.level = 1
|
||||
|
||||
# Load sounds
|
||||
self.collect_coin_sound = arcade.load_sound("sounds/coin1.wav")
|
||||
self.jump_sound = arcade.load_sound("sounds/jump1.wav")
|
||||
self.game_over = arcade.load_sound("sounds/gameover1.wav")
|
||||
|
||||
def setup(self, level):
|
||||
""" Set up the game here. Call this function to restart the game. """
|
||||
|
||||
# Used to keep track of our scrolling
|
||||
self.view_bottom = 0
|
||||
self.view_left = 0
|
||||
|
||||
# Keep track of the score
|
||||
self.score = 0
|
||||
|
||||
# Create the Sprite lists
|
||||
self.player_list = arcade.SpriteList()
|
||||
self.foreground_list = arcade.SpriteList()
|
||||
self.background_list = arcade.SpriteList()
|
||||
self.wall_list = arcade.SpriteList()
|
||||
self.coin_list = arcade.SpriteList()
|
||||
|
||||
# Set up the player, specifically placing it at these coordinates.
|
||||
self.player_sprite = arcade.Sprite("images/player_1/player_stand.png",
|
||||
CHARACTER_SCALING)
|
||||
self.player_sprite.center_x = PLAYER_START_X
|
||||
self.player_sprite.center_y = PLAYER_START_Y
|
||||
self.player_list.append(self.player_sprite)
|
||||
|
||||
# --- Load in a map from the tiled editor ---
|
||||
|
||||
# Name of the layer in the file that has our platforms/walls
|
||||
platforms_layer_name = 'Platforms'
|
||||
# Name of the layer that has items for pick-up
|
||||
coins_layer_name = 'Coins'
|
||||
# Name of the layer that has items for foreground
|
||||
foreground_layer_name = 'Foreground'
|
||||
# Name of the layer that has items for background
|
||||
background_layer_name = 'Background'
|
||||
# Name of the layer that has items we shouldn't touch
|
||||
dont_touch_layer_name = "Don't Touch"
|
||||
|
||||
# Map name
|
||||
map_name = f"map2_level_{level}.tmx"
|
||||
|
||||
# Read in the tiled map
|
||||
my_map = arcade.tilemap.read_tmx(map_name)
|
||||
|
||||
# Calculate the right edge of the my_map in pixels
|
||||
self.end_of_map = my_map.map_size.width * GRID_PIXEL_SIZE
|
||||
|
||||
# -- Background
|
||||
self.background_list = arcade.tilemap.process_layer(my_map,
|
||||
background_layer_name,
|
||||
TILE_SCALING)
|
||||
|
||||
# -- Foreground
|
||||
self.foreground_list = arcade.tilemap.process_layer(my_map,
|
||||
foreground_layer_name,
|
||||
TILE_SCALING)
|
||||
|
||||
# -- Platforms
|
||||
self.wall_list = arcade.tilemap.process_layer(my_map,
|
||||
platforms_layer_name,
|
||||
TILE_SCALING)
|
||||
|
||||
# -- Coins
|
||||
self.coin_list = arcade.tilemap.process_layer(my_map,
|
||||
coins_layer_name,
|
||||
TILE_SCALING)
|
||||
|
||||
# -- Don't Touch Layer
|
||||
self.dont_touch_list = arcade.tilemap.process_layer(my_map,
|
||||
dont_touch_layer_name,
|
||||
TILE_SCALING)
|
||||
|
||||
# --- Other stuff
|
||||
# Set the background color
|
||||
if my_map.background_color:
|
||||
arcade.set_background_color(my_map.background_color)
|
||||
|
||||
# Create the 'physics engine'
|
||||
self.physics_engine = arcade.PhysicsEnginePlatformer(self.player_sprite,
|
||||
self.wall_list,
|
||||
GRAVITY)
|
||||
|
||||
def on_draw(self):
|
||||
""" Render the screen. """
|
||||
|
||||
# Clear the screen to the background color
|
||||
arcade.start_render()
|
||||
|
||||
# Draw our sprites
|
||||
self.wall_list.draw()
|
||||
self.background_list.draw()
|
||||
self.wall_list.draw()
|
||||
self.coin_list.draw()
|
||||
self.dont_touch_list.draw()
|
||||
self.player_list.draw()
|
||||
self.foreground_list.draw()
|
||||
|
||||
# Draw our score on the screen, scrolling it with the viewport
|
||||
score_text = f"Score: {self.score}"
|
||||
arcade.draw_text(score_text, 10 + self.view_left, 10 + self.view_bottom,
|
||||
arcade.csscolor.BLACK, 18)
|
||||
|
||||
def on_key_press(self, key, modifiers):
|
||||
"""Called whenever a key is pressed. """
|
||||
|
||||
if key == arcade.key.UP or key == arcade.key.W:
|
||||
if self.physics_engine.can_jump():
|
||||
self.player_sprite.change_y = PLAYER_JUMP_SPEED
|
||||
arcade.play_sound(self.jump_sound)
|
||||
elif key == arcade.key.LEFT or key == arcade.key.A:
|
||||
self.player_sprite.change_x = -PLAYER_MOVEMENT_SPEED
|
||||
elif key == arcade.key.RIGHT or key == arcade.key.D:
|
||||
self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED
|
||||
|
||||
def on_key_release(self, key, modifiers):
|
||||
"""Called when the user releases a key. """
|
||||
|
||||
if key == arcade.key.LEFT or key == arcade.key.A:
|
||||
self.player_sprite.change_x = 0
|
||||
elif key == arcade.key.RIGHT or key == arcade.key.D:
|
||||
self.player_sprite.change_x = 0
|
||||
|
||||
def update(self, delta_time):
|
||||
""" Movement and game logic """
|
||||
|
||||
# Call update on all sprites (The sprites don't do much in this
|
||||
# example though.)
|
||||
self.physics_engine.update()
|
||||
|
||||
# See if we hit any coins
|
||||
coin_hit_list = arcade.check_for_collision_with_list(self.player_sprite,
|
||||
self.coin_list)
|
||||
|
||||
# Loop through each coin we hit (if any) and remove it
|
||||
for coin in coin_hit_list:
|
||||
# Remove the coin
|
||||
coin.remove_from_sprite_lists()
|
||||
# Play a sound
|
||||
arcade.play_sound(self.collect_coin_sound)
|
||||
# Add one to the score
|
||||
self.score += 1
|
||||
|
||||
# Track if we need to change the viewport
|
||||
changed_viewport = False
|
||||
|
||||
# Did the player fall off the map?
|
||||
if self.player_sprite.center_y < -100:
|
||||
self.player_sprite.center_x = PLAYER_START_X
|
||||
self.player_sprite.center_y = PLAYER_START_Y
|
||||
|
||||
# Set the camera to the start
|
||||
self.view_left = 0
|
||||
self.view_bottom = 0
|
||||
changed_viewport = True
|
||||
arcade.play_sound(self.game_over)
|
||||
|
||||
# Did the player touch something they should not?
|
||||
if arcade.check_for_collision_with_list(self.player_sprite,
|
||||
self.dont_touch_list):
|
||||
self.player_sprite.change_x = 0
|
||||
self.player_sprite.change_y = 0
|
||||
self.player_sprite.center_x = PLAYER_START_X
|
||||
self.player_sprite.center_y = PLAYER_START_Y
|
||||
|
||||
# Set the camera to the start
|
||||
self.view_left = 0
|
||||
self.view_bottom = 0
|
||||
changed_viewport = True
|
||||
arcade.play_sound(self.game_over)
|
||||
|
||||
# See if the user got to the end of the level
|
||||
if self.player_sprite.center_x >= self.end_of_map:
|
||||
# Advance to the next level
|
||||
self.level += 1
|
||||
|
||||
# Load the next level
|
||||
self.setup(self.level)
|
||||
|
||||
# Set the camera to the start
|
||||
self.view_left = 0
|
||||
self.view_bottom = 0
|
||||
changed_viewport = True
|
||||
|
||||
# --- Manage Scrolling ---
|
||||
|
||||
# Scroll left
|
||||
left_boundary = self.view_left + LEFT_VIEWPORT_MARGIN
|
||||
if self.player_sprite.left < left_boundary:
|
||||
self.view_left -= left_boundary - self.player_sprite.left
|
||||
changed_viewport = True
|
||||
|
||||
# Scroll right
|
||||
right_boundary = self.view_left + SCREEN_WIDTH - RIGHT_VIEWPORT_MARGIN
|
||||
if self.player_sprite.right > right_boundary:
|
||||
self.view_left += self.player_sprite.right - right_boundary
|
||||
changed_viewport = True
|
||||
|
||||
# Scroll up
|
||||
top_boundary = self.view_bottom + SCREEN_HEIGHT - TOP_VIEWPORT_MARGIN
|
||||
if self.player_sprite.top > top_boundary:
|
||||
self.view_bottom += self.player_sprite.top - top_boundary
|
||||
changed_viewport = True
|
||||
|
||||
# Scroll down
|
||||
bottom_boundary = self.view_bottom + BOTTOM_VIEWPORT_MARGIN
|
||||
if self.player_sprite.bottom < bottom_boundary:
|
||||
self.view_bottom -= bottom_boundary - self.player_sprite.bottom
|
||||
changed_viewport = True
|
||||
|
||||
if changed_viewport:
|
||||
# Only scroll to integers. Otherwise we end up with pixels that
|
||||
# don't line up on the screen
|
||||
self.view_bottom = int(self.view_bottom)
|
||||
self.view_left = int(self.view_left)
|
||||
|
||||
# Do the scrolling
|
||||
arcade.set_viewport(self.view_left,
|
||||
SCREEN_WIDTH + self.view_left,
|
||||
self.view_bottom,
|
||||
SCREEN_HEIGHT + self.view_bottom)
|
||||
|
||||
|
||||
def main():
|
||||
""" Main method """
|
||||
window = MyGame()
|
||||
window.setup(window.level)
|
||||
arcade.run()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
293
arcade/examples/platform_tutorial/10_ladders_and_more.py
Normal file
@ -0,0 +1,293 @@
|
||||
"""
|
||||
Platformer Game
|
||||
"""
|
||||
import arcade
|
||||
import os
|
||||
|
||||
# Constants
|
||||
SCREEN_WIDTH = 1000
|
||||
SCREEN_HEIGHT = 650
|
||||
SCREEN_TITLE = "Platformer"
|
||||
|
||||
# Constants used to scale our sprites from their original size
|
||||
CHARACTER_SCALING = 1
|
||||
TILE_SCALING = 0.5
|
||||
COIN_SCALING = 0.5
|
||||
SPRITE_PIXEL_SIZE = 128
|
||||
GRID_PIXEL_SIZE = (SPRITE_PIXEL_SIZE * TILE_SCALING)
|
||||
|
||||
# Movement speed of player, in pixels per frame
|
||||
PLAYER_MOVEMENT_SPEED = 7
|
||||
GRAVITY = 1.5
|
||||
PLAYER_JUMP_SPEED = 30
|
||||
|
||||
# How many pixels to keep as a minimum margin between the character
|
||||
# and the edge of the screen.
|
||||
LEFT_VIEWPORT_MARGIN = 200
|
||||
RIGHT_VIEWPORT_MARGIN = 200
|
||||
BOTTOM_VIEWPORT_MARGIN = 150
|
||||
TOP_VIEWPORT_MARGIN = 100
|
||||
|
||||
PLAYER_START_X = 64
|
||||
PLAYER_START_Y = 256
|
||||
|
||||
|
||||
class MyGame(arcade.Window):
|
||||
"""
|
||||
Main application class.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
"""
|
||||
Initializer for the game
|
||||
"""
|
||||
|
||||
# Call the parent class and set up the window
|
||||
super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
|
||||
|
||||
# Set the path to start with this program
|
||||
file_path = os.path.dirname(os.path.abspath(__file__))
|
||||
os.chdir(file_path)
|
||||
|
||||
# These are 'lists' that keep track of our sprites. Each sprite should
|
||||
# go into a list.
|
||||
self.coin_list = None
|
||||
self.wall_list = None
|
||||
self.background_list = None
|
||||
self.ladder_list = None
|
||||
self.player_list = None
|
||||
|
||||
# Separate variable that holds the player sprite
|
||||
self.player_sprite = None
|
||||
|
||||
# Our 'physics' engine
|
||||
self.physics_engine = None
|
||||
|
||||
# Used to keep track of our scrolling
|
||||
self.view_bottom = 0
|
||||
self.view_left = 0
|
||||
|
||||
self.end_of_map = 0
|
||||
|
||||
# Keep track of the score
|
||||
self.score = 0
|
||||
|
||||
# Load sounds
|
||||
self.collect_coin_sound = arcade.load_sound("sounds/coin1.wav")
|
||||
self.jump_sound = arcade.load_sound("sounds/jump1.wav")
|
||||
self.game_over = arcade.load_sound("sounds/gameover1.wav")
|
||||
|
||||
def setup(self):
|
||||
""" Set up the game here. Call this function to restart the game. """
|
||||
|
||||
# Used to keep track of our scrolling
|
||||
self.view_bottom = 0
|
||||
self.view_left = 0
|
||||
|
||||
# Keep track of the score
|
||||
self.score = 0
|
||||
|
||||
# Create the Sprite lists
|
||||
self.player_list = arcade.SpriteList()
|
||||
self.background_list = arcade.SpriteList()
|
||||
self.wall_list = arcade.SpriteList()
|
||||
self.coin_list = arcade.SpriteList()
|
||||
|
||||
# Set up the player, specifically placing it at these coordinates.
|
||||
# self.player_sprite = arcade.Sprite("images/player_1/player_stand.png", CHARACTER_SCALING)
|
||||
self.player_sprite = \
|
||||
arcade.Sprite("../../../arcade/examples/platform_tutorial/images/player_1/player_stand.png")
|
||||
self.player_sprite.center_x = PLAYER_START_X
|
||||
self.player_sprite.center_y = PLAYER_START_Y
|
||||
self.player_list.append(self.player_sprite)
|
||||
|
||||
# --- Load in a map from the tiled editor ---
|
||||
|
||||
# Name of the layer in the file that has our platforms/walls
|
||||
platforms_layer_name = 'Platforms'
|
||||
moving_platforms_layer_name = 'Moving Platforms'
|
||||
|
||||
# Name of the layer that has items for pick-up
|
||||
coins_layer_name = 'Coins'
|
||||
|
||||
# Map name
|
||||
map_name = f"map_with_ladders.tmx"
|
||||
|
||||
# Read in the tiled map
|
||||
my_map = arcade.tilemap.read_tmx(map_name)
|
||||
|
||||
# Calculate the right edge of the my_map in pixels
|
||||
self.end_of_map = my_map.map_size.width * GRID_PIXEL_SIZE
|
||||
|
||||
# -- Platforms
|
||||
self.wall_list = arcade.tilemap.process_layer(my_map, platforms_layer_name, TILE_SCALING)
|
||||
|
||||
# -- Moving Platforms
|
||||
moving_platforms_list = arcade.tilemap.process_layer(my_map, moving_platforms_layer_name, TILE_SCALING)
|
||||
for sprite in moving_platforms_list:
|
||||
self.wall_list.append(sprite)
|
||||
|
||||
# -- Background objects
|
||||
self.background_list = arcade.tilemap.process_layer(my_map, "Background", TILE_SCALING)
|
||||
|
||||
# -- Background objects
|
||||
self.ladder_list = arcade.tilemap.process_layer(my_map, "Ladders", TILE_SCALING)
|
||||
|
||||
# -- Coins
|
||||
self.coin_list = arcade.tilemap.process_layer(my_map, coins_layer_name, TILE_SCALING)
|
||||
|
||||
# --- Other stuff
|
||||
# Set the background color
|
||||
if my_map.background_color:
|
||||
arcade.set_background_color(my_map.background_color)
|
||||
|
||||
# Create the 'physics engine'
|
||||
self.physics_engine = arcade.PhysicsEnginePlatformer(self.player_sprite,
|
||||
self.wall_list,
|
||||
gravity_constant=GRAVITY,
|
||||
ladders=self.ladder_list)
|
||||
|
||||
def on_draw(self):
|
||||
""" Render the screen. """
|
||||
|
||||
# Clear the screen to the background color
|
||||
arcade.start_render()
|
||||
|
||||
# Draw our sprites
|
||||
self.wall_list.draw()
|
||||
self.background_list.draw()
|
||||
self.ladder_list.draw()
|
||||
self.coin_list.draw()
|
||||
self.player_list.draw()
|
||||
|
||||
# Draw our score on the screen, scrolling it with the viewport
|
||||
score_text = f"Score: {self.score}"
|
||||
arcade.draw_text(score_text, 10 + self.view_left, 10 + self.view_bottom,
|
||||
arcade.csscolor.BLACK, 18)
|
||||
|
||||
def on_key_press(self, key, modifiers):
|
||||
"""Called whenever a key is pressed. """
|
||||
|
||||
if key == arcade.key.UP or key == arcade.key.W:
|
||||
if self.physics_engine.is_on_ladder():
|
||||
self.player_sprite.change_y = PLAYER_MOVEMENT_SPEED
|
||||
elif self.physics_engine.can_jump():
|
||||
self.player_sprite.change_y = PLAYER_JUMP_SPEED
|
||||
arcade.play_sound(self.jump_sound)
|
||||
elif key == arcade.key.DOWN or key == arcade.key.S:
|
||||
if self.physics_engine.is_on_ladder():
|
||||
self.player_sprite.change_y = -PLAYER_MOVEMENT_SPEED
|
||||
elif key == arcade.key.LEFT or key == arcade.key.A:
|
||||
self.player_sprite.change_x = -PLAYER_MOVEMENT_SPEED
|
||||
elif key == arcade.key.RIGHT or key == arcade.key.D:
|
||||
self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED
|
||||
|
||||
def on_key_release(self, key, modifiers):
|
||||
"""Called when the user releases a key. """
|
||||
|
||||
if key == arcade.key.UP or key == arcade.key.W:
|
||||
if self.physics_engine.is_on_ladder():
|
||||
self.player_sprite.change_y = 0
|
||||
elif key == arcade.key.DOWN or key == arcade.key.S:
|
||||
if self.physics_engine.is_on_ladder():
|
||||
self.player_sprite.change_y = 0
|
||||
elif key == arcade.key.LEFT or key == arcade.key.A:
|
||||
self.player_sprite.change_x = 0
|
||||
elif key == arcade.key.RIGHT or key == arcade.key.D:
|
||||
self.player_sprite.change_x = 0
|
||||
|
||||
def update(self, delta_time):
|
||||
""" Movement and game logic """
|
||||
|
||||
# Call update on all sprites (The sprites don't do much in this
|
||||
# example though.)
|
||||
self.physics_engine.update()
|
||||
|
||||
# Update animations
|
||||
self.coin_list.update_animation(delta_time)
|
||||
self.background_list.update_animation(delta_time)
|
||||
|
||||
# Update walls, used with moving platforms
|
||||
self.wall_list.update()
|
||||
|
||||
# See if the wall hit a boundary and needs to reverse direction.
|
||||
for wall in self.wall_list:
|
||||
|
||||
if wall.boundary_right and wall.right > wall.boundary_right and wall.change_x > 0:
|
||||
wall.change_x *= -1
|
||||
if wall.boundary_left and wall.left < wall.boundary_left and wall.change_x < 0:
|
||||
wall.change_x *= -1
|
||||
if wall.boundary_top and wall.top > wall.boundary_top and wall.change_y > 0:
|
||||
wall.change_y *= -1
|
||||
if wall.boundary_bottom and wall.bottom < wall.boundary_bottom and wall.change_y < 0:
|
||||
wall.change_y *= -1
|
||||
|
||||
# See if we hit any coins
|
||||
coin_hit_list = arcade.check_for_collision_with_list(self.player_sprite,
|
||||
self.coin_list)
|
||||
|
||||
# Loop through each coin we hit (if any) and remove it
|
||||
for coin in coin_hit_list:
|
||||
|
||||
# Figure out how many points this coin is worth
|
||||
if 'Points' not in coin.properties:
|
||||
print("Warning, collected a coing without a Points property.")
|
||||
else:
|
||||
points = int(coin.properties['Points'])
|
||||
self.score += points
|
||||
|
||||
# Remove the coin
|
||||
coin.remove_from_sprite_lists()
|
||||
arcade.play_sound(self.collect_coin_sound)
|
||||
|
||||
# Track if we need to change the viewport
|
||||
changed_viewport = False
|
||||
|
||||
# --- Manage Scrolling ---
|
||||
|
||||
# Scroll left
|
||||
left_boundary = self.view_left + LEFT_VIEWPORT_MARGIN
|
||||
if self.player_sprite.left < left_boundary:
|
||||
self.view_left -= left_boundary - self.player_sprite.left
|
||||
changed_viewport = True
|
||||
|
||||
# Scroll right
|
||||
right_boundary = self.view_left + SCREEN_WIDTH - RIGHT_VIEWPORT_MARGIN
|
||||
if self.player_sprite.right > right_boundary:
|
||||
self.view_left += self.player_sprite.right - right_boundary
|
||||
changed_viewport = True
|
||||
|
||||
# Scroll up
|
||||
top_boundary = self.view_bottom + SCREEN_HEIGHT - TOP_VIEWPORT_MARGIN
|
||||
if self.player_sprite.top > top_boundary:
|
||||
self.view_bottom += self.player_sprite.top - top_boundary
|
||||
changed_viewport = True
|
||||
|
||||
# Scroll down
|
||||
bottom_boundary = self.view_bottom + BOTTOM_VIEWPORT_MARGIN
|
||||
if self.player_sprite.bottom < bottom_boundary:
|
||||
self.view_bottom -= bottom_boundary - self.player_sprite.bottom
|
||||
changed_viewport = True
|
||||
|
||||
if changed_viewport:
|
||||
# Only scroll to integers. Otherwise we end up with pixels that
|
||||
# don't line up on the screen
|
||||
self.view_bottom = int(self.view_bottom)
|
||||
self.view_left = int(self.view_left)
|
||||
|
||||
# Do the scrolling
|
||||
arcade.set_viewport(self.view_left,
|
||||
SCREEN_WIDTH + self.view_left,
|
||||
self.view_bottom,
|
||||
SCREEN_HEIGHT + self.view_bottom)
|
||||
|
||||
|
||||
def main():
|
||||
""" Main method """
|
||||
window = MyGame()
|
||||
window.setup()
|
||||
arcade.run()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@ -0,0 +1,2 @@
|
||||
[InternetShortcut]
|
||||
URL=http://kenney.nl/support
|
||||
@ -0,0 +1,2 @@
|
||||
[InternetShortcut]
|
||||
URL=http://social.kenney.nl/facebook
|
||||
@ -0,0 +1,2 @@
|
||||
[InternetShortcut]
|
||||
URL=http://social.kenney.nl/twitter
|
||||
@ -0,0 +1,2 @@
|
||||
[InternetShortcut]
|
||||
URL=http://www.kenney.nl/
|
||||
17
arcade/examples/platform_tutorial/images/License.txt
Normal file
@ -0,0 +1,17 @@
|
||||
|
||||
|
||||
|
||||
Everything in this package is licensed CC0 (see below).
|
||||
|
||||
------------------------------
|
||||
|
||||
License (Creative Commons Zero, CC0)
|
||||
http://creativecommons.org/publicdomain/zero/1.0/
|
||||
|
||||
You may use these graphics in personal and commercial projects.
|
||||
Credit (Kenney or www.kenney.nl) would be nice but is not mandatory.
|
||||
|
||||
------------------------------
|
||||
|
||||
Donate: http://donate.kenney.nl/
|
||||
Request: http://request.kenney.nl/
|
||||
|
After Width: | Height: | Size: 4.6 KiB |
|
After Width: | Height: | Size: 4.6 KiB |
|
After Width: | Height: | Size: 5.0 KiB |
|
After Width: | Height: | Size: 5.0 KiB |
|
After Width: | Height: | Size: 5.1 KiB |
|
After Width: | Height: | Size: 5.0 KiB |
BIN
arcade/examples/platform_tutorial/images/enemies/bee.png
Normal file
|
After Width: | Height: | Size: 4.5 KiB |
BIN
arcade/examples/platform_tutorial/images/enemies/fishGreen.png
Normal file
|
After Width: | Height: | Size: 4.2 KiB |
BIN
arcade/examples/platform_tutorial/images/enemies/fishPink.png
Normal file
|
After Width: | Height: | Size: 4.1 KiB |
BIN
arcade/examples/platform_tutorial/images/enemies/fly.png
Normal file
|
After Width: | Height: | Size: 3.8 KiB |
BIN
arcade/examples/platform_tutorial/images/enemies/frog.png
Normal file
|
After Width: | Height: | Size: 3.9 KiB |
BIN
arcade/examples/platform_tutorial/images/enemies/frog_move.png
Normal file
|
After Width: | Height: | Size: 4.3 KiB |
BIN
arcade/examples/platform_tutorial/images/enemies/ladybug.png
Normal file
|
After Width: | Height: | Size: 4.0 KiB |
BIN
arcade/examples/platform_tutorial/images/enemies/mouse.png
Normal file
|
After Width: | Height: | Size: 3.2 KiB |
BIN
arcade/examples/platform_tutorial/images/enemies/saw.png
Normal file
|
After Width: | Height: | Size: 4.9 KiB |
BIN
arcade/examples/platform_tutorial/images/enemies/sawHalf.png
Normal file
|
After Width: | Height: | Size: 3.7 KiB |
BIN
arcade/examples/platform_tutorial/images/enemies/slimeBlock.png
Normal file
|
After Width: | Height: | Size: 2.9 KiB |
BIN
arcade/examples/platform_tutorial/images/enemies/slimeBlue.png
Normal file
|
After Width: | Height: | Size: 3.0 KiB |
|
After Width: | Height: | Size: 3.0 KiB |
BIN
arcade/examples/platform_tutorial/images/enemies/slimeGreen.png
Normal file
|
After Width: | Height: | Size: 3.0 KiB |
BIN
arcade/examples/platform_tutorial/images/enemies/slimePurple.png
Normal file
|
After Width: | Height: | Size: 3.0 KiB |
BIN
arcade/examples/platform_tutorial/images/enemies/wormGreen.png
Normal file
|
After Width: | Height: | Size: 3.1 KiB |
|
After Width: | Height: | Size: 3.2 KiB |
|
After Width: | Height: | Size: 3.2 KiB |
BIN
arcade/examples/platform_tutorial/images/enemies/wormPink.png
Normal file
|
After Width: | Height: | Size: 3.1 KiB |
BIN
arcade/examples/platform_tutorial/images/items/coinBronze.png
Normal file
|
After Width: | Height: | Size: 2.5 KiB |
BIN
arcade/examples/platform_tutorial/images/items/coinGold.png
Normal file
|
After Width: | Height: | Size: 2.3 KiB |
BIN
arcade/examples/platform_tutorial/images/items/coinSilver.png
Normal file
|
After Width: | Height: | Size: 2.5 KiB |
BIN
arcade/examples/platform_tutorial/images/items/flagGreen1.png
Normal file
|
After Width: | Height: | Size: 2.2 KiB |
BIN
arcade/examples/platform_tutorial/images/items/flagGreen2.png
Normal file
|
After Width: | Height: | Size: 2.3 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
BIN
arcade/examples/platform_tutorial/images/items/flagRed1.png
Normal file
|
After Width: | Height: | Size: 2.2 KiB |
BIN
arcade/examples/platform_tutorial/images/items/flagRed2.png
Normal file
|
After Width: | Height: | Size: 2.3 KiB |
BIN
arcade/examples/platform_tutorial/images/items/flagRed_down.png
Normal file
|
After Width: | Height: | Size: 1.4 KiB |
BIN
arcade/examples/platform_tutorial/images/items/flagYellow1.png
Normal file
|
After Width: | Height: | Size: 2.1 KiB |
BIN
arcade/examples/platform_tutorial/images/items/flagYellow2.png
Normal file
|
After Width: | Height: | Size: 2.2 KiB |
|
After Width: | Height: | Size: 1.2 KiB |
BIN
arcade/examples/platform_tutorial/images/items/gemBlue.png
Normal file
|
After Width: | Height: | Size: 1.3 KiB |
BIN
arcade/examples/platform_tutorial/images/items/gemGreen.png
Normal file
|
After Width: | Height: | Size: 1.3 KiB |
BIN
arcade/examples/platform_tutorial/images/items/gemRed.png
Normal file
|
After Width: | Height: | Size: 1.3 KiB |
BIN
arcade/examples/platform_tutorial/images/items/gemYellow.png
Normal file
|
After Width: | Height: | Size: 1.2 KiB |
BIN
arcade/examples/platform_tutorial/images/items/keyBlue.png
Normal file
|
After Width: | Height: | Size: 2.1 KiB |
BIN
arcade/examples/platform_tutorial/images/items/keyGreen.png
Normal file
|
After Width: | Height: | Size: 2.2 KiB |
BIN
arcade/examples/platform_tutorial/images/items/keyRed.png
Normal file
|
After Width: | Height: | Size: 2.0 KiB |
BIN
arcade/examples/platform_tutorial/images/items/keyYellow.png
Normal file
|
After Width: | Height: | Size: 1.8 KiB |
BIN
arcade/examples/platform_tutorial/images/items/ladderMid.png
Normal file
|
After Width: | Height: | Size: 731 B |
BIN
arcade/examples/platform_tutorial/images/items/ladderTop.png
Normal file
|
After Width: | Height: | Size: 1.3 KiB |
BIN
arcade/examples/platform_tutorial/images/items/star.png
Normal file
|
After Width: | Height: | Size: 1.6 KiB |
|
After Width: | Height: | Size: 3.9 KiB |
|
After Width: | Height: | Size: 5.6 KiB |
|
After Width: | Height: | Size: 5.3 KiB |
|
After Width: | Height: | Size: 4.7 KiB |
|
After Width: | Height: | Size: 4.5 KiB |
|
After Width: | Height: | Size: 3.4 KiB |
|
After Width: | Height: | Size: 4.8 KiB |
|
After Width: | Height: | Size: 4.2 KiB |
|
After Width: | Height: | Size: 4.6 KiB |
|
After Width: | Height: | Size: 4.1 KiB |
4
arcade/examples/platform_tutorial/images/readme.txt
Normal file
@ -0,0 +1,4 @@
|
||||
These assets are a subset of what is available from Kenney.nl.
|
||||
|
||||
If you like his work, please go support is by purchasing his full asset packs
|
||||
at https://kenney.nl/
|
||||
BIN
arcade/examples/platform_tutorial/images/tiles/boxCrate.png
Normal file
|
After Width: | Height: | Size: 1.2 KiB |
|
After Width: | Height: | Size: 2.5 KiB |
|
After Width: | Height: | Size: 2.0 KiB |
BIN
arcade/examples/platform_tutorial/images/tiles/brickBrown.png
Normal file
|
After Width: | Height: | Size: 1.8 KiB |
BIN
arcade/examples/platform_tutorial/images/tiles/brickGrey.png
Normal file
|
After Width: | Height: | Size: 1.8 KiB |
BIN
arcade/examples/platform_tutorial/images/tiles/bridgeA.png
Normal file
|
After Width: | Height: | Size: 3.3 KiB |
BIN
arcade/examples/platform_tutorial/images/tiles/bridgeB.png
Normal file
|
After Width: | Height: | Size: 1.4 KiB |
BIN
arcade/examples/platform_tutorial/images/tiles/bush.png
Normal file
|
After Width: | Height: | Size: 2.5 KiB |
BIN
arcade/examples/platform_tutorial/images/tiles/cactus.png
Normal file
|
After Width: | Height: | Size: 2.2 KiB |
BIN
arcade/examples/platform_tutorial/images/tiles/dirt.png
Normal file
|
After Width: | Height: | Size: 4.4 KiB |
BIN
arcade/examples/platform_tutorial/images/tiles/dirtCenter.png
Normal file
|
After Width: | Height: | Size: 3.3 KiB |
|
After Width: | Height: | Size: 4.0 KiB |
|
After Width: | Height: | Size: 4.0 KiB |
|
After Width: | Height: | Size: 4.0 KiB |
|
After Width: | Height: | Size: 4.6 KiB |
|
After Width: | Height: | Size: 4.5 KiB |
|
After Width: | Height: | Size: 3.3 KiB |
|
After Width: | Height: | Size: 3.3 KiB |
BIN
arcade/examples/platform_tutorial/images/tiles/dirtHalf.png
Normal file
|
After Width: | Height: | Size: 3.7 KiB |
BIN
arcade/examples/platform_tutorial/images/tiles/dirtHalf_left.png
Normal file
|
After Width: | Height: | Size: 3.4 KiB |
BIN
arcade/examples/platform_tutorial/images/tiles/dirtHalf_mid.png
Normal file
|
After Width: | Height: | Size: 3.0 KiB |
|
After Width: | Height: | Size: 3.4 KiB |
BIN
arcade/examples/platform_tutorial/images/tiles/dirtHill_left.png
Normal file
|
After Width: | Height: | Size: 2.1 KiB |
|
After Width: | Height: | Size: 2.1 KiB |
BIN
arcade/examples/platform_tutorial/images/tiles/dirtLeft.png
Normal file
|
After Width: | Height: | Size: 4.1 KiB |
BIN
arcade/examples/platform_tutorial/images/tiles/dirtMid.png
Normal file
|
After Width: | Height: | Size: 3.8 KiB |