Using Views for a Pause Screen

You might also want to check out Using Views for Start/End Screens.

view_pause_screen.py
  1"""
  2This program shows how to have a pause screen without resetting the game.
  3
  4Make a separate class for each view (screen) in your game.
  5The class will inherit from arcade.View. The structure will
  6look like an arcade.Window as each View will need to have its own draw,
  7update and window event methods. To switch a View, simply create a view
  8with `view = MyView()` and then use the "self.window.set_view(view)" method.
  9
 10If Python and Arcade are installed, this example can be run from the command line with:
 11python -m arcade.examples.view_pause_screen
 12"""
 13
 14import arcade
 15
 16WIDTH = 800
 17HEIGHT = 600
 18SPRITE_SCALING = 0.5
 19
 20
 21class MenuView(arcade.View):
 22    def on_show_view(self):
 23        self.window.background_color = arcade.color.WHITE
 24
 25    def on_draw(self):
 26        self.clear()
 27        arcade.draw_text("Menu Screen", WIDTH / 2, HEIGHT / 2,
 28                         arcade.color.BLACK, font_size=50, anchor_x="center")
 29        arcade.draw_text("Click to advance.", WIDTH / 2, HEIGHT / 2 - 75,
 30                         arcade.color.GRAY, font_size=20, anchor_x="center")
 31
 32    def on_mouse_press(self, _x, _y, _button, _modifiers):
 33        game = GameView()
 34        self.window.show_view(game)
 35
 36
 37class GameView(arcade.View):
 38    def __init__(self):
 39        super().__init__()
 40        self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png",
 41                                           scale=SPRITE_SCALING)
 42        self.player_sprite.center_x = 50
 43        self.player_sprite.center_y = 50
 44        self.player_sprite.velocity = [3, 3]
 45
 46    def on_show_view(self):
 47        self.window.background_color = arcade.color.AMAZON
 48
 49    def on_draw(self):
 50        self.clear()
 51        # Draw all the sprites.
 52        self.player_sprite.draw()
 53
 54        # Show tip to pause screen
 55        arcade.draw_text("Press Esc. to pause",
 56                         WIDTH / 2,
 57                         HEIGHT - 100,
 58                         arcade.color.BLACK,
 59                         font_size=20,
 60                         anchor_x="center")
 61
 62    def on_update(self, delta_time):
 63        # Call update on all sprites
 64        self.player_sprite.update()
 65
 66        # Bounce off the edges
 67        if self.player_sprite.left < 0 or self.player_sprite.right > WIDTH:
 68            self.player_sprite.change_x *= -1
 69        if self.player_sprite.bottom < 0 or self.player_sprite.top > HEIGHT:
 70            self.player_sprite.change_y *= -1
 71
 72    def on_key_press(self, key, _modifiers):
 73        if key == arcade.key.ESCAPE:
 74            # pass self, the current view, to preserve this view's state
 75            pause = PauseView(self)
 76            self.window.show_view(pause)
 77
 78
 79class PauseView(arcade.View):
 80    def __init__(self, game_view):
 81        super().__init__()
 82        self.game_view = game_view
 83
 84    def on_show_view(self):
 85        self.window.background_color = arcade.color.ORANGE
 86
 87    def on_draw(self):
 88        self.clear()
 89
 90        # Draw player, for effect, on pause screen.
 91        # The previous View (GameView) was passed in
 92        # and saved in self.game_view.
 93        player_sprite = self.game_view.player_sprite
 94        player_sprite.draw()
 95
 96        # draw an orange filter over him
 97        arcade.draw_lrbt_rectangle_filled(left=player_sprite.left,
 98                                          right=player_sprite.right,
 99                                          bottom=player_sprite.bottom,
100                                          top=player_sprite.top,
101                                          color=arcade.color.ORANGE[:3] + (200,))
102
103        arcade.draw_text("PAUSED", WIDTH / 2, HEIGHT / 2 + 50,
104                         arcade.color.BLACK, font_size=50, anchor_x="center")
105
106        # Show tip to return or reset
107        arcade.draw_text("Press Esc. to return",
108                         WIDTH / 2,
109                         HEIGHT / 2,
110                         arcade.color.BLACK,
111                         font_size=20,
112                         anchor_x="center")
113        arcade.draw_text("Press Enter to reset",
114                         WIDTH / 2,
115                         HEIGHT / 2 - 30,
116                         arcade.color.BLACK,
117                         font_size=20,
118                         anchor_x="center")
119
120    def on_key_press(self, key, _modifiers):
121        if key == arcade.key.ESCAPE:   # resume game
122            self.window.show_view(self.game_view)
123        elif key == arcade.key.ENTER:  # reset game
124            game = GameView()
125            self.window.show_view(game)
126
127
128def main():
129    window = arcade.Window(WIDTH, HEIGHT, "Instruction and Game Over Views Example")
130    menu = MenuView()
131    window.show_view(menu)
132    arcade.run()
133
134
135if __name__ == "__main__":
136    main()