forked from 1egoman/empty-fork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_menu.py
executable file
·423 lines (272 loc) · 11 KB
/
main_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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import pygame
from pygame.locals import *
import os, sys
import shutil
# MAIN MENU
def go(s, a=True):
logo_h = 200
# load images
logo = pygame.image.load( os.path.join("src", "menu", "logo.png") ).convert_alpha()
play_button = pygame.image.load( os.path.join("src", "menu", "playbutton.png") ).convert_alpha()
play_button_down = pygame.image.load( os.path.join("src", "menu", "playbuttondown.png") ).convert_alpha()
quit_button = pygame.image.load( os.path.join("src", "menu", "quitbutton.png") ).convert_alpha()
quit_button_down = pygame.image.load( os.path.join("src", "menu", "quitbuttondown.png") ).convert_alpha()
play_down = quit_down = False
loop = 1
f = 1000
while loop:
f += 1
cx = (s.get_width() - play_button.get_width())/2
# events
for event in pygame.event.get():
if event.type == QUIT:
# quit program
return s, None
elif event.type == VIDEORESIZE:
# resize screen
s = pygame.display.set_mode(event.size, RESIZABLE)
elif event.type == MOUSEBUTTONDOWN:
mx, my = event.pos
# play button
if mx >= cx and mx <= cx+play_button.get_width() and my >= logo_h+64 and my <= logo_h+play_button.get_height()+64:
play_down = True
# return s, "world"
else:
play_down = False
if f > 50 and mx >= cx and mx <= cx+play_button.get_width() and my >= logo_h+80 and my <= logo_h+play_button.get_height()+80+quit_button.get_height():
quit_down = True
else:
quit_down = False
elif event.type == MOUSEBUTTONUP:
mx, my = event.pos
# play button
if mx >= cx and mx <= cx+play_button.get_width() and my >= logo_h+64 and my <= logo_h+play_button.get_height()+64:
aa = load_new_world(s)
if aa: return aa
f = 0
# quit button
if f > 50 and mx >= cx and mx <= cx+play_button.get_width() and my >= logo_h+80 and my <= logo_h+play_button.get_height()+80+quit_button.get_height():
s.blit(quit_button, (cx, logo_h+play_button.get_height()+80 ))
pygame.display.flip()
return s, None
play_down = quit_down = False
# fill screen
s.fill((180,180,180))
if s.get_height() > 750: pygame.draw.rect(s, (120, 120, 120), (0, s.get_height()-100, s.get_width(), s.get_height()-100))
# draw buttons and logo
s.blit(logo, (cx, 16 ))
if play_down:
s.blit(play_button_down, (cx, logo_h+64))
else:
s.blit(play_button, (cx, logo_h+64))
if quit_down:
s.blit(quit_button_down, (cx, logo_h+play_button.get_height()+80 ))
else:
s.blit(quit_button, (cx, logo_h+play_button.get_height()+80 ))
# flip
pygame.display.flip()
# WORLD SELECTION
def load_new_world(s):
new_button = pygame.image.load( os.path.join("src", "menu", "newbutton.png") ).convert_alpha()
del_button = pygame.image.load( os.path.join("src", "menu", "delbutton.png") ).convert_alpha()
back_button = pygame.image.load( os.path.join("src", "menu", "backbutton.png") ).convert_alpha()
go_button = pygame.image.load( os.path.join("src", "menu", "gobutton.png") ).convert_alpha()
new_button_down = pygame.image.load( os.path.join("src", "menu", "newbuttondown.png") ).convert_alpha()
del_button_down = pygame.image.load( os.path.join("src", "menu", "delbuttondown.png") ).convert_alpha()
back_button_down = pygame.image.load( os.path.join("src", "menu", "backbuttondown.png") ).convert_alpha()
go_button_down = pygame.image.load( os.path.join("src", "menu", "gobuttondown.png") ).convert_alpha()
slider = pygame.image.load( os.path.join("src", "menu", "slider.png") ).convert_alpha()
font = pygame.font.SysFont(pygame.font.get_default_font(), 32)
iH = 96 # each list item's height
selected = None
sP = 0.0
scroll = False
new_down = del_down = back_down = go_down = False
loop = 1
while loop:
W = 512
H = 256
cx = (s.get_width() - W)/2
if s.get_height() > 512+96:
H = 512
else:
H = 256
# events
for event in pygame.event.get():
if event.type == QUIT:
# quit program
return s, None
elif event.type == VIDEORESIZE:
# resize screen
s = pygame.display.set_mode(event.size, RESIZABLE)
elif event.type == MOUSEMOTION:
mx, my = event.pos
m = pygame.mouse.get_pressed()
# selectbox
my -= 16
if scroll and m[0] and my <= H+16 and mx > cx+W+8 and mx < cx+W+40:
# adjust scrollbar
sP = my*1.0/H
if sP < 0: sP = 0
if sP > 1: sP = 1.0
elif event.type == MOUSEBUTTONDOWN:
mx, my = event.pos
if scroll and event.button == 5: sP += 0.05
if scroll and event.button == 4: sP -= 0.05
if sP < 0: sP = 0
if sP > 1: sP = 1.0
# selectbox
if event.button == 1 and my <= H+16:
my -= 16
if scroll and mx > cx+W+8 and mx < cx+W+40:
# adjust scrollbar
sP = my-16*1.0/H
if sP < 0: sP = 0
if sP > 1: sP = 1.0
else:
my += mD
selected = my/iH
# new
if mx > cx and mx < cx+new_button.get_width() and my > 32+H and my < 32+H+new_button.get_height():
new_down = True
else:
new_down = False
# delete
if selected != None and mx > W-new_button.get_width()+cx and mx < W+cx and my > 32+H and my < 32+H+new_button.get_height():
del_down = True
else:
del_down = False
# go (cx+new_button.get_width()+10, 32+H )
if selected != None and mx > cx+new_button.get_width()+10 and mx < cx+new_button.get_width()+10+go_button.get_width() and my > 32+H and my < 32+H+new_button.get_height():
go_down = True
else:
go_down = False
# back
if mx > cx and mx < cx+new_button.get_width() and my > 42+new_button.get_height()+H and my < 42+new_button.get_height()+H+back_button.get_height():
back_down = True
else:
back_down = False
elif event.type == MOUSEBUTTONUP:
mx, my = event.pos
# add
if mx > cx and mx < cx+new_button.get_width() and my > 32+H and my < 32+H+new_button.get_height():
return new_world(s)
# delete
if selected != None and mx > W-new_button.get_width()+cx and mx < W+cx and my > 32+H and my < 32+H+new_button.get_height():
# delete world
shutil.rmtree( os.path.join("saves", worlds[selected]) )
# back
if mx > cx and mx < cx+new_button.get_width() and my > 42+new_button.get_height()+H and my < 42+new_button.get_height()+H+back_button.get_height():
return None
if selected != None and mx > cx+new_button.get_width()+10 and mx < cx+new_button.get_width()+10+go_button.get_width() and my > 32+H and my < 32+H+new_button.get_height():
# launch selected World
return s, worlds[selected]
# deselect buttons
new_down = False
del_down = False
back_down = False
go_down = False
if my > H+16 and mx < cx or mx > cx+H: selected = None
# fill screen
s.fill((180,180,180))
if s.get_height() > 750: pygame.draw.rect(s, (120, 120, 120), (0, s.get_height()-100, s.get_width(), s.get_height()-100))
# draw selectbox
worlds = [t for t in reversed( os.listdir("saves") )]
pygame.draw.rect(s, (100, 100, 100), (0, 16, s.get_width(), H))
pygame.draw.rect(s, (0, 0, 0), (-10, 16, s.get_width()+20, H), 3)
if scroll: pygame.draw.rect(s, (140, 140, 140), (cx+W+8, 18, 32, H-4))
tH = iH*(len(worlds))
if scroll: s.blit(slider, ( cx+W+12, 18+((H-46)*sP)+4 ))
if sP < 0: sP = 0.0
if sP > 1: sP = 1.0
mD = int( (tH-H+iH)*sP )
if selected > len(worlds)-1 or selected < 0:
selected = None
# draw selectbox contents
aa = False
for c,e in enumerate(worlds):
# stop when list reaches capacity
if iH*(c+1)+20-mD > H+16:
scroll = True
aa = True
break
# draw selection
if selected == c:
d = iH
pygame.draw.rect(s, (120, 120, 120), (cx, c*iH+20-mD, W, d))
# render item
rndr = font.render(e, 1, (255,255,255))
s.blit(rndr, (cx+20, c*iH+32-mD))
# update scroll
if not aa and mD < 0: scroll = False
# draw buttons
if new_down:
s.blit(new_button_down, (cx, 32+H ))
else:
s.blit(new_button, (cx, 32+H ))
if back_down:
s.blit(back_button_down, (cx , 42+new_button.get_height()+H))
else:
s.blit(back_button, (cx , 42+new_button.get_height()+H))
if selected != None:
if del_down:
s.blit(del_button_down, (W-new_button.get_width()+cx , 32+H))
else:
s.blit(del_button, (W-new_button.get_width()+cx , 32+H))
if go_down:
s.blit(go_button_down, (cx+new_button.get_width()+10, 32+H ))
else:
s.blit(go_button, (cx+new_button.get_width()+10, 32+H ))
# flip
pygame.display.flip()
def new_world(s):
go_button = pygame.image.load( os.path.join("src", "menu", "gobutton.png") ).convert_alpha()
go_button_down = pygame.image.load( os.path.join("src", "menu", "gobuttondown.png") ).convert_alpha()
go_down = False
font = pygame.font.SysFont(pygame.font.get_default_font(), 48)
name = ""
while 1:
cx = (s.get_width() - go_button.get_width())/2
# events
for event in pygame.event.get():
if event.type == QUIT:
# quit program
return s, None
elif event.type == VIDEORESIZE:
# resize screen
s = pygame.display.set_mode(event.size, RESIZABLE)
elif event.type == KEYDOWN:
if event.key == K_BACKSPACE:
name = name[:-1]
else:
name += event.unicode
elif event.type == MOUSEBUTTONDOWN:
mx, my = event.pos
# go button
if name and mx >= cx and mx <= cx+go_button.get_width() and my >= 200 and my <= go_button.get_height()+200:
go_down = True
else:
go_down = False
elif event.type == MOUSEBUTTONUP:
mx, my = event.pos
# go button
if name and mx >= cx and mx <= cx+go_button.get_width() and my >= 200 and my <= go_button.get_height()+200:
# make world dir
p = os.path.join("saves", name)
if not os.path.exists(p): os.mkdir(p)
return s, name
go_down = False
# fill screen
s.fill((180,180,180))
if s.get_height() > 750: pygame.draw.rect(s, (120, 120, 120), (0, s.get_height()-100, s.get_width(), s.get_height()-100))
rndr = font.render("World Name: ", 1, (255,255,255))
s.blit(rndr, (cx, 48))
pygame.draw.rect(s, (120,120,120), (cx, 96, 512, 64))
rndr = font.render(name, 1, (255,255,255))
s.blit(rndr, (cx+10, 110))
if name and go_down:
s.blit(go_button_down, (cx, 200))
elif name:
s.blit(go_button, (cx, 200))
# flip
pygame.display.flip()