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