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#
    """

    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

        # Separate variable that holds the player sprite
        self.player_sprite = 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#
        arcade.set_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(
                ":resources:images/tiles/boxCrate_double.png", TILE_SCALING
            )

Lastly in our on_draw function we can draw the scene.

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

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

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

Source Code#

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