-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSettingPage.py
198 lines (165 loc) · 10.3 KB
/
SettingPage.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
import sys
import pygame
from Button import Button, TextButton, Slider
from Colors import *
import pickle
import os
from resource_path import *
class SettingPage():
def __init__(self, screen, setting, pre_page = None):
self.screen = screen
self.screen_width, self.screen_height = pygame.display.get_surface().get_size()
self.setting = setting
self.key_idx = 0
self.player_names = ''
self.pre_page = pre_page
if os.path.exists(resource_path("./setting_state.pkl")):
self.size_idx = setting.size_idx
self.color_idx = setting.color_idx
self.keys_idx = setting.keys_idx
# print("size_idx : " + str(setting.size_idx))
else:
self.size_idx = 0
self.color_idx = 0
self.keys_idx = 0
self.screen_size_txt = TextButton(0.3, 0.2, 200, 50, 'screen size')
self.color_weak_txt = TextButton(0.3, 0.3, 200, 50, 'color weak mode')
self.key_txt = TextButton(0.3, 0.4, 200, 50, 'key')
self.volume_txt = TextButton(0.3, 0.5, 200, 50, 'vol.')
self.back_volume_txt = TextButton(0.3, 0.6, 200, 50, 'background vol.')
self.effect_volume_txt = TextButton(0.3, 0.7, 200, 50, 'effect vol.')
self.save_txt = TextButton(0.25, 0.85, 200, 50, 'SAVE')
self.reset_txt = TextButton(0.5, 0.85, 200, 50, 'RESET')
self.back_txt = TextButton(0.75, 0.85, 200, 50, 'BACK')
self.size_opt = {"idx": self.size_idx, "opt": ("800 x 600", "1000 x 750", "1200 x 900")}
self.size_opt_btn = Button(0.7, 0.2, 200, 50, str(self.size_opt["opt"][self.size_opt["idx"]]))
self.left_btn_0 = TextButton(0.55, 0.2, 30, 30, '<', background_color=LIGHT_GRAY)
self.right_btn_0 = TextButton(0.85, 0.2, 30, 30, '>', background_color=LIGHT_GRAY)
self.color_weak_opt = {"idx": self.color_idx, "opt": ("off", "on")}
self.color_weak_opt_btn = Button(0.7, 0.3, 200, 50, self.color_weak_opt["opt"][self.color_weak_opt["idx"]])
self.left_btn_1 = TextButton(0.55, 0.3, 30, 30, '<', background_color=LIGHT_GRAY)
self.right_btn_1 = TextButton(0.85, 0.3, 30, 30, '>', background_color=LIGHT_GRAY)
self.key_opt = {"idx":self.keys_idx, "opt": ("mouse", "WSAD", "arrow keys")}
self.key_opt_btn = Button(0.7, 0.4, 200, 50, self.key_opt["opt"][self.key_opt["idx"]])
self.left_btn_2 = TextButton(0.55, 0.4, 30, 30, '<', background_color=LIGHT_GRAY)
self.right_btn_2 = TextButton(0.85, 0.4, 30, 30, '>', background_color=LIGHT_GRAY)
self.volume_slider = Slider(0.7, 0.5, 200, 50, setting.volume)
self.back_volume_slider = Slider(0.7, 0.6, 200, 50, setting.back_volume)
self.effect_volume_slider = Slider(0.7, 0.7, 200, 50, setting.effect_volume)
self.opt_texts = [self.screen_size_txt, self.color_weak_txt, self.key_txt,
self.volume_txt, self.back_volume_txt, self.effect_volume_txt,
self.save_txt, self.reset_txt, self.back_txt]
# 3가지 모음
self.opt = [self.size_opt, self.color_weak_opt, self.key_opt]
self.opt_buttons = [self.size_opt_btn, self.color_weak_opt_btn, self.key_opt_btn]
self.left_buttons = [self.left_btn_0, self.left_btn_1, self.left_btn_2]
self.right_buttons = [self.right_btn_0, self.right_btn_1, self.right_btn_2]
self.volume_sliders = [self.volume_slider, self.back_volume_slider, self.effect_volume_slider]
def running(self):
self.screen.fill(WHITE)
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit(0)
elif event.type == pygame.MOUSEBUTTONDOWN:
for key_idx, left_button in enumerate(self.left_buttons):
self.key_idx = key_idx
if left_button.rect.collidepoint(event.pos):
self.opt[self.key_idx]["idx"] -= 1
self.opt[self.key_idx]["idx"] %= len(self.opt[self.key_idx]["opt"])
self.opt_buttons[self.key_idx].text = self.opt[self.key_idx]["opt"][self.opt[self.key_idx]["idx"]]
# print("left button click : " + str(self.opt[self.key_idx]["idx"]))
for key_idx, right_button in enumerate(self.right_buttons):
self.key_idx = key_idx
if right_button.rect.collidepoint(event.pos):
self.opt[self.key_idx]["idx"] += 1
self.opt[self.key_idx]["idx"] %= len(self.opt[self.key_idx]["opt"])
self.opt_buttons[self.key_idx].text = self.opt[self.key_idx]["opt"][self.opt[self.key_idx]["idx"]]
for slider in self.volume_sliders:
if event.pos[0] >= slider.top and event.pos[0] <= slider.top + slider.length and event.pos[1] >= slider.left - 10 and event.pos[1] <= slider.left + 10:
slider.value = int((event.pos[0] - slider.top) / slider.length * (slider.max_val - slider.min_val) + slider.min_val)
if self.save_txt.rect.collidepoint(event.pos):
width, height = self.size_opt_btn.text.split(' x ')
screen_size = (int(width), int(height))
color_weak = True if self.color_weak_opt_btn.text == "on" else False
key = self.key_opt_btn.text
volumn = self.volume_slider.value
back_volumn = self.back_volume_slider.value
effect_volumn = self.effect_volume_slider.value
self.setting.set(screen_size, color_weak, key, volumn, back_volumn, effect_volumn, self.opt[0]["idx"], self.opt[1]["idx"], self.opt[2]["idx"])
setting2 = self.setting
# pickle에 현재 데이터 저장
with open(resource_path("./setting_state.pkl"), "wb") as f:
pickle.dump(setting2, f)
if self.pre_page is not None:
if os.path.exists(resource_path('game_state.pkl')):
with open(resource_path('game_state.pkl'), 'rb') as f:
game_state = pickle.load(f)
self.player_names = game_state.player_names
return "game", self.player_names
else:
return "main"
elif self.reset_txt.rect.collidepoint(event.pos):
self.size_opt_btn.text = "800 x 600"
self.color_weak_opt_btn.text = "off"
self.key_opt_btn.text = "mouse"
self.volume_slider.value = 50
self.back_volume_slider.value = 50
self.effect_volume_slider.value = 50
self.setting.reset()
setting2 = self.setting
with open(resource_path("./setting_state.pkl"), "wb") as f:
pickle.dump(setting2, f)
return "main"
elif self.back_txt.rect.collidepoint(event.pos):
return "main"
elif event.type == pygame.KEYDOWN:
buttons_with_keyboard = self.opt_buttons + self.opt_texts[4:]
if event.key == pygame.K_DOWN:
self.key_idx += 1
self.key_idx %= len(buttons_with_keyboard)
buttons_with_keyboard[(self.key_idx - 1) % len(buttons_with_keyboard)].key_hovered = False
buttons_with_keyboard[self.key_idx].key_hovered = True
elif event.key == pygame.K_UP:
self.key_idx -= 1
self.key_idx %= len(buttons_with_keyboard)
buttons_with_keyboard[(self.key_idx + 1) % len(buttons_with_keyboard)].key_hovered = False
buttons_with_keyboard[self.key_idx].key_hovered = True
elif event.key == pygame.K_LEFT and self.key_idx < len(self.opt_buttons):
self.opt[self.key_idx]["idx"] -= 1
self.opt[self.key_idx]["idx"] %= len(self.opt[self.key_idx]["opt"])
self.opt_buttons[self.key_idx].text = self.opt[self.key_idx]["opt"][self.opt[self.key_idx]["idx"]]
elif event.key == pygame.K_RIGHT and self.key_idx < len(self.opt_buttons):
self.opt[self.key_idx]["idx"] += 1
self.opt[self.key_idx]["idx"] %= len(self.opt[self.key_idx]["opt"])
self.opt_buttons[self.key_idx].text = self.opt[self.key_idx]["opt"][self.opt[self.key_idx]["idx"]]
elif event.key == pygame.K_RETURN:
if self.key_idx == 4:
width, height = self.size_opt_btn.text.split(' x ')
screen_size = (int(width), int(height))
color_weak = True if self.color_weak_opt_btn.text == "on" else False
key = self.key_opt_btn.text
self.setting.set(screen_size, color_weak, key)
setting2 = self.setting
# pickle에 현재 데이터 저장
with open(resource_path("./setting_state.pkl"), "wb") as f:
pickle.dump(setting2, f)
if self.pre_page is not None:
if os.path.exists(resource_path('game_state.pkl')):
with open(resource_path('game_state.pkl'), 'rb') as f:
game_state = pickle.load(f)
self.player_names = game_state.player_names
return "game", self.player_names
else:
return "main"
elif self.key_idx == 5:
self.setting.reset()
return "main"
if self.key_idx == len(buttons_with_keyboard) - 1:
return "main"
for button in (self.opt_texts + self.opt_buttons + self.left_buttons + self.right_buttons):
button.process(self.screen)
for slider in self.volume_sliders:
slider.process_slider(self.screen)
pygame.display.flip()
fpsClock.tick(fps)
return "setting"