MENU_04.py完整列表#
menu_04.py#
1"""
2Menu.
3
4Shows the usage of almost every gui widget, switching views and making a modal.
5"""
6import arcade
7import arcade.gui
8
9# Screen title and size
10SCREEN_WIDTH = 800
11SCREEN_HEIGHT = 600
12SCREEN_TITLE = "Making a Menu"
13
14
15class MainView(arcade.View):
16 """This is the class where your normal game would go."""
17
18 def __init__(self):
19 super().__init__()
20
21 self.manager = arcade.gui.UIManager()
22
23 switch_menu_button = arcade.gui.UIFlatButton(text="Pause", width=150)
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_hide_view(self):
42 # Disable the UIManager when the view is hidden.
43 self.manager.disable()
44
45 def on_show_view(self):
46 """ This is run once when we switch to this view """
47 arcade.set_background_color(arcade.color.DARK_BLUE_GRAY)
48
49 # Enable the UIManager when the view is showm.
50 self.manager.enable()
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 resume_button = arcade.gui.UIFlatButton(text="Resume", width=150)
70 start_new_game_button = arcade.gui.UIFlatButton(text="Start New Game", width=150)
71 volume_button = arcade.gui.UIFlatButton(text="Volume", width=150)
72 options_button = arcade.gui.UIFlatButton(text="Options", width=150)
73
74 exit_button = arcade.gui.UIFlatButton(text="Exit", width=320)
75
76 # Initialise a grid in which widgets can be arranged.
77 self.grid = arcade.gui.UIGridLayout(column_count=2, row_count=3, horizontal_spacing=20, vertical_spacing=20)
78
79 # Adding the buttons to the layout.
80 self.grid.add(resume_button, col_num=0, row_num=0)
81 self.grid.add(start_new_game_button, col_num=1, row_num=0)
82 self.grid.add(volume_button, col_num=0, row_num=1)
83 self.grid.add(options_button, col_num=1, row_num=1)
84 self.grid.add(exit_button, col_num=0, row_num=2, col_span=2)
85
86 self.anchor = self.manager.add(arcade.gui.UIAnchorLayout())
87
88 self.anchor.add(
89 anchor_x="center_x",
90 anchor_y="center_y",
91 child=self.grid,
92 )
93
94 self.main_view = main_view
95
96 @resume_button.event("on_click")
97 def on_click_resume_button(event):
98 # Pass already created view because we are resuming.
99 self.window.show_view(self.main_view)
100
101 @start_new_game_button.event("on_click")
102 def on_click_start_new_game_button(event):
103 # Create a new view because we are starting a new game.
104 main_view = MainView()
105 self.window.show_view(main_view)
106
107 @exit_button.event("on_click")
108 def on_click_exit_button(event):
109 arcade.exit()
110
111 @volume_button.event("on_click")
112 def on_click_volume_button(event):
113 volume_menu = SubMenu()
114 self.manager.add(
115 volume_menu,
116 layer=1
117 )
118
119 @options_button.event("on_click")
120 def on_click_options_button(event):
121 options_menu = SubMenu()
122 self.manager.add(
123 options_menu,
124 layer=1
125 )
126
127 def on_hide_view(self):
128 # Disable the UIManager when the view is hidden.
129 self.manager.disable()
130
131 def on_show_view(self):
132 """ This is run once when we switch to this view """
133
134 # Makes the background darker
135 arcade.set_background_color([rgb - 50 for rgb in arcade.color.DARK_BLUE_GRAY])
136
137 # Enable the UIManager when the view is showm.
138 self.manager.enable()
139
140 def on_draw(self):
141 """ Render the screen. """
142 # Clear the screen
143 self.clear()
144 self.manager.draw()
145
146
147class SubMenu(arcade.gui.UIMouseFilterMixin, arcade.gui.UIAnchorLayout):
148 """Acts like a fake view/window."""
149
150 def __init__(self, ):
151 super().__init__(size_hint=(1, 1))
152
153 # Setup frame which will act like the window.
154 frame = self.add(arcade.gui.UIAnchorLayout(width=300, height=400, size_hint=None))
155 frame.with_padding(all=20)
156
157 # Add a background to the window.
158 frame.with_background(texture=arcade.gui.NinePatchTexture(
159 left=7,
160 right=7,
161 bottom=7,
162 top=7,
163 texture=arcade.load_texture(
164 ":resources:gui_basic_assets/window/dark_blue_gray_panel.png"
165 )
166 ))
167
168 back_button = arcade.gui.UIFlatButton(text="Back", width=250)
169 # The type of event listener we used earlier for the button will not work here.
170 back_button.on_click = self.on_click_back_button
171
172 # Internal widget layout to handle widgets in this class.
173 widget_layout = arcade.gui.UIBoxLayout(align="left", space_between=10)
174
175 widget_layout.add(back_button)
176
177 frame.add(child=widget_layout, anchor_x="center_x", anchor_y="top")
178
179 def on_click_back_button(self, event):
180 # Removes the widget from the manager.
181 # After this the manager will respond to its events like it previously did.
182 self.parent.remove(self)
183
184
185def main():
186 window = arcade.Window(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE, resizable=True)
187 main_view = MainView()
188 window.show_view(main_view)
189 arcade.run()
190
191
192if __name__ == "__main__":
193 main()