Skip to content

Commit

Permalink
Issue pythonarcade#151. Work on adding example of player going betwee…
Browse files Browse the repository at this point in the history
…n different rooms.
  • Loading branch information
pvcraven committed Dec 9, 2017
1 parent a4bfac5 commit 5f33526
Show file tree
Hide file tree
Showing 4 changed files with 256 additions and 3 deletions.
13 changes: 10 additions & 3 deletions doc/examples/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,17 @@ Sprite Movement
Levels
^^^^^^

.. toctree::
:maxdepth: 3
.. figure:: thumbs/sprite_collect_coins_diff_levels.gif
:figwidth: 170px

:ref:`example-sprite-collect-coins-diff-levels`

.. figure:: thumbs/sprite_rooms.png
:figwidth: 170px

:ref:`sprite_rooms`


sprite_collect_coins_diff_levels

Bullets
^^^^^^^
Expand Down
Binary file added doc/examples/sprite_rooms.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions doc/examples/sprite_rooms.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
:orphan:

.. _sprite_rooms:

Moving Between Different Rooms
==============================

.. image:: sprite_rooms.png
:width: 600px
:height: 600px
:align: center
:alt: Screenshot of moving between rooms with sprites

.. literalinclude:: ../../examples/sprite_rooms.py
:caption: sprite_rooms.py
:linenos:
230 changes: 230 additions & 0 deletions examples/sprite_rooms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
"""
Sprite move between different rooms.
Artwork from http://kenney.nl
"""
import arcade

SPRITE_SCALING = 0.5
SPRITE_NATIVE_SIZE = 128
SPRITE_SIZE = int(SPRITE_NATIVE_SIZE * SPRITE_SCALING)

SCREEN_WIDTH = SPRITE_SIZE * 14
SCREEN_HEIGHT = SPRITE_SIZE * 10

MOVEMENT_SPEED = 5


class Room:
"""
This class holds all the information about the
different rooms.
"""
def __init__(self):
# You may want many lists. Lists for coins, monsters, etc.
self.wall_list = None

# This holds the background images. If you don't want changing
# background images, you can delete this part.
self.background = None


def setup_room_1():
"""
Create and return room 1.
If your program gets large, you may want to separate this into different
files.
"""
room = Room()

""" Set up the game and initialize the variables. """
# Sprite lists
room.wall_list = arcade.SpriteList()

# -- Set up the walls
# Create bottom and top row of boxes
# This y loops a list of two, the coordinate 0, and just under the top of window
for y in (0, SCREEN_HEIGHT - SPRITE_SIZE):
# Loop for each box going across
for x in range(0, SCREEN_WIDTH, SPRITE_SIZE):
wall = arcade.Sprite("images/boxCrate_double.png", SPRITE_SCALING)
wall.left = x
wall.bottom = y
room.wall_list.append(wall)

# Create left and right column of boxes
for x in (0, SCREEN_WIDTH - SPRITE_SIZE):
# Loop for each box going across
for y in range(SPRITE_SIZE, SCREEN_HEIGHT - SPRITE_SIZE, SPRITE_SIZE):
# Skip making a block 4 and 5 blocks up on the right side
if (y != SPRITE_SIZE * 4 and y != SPRITE_SIZE * 5) or x == 0:
wall = arcade.Sprite("images/boxCrate_double.png", SPRITE_SCALING)
wall.left = x
wall.bottom = y
room.wall_list.append(wall)

wall = arcade.Sprite("images/boxCrate_double.png", SPRITE_SCALING)
wall.left = 7 * SPRITE_SIZE
wall.bottom = 5 * SPRITE_SIZE
room.wall_list.append(wall)

# If you want coins or monsters in a level, then add that code here.

# Load the background image for this level.
room.background = arcade.load_texture("images/background.jpg")

return room


def setup_room_2():
"""
Create and return room 2.
"""
room = Room()

""" Set up the game and initialize the variables. """
# Sprite lists
room.wall_list = arcade.SpriteList()

