Step 3 - Scene Object#

Next we will add a Scene to our game. A Scene is a tool to manage a number of different SpriteLists by assigning each one a name, and maintaining a draw order.

SpriteLists can be drawn directly like we saw in step 2 of this tutorial, but a Scene can be helpful to handle a lot of different lists at once and being able to draw them all with one call to the scene.

To start with we will remove our sprite lists from the __init__ function, and replace them with a scene object.

03_scene_object.py - Scene Object Definition#
    """
    Main application class.
    """

    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

Next we will initialize the scene object in the setup function and then add the SpriteLists to it instead of creating new SpriteList objects directly.

Then instead of appending the Sprites to the SpriteLists directly, we can add them to the Scene and specify by name what SpriteList we want them added to.

03_scene_object.py - Add SpriteLists to the Scene#
        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.center_x = 64
        self.player_sprite.center_y = 128
        self.scene.add_sprite("Player", 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.scene.add_sprite("Walls", 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(

Lastly in our on_draw function we can draw the scene.

03_scene_object.py - Draw the Scene#
            )
            wall.position = coordinate
            self.scene.add_sprite("Walls", wall)

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

        # Clear the screen to the background color

Source Code#

03_scene_object - Scene Object#
 1"""
 2Platformer Game
 3
 4python -m arcade.examples.platform_tutorial.03_scene_object
 5"""
 6from __future__ import annotations
 7
 8import arcade
 9
10# Constants
11SCREEN_WIDTH = 1000
12SCREEN_HEIGHT = 650
13SCREEN_TITLE = "Platformer"
14
15# Constants used to scale our sprites from their original size
16CHARACTER_SCALING = 1
17TILE_SCALING = 0.5
18
19
20class MyGame(arcade.Window):
21    """
22    Main application class.
23    """
24
25    def __init__(self):
26
27        # Call the parent class and set up the window
28        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
29
30        # Our Scene Object
31        self.scene = None
32
33        # Separate variable that holds the player sprite
34        self.player_sprite = None
35
36        self.background_color = arcade.csscolor.CORNFLOWER_BLUE
37
38    def setup(self):
39        """Set up the game here. Call this function to restart the game."""
40
41        # Initialize Scene
42        self.scene = arcade.Scene()
43
44        # Create the Sprite lists
45        self.scene.add_sprite_list("Player")
46        self.scene.add_sprite_list("Walls", use_spatial_hash=True)
47
48        # Set up the player, specifically placing it at these coordinates.
49        image_source = ":resources:images/animated_characters/female_adventurer/femaleAdventurer_idle.png"
50        self.player_sprite = arcade.Sprite(image_source, CHARACTER_SCALING)
51        self.player_sprite.center_x = 64
52        self.player_sprite.center_y = 128
53        self.scene.add_sprite("Player", self.player_sprite)
54
55        # Create the ground
56        # This shows using a loop to place multiple sprites horizontally
57        for x in range(0, 1250, 64):
58            wall = arcade.Sprite(":resources:images/tiles/grassMid.png", TILE_SCALING)
59            wall.center_x = x
60            wall.center_y = 32
61            self.scene.add_sprite("Walls", wall)
62
63        # Put some crates on the ground
64        # This shows using a coordinate list to place sprites
65        coordinate_list = [[512, 96], [256, 96], [768, 96]]
66
67        for coordinate in coordinate_list:
68            # Add a crate on the ground
69            wall = arcade.Sprite(
70                ":resources:images/tiles/boxCrate_double.png", TILE_SCALING
71            )
72            wall.position = coordinate
73            self.scene.add_sprite("Walls", wall)
74
75    def on_draw(self):
76        """Render the screen."""
77
78        # Clear the screen to the background color
79        self.clear()
80
81        # Draw our Scene
82        self.scene.draw()
83
84
85def main():
86    """Main function"""
87    window = MyGame()
88    window.setup()
89    arcade.run()
90
91
92if __name__ == "__main__":
93    main()