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