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 = 1280
17HEIGHT = 720
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(
41 ":resources:images/animated_characters/female_person/femalePerson_idle.png",
42 scale=SPRITE_SCALING,
43 )
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 arcade.draw_sprite(self.player_sprite)
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 arcade.draw_sprite(player_sprite)
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()