Step 6 - Resetting

You might have noticed that throughout this tutorial, there has been a setup function in our Window class. So far, we haven’t used this function at all, so what is it for?

Let’s imagine that we want a way to “reset” our game to it’s initial state. This could be because the player lost, and we want to restart the game, or perhaps we just want to give the player the option to restart.

With our current architecture of creating everything in our __init__ function, we would have to duplicate all of that logic in another function in order to make that happen, or completely re-create our Window, which will be an unpleasent experience for a player.

In this chapter, we will do a small amount of re-organizing our existing code to make use of this setup function in a way that allows to simply call the setup function whenever we want our game to return to it’s original state.

First off, we will change our __init__ function to look like below. We are setting values to something like None, 0, or similar. The purpose of this step is to ensure that the attributes are created on the class. In Python, we cannot add new attributes to a class outside of the __init__ function.

def __init__(self):

    super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)

    self.player_texture = None
    self.player_sprite = None
    self.player_list = None

    self.wall_list = None

Next we will move the actual creation of these objects into our setup function. This looks almost identical to our original __init__ function. Try and move these sections of code on your own, if you get stuck you can see the setup function in the full source code listing below.

The last thing we need to do is create a way to reset the game. For now we’ll add a simple key press to do it. Add the following in your on_key_press function to reset the game when the Escape key is pressed.

if key == arcade.key.ESCAPE:
    self.setup()

Source Code

Resetting
  1"""
  2Platformer Game
  3
  4python -m arcade.examples.platform_tutorial.06_reset
  5"""
  6import arcade
  7
  8# Constants
  9WINDOW_WIDTH = 1280
 10WINDOW_HEIGHT = 720
 11WINDOW_TITLE = "Platformer"
 12
 13# Constants used to scale our sprites from their original size
 14TILE_SCALING = 0.5
 15
 16# Movement speed of player, in pixels per frame
 17PLAYER_MOVEMENT_SPEED = 5
 18GRAVITY = 1
 19PLAYER_JUMP_SPEED = 20
 20
 21
 22class GameView(arcade.Window):
 23    """
 24    Main application class.
 25    """
 26
 27    def __init__(self):
 28
 29        # Call the parent class and set up the window
 30        super().__init__(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE)
 31
 32        # Variable to hold our texture for our player
 33        self.player_texture = None
 34
 35        # Separate variable that holds the player sprite
 36        self.player_sprite = None
 37
 38        # SpriteList for our player
 39        self.player_list = None
 40
 41        # SpriteList for our boxes and ground
 42        # Putting our ground and box Sprites in the same SpriteList
 43        # will make it easier to perform collision detection against
 44        # them later on. Setting the spatial hash to True will make
 45        # collision detection much faster if the objects in this
 46        # SpriteList do not move.
 47        self.wall_list = None
 48
 49    def setup(self):
 50        """Set up the game here. Call this function to restart the game."""
 51        self.player_texture = arcade.load_texture(
 52            ":resources:images/animated_characters/female_adventurer/femaleAdventurer_idle.png"
 53        )
 54
 55        self.player_sprite = arcade.Sprite(self.player_texture)
 56        self.player_sprite.center_x = 64
 57        self.player_sprite.center_y = 128
 58
 59        self.player_list = arcade.SpriteList()
 60        self.player_list.append(self.player_sprite)
 61
 62        self.wall_list = arcade.SpriteList(use_spatial_hash=True)
 63
 64        # Create the ground
 65        # This shows using a loop to place multiple sprites horizontally
 66        for x in range(0, 1250, 64):
 67            wall = arcade.Sprite(":resources:images/tiles/grassMid.png", scale=TILE_SCALING)
 68            wall.center_x = x
 69            wall.center_y = 32
 70            self.wall_list.append(wall)
 71
 72        # Put some crates on the ground
 73        # This shows using a coordinate list to place sprites
 74        coordinate_list = [[512, 96], [256, 96], [768, 96]]
 75
 76        for coordinate in coordinate_list:
 77            # Add a crate on the ground
 78            wall = arcade.Sprite(
 79                ":resources:images/tiles/boxCrate_double.png", scale=TILE_SCALING
 80            )
 81            wall.position = coordinate
 82            self.wall_list.append(wall)
 83
 84        # Create a Platformer Physics Engine, this will handle moving our
 85        # player as well as collisions between the player sprite and
 86        # whatever SpriteList we specify for the walls.
 87        # It is important to supply static to the walls parameter. There is a
 88        # platforms parameter that is intended for moving platforms.
 89        # If a platform is supposed to move, and is added to the walls list,
 90        # it will not be moved.
 91        self.physics_engine = arcade.PhysicsEnginePlatformer(
 92            self.player_sprite, walls=self.wall_list, gravity_constant=GRAVITY
 93        )
 94
 95        self.background_color = arcade.csscolor.CORNFLOWER_BLUE
 96
 97    def on_draw(self):
 98        """Render the screen."""
 99
100        # Clear the screen to the background color
101        self.clear()
102
103        # Draw our sprites
104        self.player_list.draw()
105        self.wall_list.draw()
106
107    def on_update(self, delta_time):
108        """Movement and Game Logic"""
109
110        # Move the player using our physics engine
111        self.physics_engine.update()
112
113    def on_key_press(self, key, modifiers):
114        """Called whenever a key is pressed."""
115
116        if key == arcade.key.ESCAPE:
117            self.setup()
118
119        if key == arcade.key.UP or key == arcade.key.W:
120            if self.physics_engine.can_jump():
121                self.player_sprite.change_y = PLAYER_JUMP_SPEED
122
123        if key == arcade.key.LEFT or key == arcade.key.A:
124            self.player_sprite.change_x = -PLAYER_MOVEMENT_SPEED
125        elif key == arcade.key.RIGHT or key == arcade.key.D:
126            self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED
127
128    def on_key_release(self, key, modifiers):
129        """Called whenever a key is released."""
130
131        if key == arcade.key.LEFT or key == arcade.key.A:
132            self.player_sprite.change_x = 0
133        elif key == arcade.key.RIGHT or key == arcade.key.D:
134            self.player_sprite.change_x = 0
135
136
137def main():
138    """Main function"""
139    window = GameView()
140    window.setup()
141    arcade.run()
142
143
144if __name__ == "__main__":
145    main()

Run This Chapter

python -m arcade.examples.platform_tutorial.06_reset