menu_03.py Full Listing
menu_03.py
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
22 self.manager = arcade.gui.UIManager()
23
24 switch_menu_button = arcade.gui.UIFlatButton(text="Pause", width=150)
25
26 # Initialise the button with an on_click event.
27 @switch_menu_button.event("on_click")
28 def on_click_switch_button(event):
29 # Passing the main view into menu view as an argument.
30 menu_view = MenuView(self)
31 self.window.show_view(menu_view)
32
33 # Use the anchor to position the button on the screen.
34 self.anchor = self.manager.add(arcade.gui.UIAnchorLayout())
35
36 self.anchor.add(
37 anchor_x="center_x",
38 anchor_y="center_y",
39 child=switch_menu_button,
40 )
41
42 def on_hide_view(self):
43 # Disable the UIManager when the view is hidden.
44 self.manager.disable()
45
46 def on_show_view(self):
47 """This is run once when we switch to this view"""
48 arcade.set_background_color(arcade.color.DARK_BLUE_GRAY)
49
50 # Enable the UIManager when the view is showm.
51 self.manager.enable()
52
53 def on_draw(self):
54 """Render the screen."""
55 # Clear the screen
56 self.clear()
57
58 # Draw the manager.
59 self.manager.draw()
60
61
62class MenuView(arcade.View):
63 """Main menu view class."""
64
65 def __init__(self, main_view):
66 super().__init__()
67
68 self.manager = arcade.gui.UIManager()
69
70 resume = arcade.gui.UIFlatButton(text="Resume", width=150)
71 start_new_game = arcade.gui.UIFlatButton(text="Start New Game", width=150)
72 volume = arcade.gui.UIFlatButton(text="Volume", width=150)
73 options = arcade.gui.UIFlatButton(text="Options", width=150)
74
75 exit = arcade.gui.UIFlatButton(text="Exit", width=320)
76
77 # Initialise a grid in which widgets can be arranged.
78 self.grid = arcade.gui.UIGridLayout(
79 column_count=2, row_count=3, horizontal_spacing=20, vertical_spacing=20
80 )
81
82 # Adding the buttons to the layout.
83 self.grid.add(resume, column=0, row=0)
84 self.grid.add(start_new_game, column=1, row=0)
85 self.grid.add(volume, column=0, row=1)
86 self.grid.add(options, column=1, row=1)
87 self.grid.add(exit, column=0, row=2, column_span=2)
88
89 self.anchor = self.manager.add(arcade.gui.UIAnchorLayout())
90
91 self.anchor.add(
92 anchor_x="center_x",
93 anchor_y="center_y",
94 child=self.grid,
95 )
96
97 self.main_view = main_view
98
99 def on_hide_view(self):
100 # Disable the UIManager when the view is hidden.
101 self.manager.disable()
102
103 def on_show_view(self):
104 """This is run once when we switch to this view"""
105
106 # Makes the background darker
107 arcade.set_background_color([rgb - 50 for rgb in arcade.color.DARK_BLUE_GRAY])
108
109 # Enable the UIManager when the view is showm.
110 self.manager.enable()
111
112 def on_draw(self):
113 """Render the screen."""
114 # Clear the screen
115 self.clear()
116 self.manager.draw()
117
118
119def main():
120 window = arcade.Window(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE, resizable=True)
121 main_view = MainView()
122 window.show_view(main_view)
123 arcade.run()
124
125
126if __name__ == "__main__":
127 main()