-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMenu.py
209 lines (184 loc) · 6.08 KB
/
Menu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
"""
Menu
"""
import os
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.graphics import Rectangle
from kivy.core.window import Window
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.relativelayout import RelativeLayout
from kivy.logger import Logger
from LevelService import LevelService
from Configurations import COLORS
from EventDispatchers import propagate_event
from PopUpProvider import open_pop_up
class Menu(RelativeLayout):
"""
Main menu class.
"""
FONT_MENU_PATH = './resources/menu/vanadine_bold.ttf'
FOND_MENU_PATH = './resources/menu/background_menu.png'
LOGO_PATH = './resources/other/logo.png'
def __init__(self, event_dispatcher, music, **kwargs):
"""
Initialize menu's button and textures.
:param event_dispatcher: dispatcher
:param kwargs: Args of Layout
:rtype: void
"""
super(Menu, self).__init__(**kwargs)
self.event_dispatcher = event_dispatcher
self.music = music
# Add fond and title.
self.canvas.add(
Rectangle(source=self.FOND_MENU_PATH, size=Window.size)
)
self.canvas.add(
Rectangle(
source=self.LOGO_PATH,
size_hint=0.2,
pos_hint={'x': 0.4, 'y': 0.4}
)
)
self.add_widget(
Label(
text="'Scape me",
font_name=self.FONT_MENU_PATH,
font_blended=False,
font_size='87sp',
size_hint=(0.25, 0.1),
pos_hint={'center_x': 0.5, 'center_y': 0.8}
)
)
# Add buttons.
self.add_widget(
Button(
text="Play !",
font_name=self.FONT_MENU_PATH,
font_size='80sp',
background_color=(0, 0, 0, 0),
size_hint=(.45, .15),
pos_hint={'center_x': 0.5, 'center_y': 0.5},
on_press=self.switch_to_menu_level_screen
)
)
self.add_widget(
Button(
text="Credits",
font_name=self.FONT_MENU_PATH,
font_size='40sp',
background_color=(0, 0, 0, 0),
pos_hint={'center_x': 0.5, 'center_y': 0.2},
size_hint=(.2, .2),
on_press=self.credit_button_callback
)
)
self.add_widget(
Button(
text="Music",
font_name=self.FONT_MENU_PATH,
font_size='25sp',
background_color=(0, 0, 0, 0),
size_hint=(0.15, 0.12),
pos_hint={'center_x': 0.90, 'center_y': 0.06},
on_press=self.music_callback
)
)
def switch_to_menu_level_screen(self, *_):
"""
Propagation method.
:param _:
:return:
"""
Logger.info("Propagate MenuLevel")
propagate_event('MenuLevel', self)
def credit_button_callback(self, _):
"""
When player click on credit button (Required method).
:param _:
:rtype: void
"""
open_pop_up(self, 'Credits')
def music_callback(self, _):
"""
Start/Stop music.
:param _:
:rtype: void
"""
self.music.update_sound_state()
class MenuLevel(FloatLayout):
"""
Level Menu class.
"""
FONT_MENU_PATH = './resources/menu/vanadine_bold.ttf'
FOND_MENU_PATH = './resources/menu/background_menu.png'
MAPS_PATH = './resources/maps/'
color_1 = COLORS['blue_color']
color_2 = COLORS['dark_blue_color']
def __init__(self, event_dispatcher, **kwargs):
"""
Initialize button and textures of MenuLevel.
:param event_dispatcher: dispatcher
:param kwargs: args of layout
:rtype: void
"""
super(MenuLevel, self).__init__(**kwargs)
self.event_dispatcher = event_dispatcher
self.level_service = LevelService()
# Add fond.
self.canvas.add(
Rectangle(source=self.FOND_MENU_PATH, size=Window.size)
)
# Add button
self.add_widget(
Button(text="Menu", font_name=self.FONT_MENU_PATH, background_color=self.color_2,
pos_hint={'x': 0.82, 'y': 0}, size_hint=(0.18, 0.15),
on_press=self.switch_to_menu_screen)
)
menu_level_grid = GridLayout(size_hint=(0.7, 0.45), pos_hint={'x': 0.15, 'y': 0.3}, row=2)
self.add_widget(menu_level_grid)
set_list = os.listdir(self.MAPS_PATH)
set_number = len(set_list)
menu_level_grid.cols = set_number / 2
for index in range(1, set_number + 1):
if index % 2 == 0:
button_title = "Level " + str(index)
menu_level_grid.add_widget(
Button(
text=button_title,
font_name=self.FONT_MENU_PATH,
background_color=self.color_1,
on_press=self.launch_level,
cls=[index]
)
)
else:
button_title = "Level " + str(index)
menu_level_grid.add_widget(
Button(
text=button_title,
font_name=self.FONT_MENU_PATH,
background_color=self.color_2,
on_press=self.launch_level,
cls=[index]
)
)
def switch_to_menu_screen(self, *_):
"""
Required method.
"""
Logger.info("Propagate Menu")
propagate_event('Menu', self)
def launch_level(self, value):
"""
Load level.
:param value:
:rtype: void
"""
set_id = value.cls[0]
if self.level_service.can_load_set(set_id):
propagate_event('LevelManager', self, set_id)
else:
open_pop_up(self, 'not_unlocked', set_id)