# -- Set up the walls
# Create bottom and top row of boxes
# This y loops a list of two, the coordinate 0, and just under the top of window
for y in (0, SCREEN_HEIGHT - SPRITE_SIZE):
# Loop for each box going across
for x in range(0, SCREEN_WIDTH, SPRITE_SIZE):
wall = arcade.Sprite("images/boxCrate_double.png", SPRITE_SCALING)
wall.left = x
wall.bottom = y
room.wall_list.append(wall)

# Create left and right column of boxes
for x in (0, SCREEN_WIDTH - SPRITE_SIZE):
# Loop for each box going across
for y in range(SPRITE_SIZE, SCREEN_HEIGHT - SPRITE_SIZE, SPRITE_SIZE):
# Skip making a block 4 and 5 blocks up
if (y != SPRITE_SIZE * 4 and y != SPRITE_SIZE * 5) or x != 0:
wall = arcade.Sprite("images/boxCrate_double.png", SPRITE_SCALING)
wall.left = x
wall.bottom = y
room.wall_list.append(wall)

wall = arcade.Sprite("images/boxCrate_double.png", SPRITE_SCALING)
wall.left = 5 * SPRITE_SIZE
wall.bottom = 6 * SPRITE_SIZE
room.wall_list.append(wall)
room.background = arcade.load_texture("images/background_2.jpg")

return room


class MyGame(arcade.Window):
""" Main application class. """

def __init__(self, width, height):
"""
Initializer
"""
super().__init__(width, height)
# Sprite lists
self.current_room = 0

# Set up the player
self.rooms = None
self.score = 0
self.player_sprite = None
self.physics_engine = None

def setup(self):
""" Set up the game and initialize the variables. """
# Set up the player
self.score = 0
self.player_sprite = arcade.Sprite("images/character.png", SPRITE_SCALING)
self.player_sprite.center_x = 100
self.player_sprite.center_y = 100

# Our list of rooms
self.rooms = []

# Create the rooms. Extend the pattern for each room.
room = setup_room_1()
self.rooms.append(room)

room = setup_room_2()
self.rooms.append(room)

# Our starting room number
self.current_room = 0

# Create a physics engine for this room
self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite, self.rooms[self.current_room].wall_list)

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

# This command has to happen before we start drawing
arcade.start_render()

# Draw the background texture
arcade.draw_texture_rectangle(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2,
SCREEN_WIDTH, SCREEN_HEIGHT, self.rooms[self.current_room].background)

# Draw all the walls in this room
self.rooms[self.current_room].wall_list.draw()

# If you have coins or monsters, then copy and modify the line
# above for each list.

self.player_sprite.draw()

def on_key_press(self, key, modifiers):
"""Called whenever a key is pressed. """

if key == arcade.key.UP:
self.player_sprite.change_y = MOVEMENT_SPEED
elif key == arcade.key.DOWN:
self.player_sprite.change_y = -MOVEMENT_SPEED
elif key == arcade.key.LEFT:
self.player_sprite.change_x = -MOVEMENT_SPEED
elif key == arcade.key.RIGHT:
self.player_sprite.change_x = 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.DOWN:
self.player_sprite.change_y = 0
elif key == arcade.key.LEFT or key == arcade.key.RIGHT:
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()

# Do some logic here to figure out what room we are in, and if we need to go
# to a different room.
if self.player_sprite.center_x > SCREEN_WIDTH and self.current_room == 0:
self.current_room = 1
self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite,
self.rooms[self.current_room].wall_list)
self.player_sprite.center_x = 0
elif self.player_sprite.center_x < 0 and self.current_room == 1:
self.current_room = 0
self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite,
self.rooms[self.current_room].wall_list)
self.player_sprite.center_x = SCREEN_WIDTH


def main():
""" Main method """
window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT)
window.setup()
arcade.run()


if __name__ == "__main__":
main()

0 comments on commit 5f33526

Please sign in to comment.