Minimal Views Example
You might also want to check out Using Views for Start/End Screens.
view_screens_minimal.py
1"""
2This program shows how to:
3 * Display a sequence of screens in your game. The "arcade.View"
4 class makes it easy to separate the code for each screen into
5 its own class.
6 * This example shows the absolute basics of using "arcade.View".
7 See the "different_screens_example.py" for how to handle
8 screen-specific data.
9
10Make a separate class for each view (screen) in your game.
11The class will inherit from arcade.View. The structure will
12look like an arcade.Window as each View will need to have its own draw,
13update and window event methods. To switch a View, simply create a View
14with `view = MyView()` and then use the "self.window.set_view(view)" method.
15
16If Python and Arcade are installed, this example can be run from the command line with:
17python -m arcade.examples.view_screens_minimal
18"""
19
20import arcade
21
22
23WIDTH = 1280
24HEIGHT = 720
25
26
27class MenuView(arcade.View):
28 """ Class that manages the 'menu' view. """
29
30 def on_show_view(self):
31 """ Called when switching to this view"""
32 self.window.background_color = arcade.color.WHITE
33
34 def on_draw(self):
35 """ Draw the menu """
36 self.clear()
37 arcade.draw_text("Menu Screen - click to advance", WIDTH / 2, HEIGHT / 2,
38 arcade.color.BLACK, font_size=30, anchor_x="center")
39
40 def on_mouse_press(self, _x, _y, _button, _modifiers):
41 """ Use a mouse press to advance to the 'game' view. """
42 game_view = GameView()
43 game_view.setup()
44 self.window.show_view(game_view)
45
46
47class GameView(arcade.View):
48 """ Manage the 'game' view for our program. """
49
50 def __init__(self):
51 super().__init__()
52 # Create variables here
53
54 def setup(self):
55 """ This should set up your game and get it ready to play """
56 # Replace 'pass' with the code to set up your game
57 pass
58
59 def on_show_view(self):
60 """ Called when switching to this view"""
61 self.background_color = arcade.color.ORANGE_PEEL
62
63 def on_draw(self):
64 """ Draw everything for the game. """
65 self.clear()
66 arcade.draw_text("Game - press SPACE to advance", WIDTH / 2, HEIGHT / 2,
67 arcade.color.BLACK, font_size=30, anchor_x="center")
68
69 def on_key_press(self, key, _modifiers):
70 """ Handle key presses. In this case, we'll just count a 'space' as
71 game over and advance to the game over view. """
72 if key == arcade.key.SPACE:
73 game_over_view = GameOverView()
74 self.window.show_view(game_over_view)
75
76
77class GameOverView(arcade.View):
78 """ Class to manage the game over view """
79 def on_show_view(self):
80 """ Called when switching to this view"""
81 self.window.background_color = arcade.color.BLACK
82
83 def on_draw(self):
84 """ Draw the game over view """
85 self.clear()
86 arcade.draw_text("Game Over - press ESCAPE to advance", WIDTH / 2, HEIGHT / 2,
87 arcade.color.WHITE, 30, anchor_x="center")
88
89 def on_key_press(self, key, _modifiers):
90 """ If user hits escape, go back to the main menu view """
91 if key == arcade.key.ESCAPE:
92 menu_view = MenuView()
93 self.window.show_view(menu_view)
94
95
96def main():
97 """ Startup """
98 window = arcade.Window(WIDTH, HEIGHT, "Different Views Minimal Example")
99 menu_view = MenuView()
100 window.show_view(menu_view)
101 arcade.run()
102
103
104if __name__ == "__main__":
105 main()