menu_02.py Full Listing
1"""
2Menu.
3
4Shows the usage of almost every gui widget, switching views and making a modal.
5"""
6
7import arcade
8import arcade.gui
9
10# Screen title and size
11SCREEN_WIDTH = 800
12SCREEN_HEIGHT = 600
13SCREEN_TITLE = "Making a Menu"
14
15
16class MainView(arcade.View):
17 """This is the class where your normal game would go."""
18
19 def __init__(self):
20 super().__init__()
21 self.manager = arcade.gui.UIManager()
22
23 switch_menu_button = arcade.gui.UIFlatButton(text="Pause", width=250)
24
25 # Initialise the button with an on_click event.
26 @switch_menu_button.event("on_click")
27 def on_click_switch_button(event):
28 # Passing the main view into menu view as an argument.
29 menu_view = MenuView(self)
30 self.window.show_view(menu_view)
31
32 # Use the anchor to position the button on the screen.
33 self.anchor = self.manager.add(arcade.gui.UIAnchorLayout())
34
35 self.anchor.add(
36 anchor_x="center_x",
37 anchor_y="center_y",
38 child=switch_menu_button,
39 )
40
41 def on_show_view(self):
42 """This is run once when we switch to this view"""
43 arcade.set_background_color(arcade.color.DARK_BLUE_GRAY)
44
45 # Enable the UIManager when the view is showm.
46 self.manager.enable()
47
48 def on_hide_view(self):
49 # Disable the UIManager when the view is hidden.
50 self.manager.disable()
51
52 def on_draw(self):
53 """Render the screen."""
54 # Clear the screen
55 self.clear()
56
57 # Draw the manager.
58 self.manager.draw()
59
60
61class MenuView(arcade.View):
62 """Main menu view class."""
63
64 def __init__(self, main_view):
65 super().__init__()
66
67 self.manager = arcade.gui.UIManager()
68
69 self.main_view = main_view
70
71 def on_hide_view(self):
72 # Disable the UIManager when the view is hidden.
73 self.manager.disable()
74
75 def on_show_view(self):
76 """This is run once when we switch to this view"""
77
78 # Makes the background darker
79 arcade.set_background_color([rgb - 50 for rgb in arcade.color.DARK_BLUE_GRAY])
80
81 self.manager.enable()
82
83 def on_draw(self):
84 """Render the screen."""
85
86 # Clear the screen
87 self.clear()
88 self.manager.draw()
89
90
91def main():
92 window = arcade.Window(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE, resizable=True)
93 main_view = MainView()
94 window.show_view(main_view)
95 arcade.run()
96
97
98if __name__ == "__main__":
99 main()