summaryrefslogtreecommitdiff
path: root/src/menu.py
blob: 49e263f659c08ab1fe8c9bd2d54ac947b569ab9a (plain)
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
import os.path

import pygame
import helper
import audio
import save
import constants
import debug

clock = pygame.time.Clock()

def show(screen):
    running = True
    focused = True
    opacity = 0
    audio.play_menu(True)

    while running:
        pygame.event.set_allowed([pygame.APPMOUSEFOCUS, pygame.APPINPUTFOCUS, pygame.WINDOWRESIZED, pygame.WINDOWEXPOSED, pygame.WINDOWENTER, pygame.WINDOWLEAVE, pygame.WINDOWFOCUSLOST, pygame.WINDOWFOCUSGAINED, pygame.WINDOWICCPROFCHANGED, pygame.WINDOWTAKEFOCUS, pygame.WINDOWMINIMIZED, pygame.MOUSEBUTTONDOWN, pygame.MOUSEMOTION, pygame.QUIT])
        for event in pygame.event.get():
            if event.type == pygame.WINDOWFOCUSLOST:
                focused = False
            if event.type == pygame.WINDOWFOCUSGAINED:
                focused = True
            if event.type == pygame.MOUSEMOTION:
                pygame.mouse.set_visible(True)
            if event.type == pygame.MOUSEBUTTONDOWN:
                left, middle, right = pygame.mouse.get_pressed()
                mouse = helper.get_real_mouse(screen)
                print(mouse)

                if left:
                    if 45 < mouse[0] < 195 and 40 < mouse[1] < 70:
                        audio.play_sfx("action")
                        print("New game")
                        screen.fill("black")
                        canvas = pygame.Surface((1280, 720))
                        canvas.fill("black")

                        width = screen.get_size()[0]
                        height = width / (16/9)

                        if width > screen.get_size()[0] or height > screen.get_size()[1]:
                            height = screen.get_size()[1]
                            width = height * (16/9)

                        run = True
                        done = False
                        text = ""
                        quit = False

                        while run and not done:
                            canvas.fill("black")

                            pygame.event.set_allowed([pygame.APPMOUSEFOCUS, pygame.APPINPUTFOCUS, pygame.WINDOWRESIZED, pygame.WINDOWEXPOSED, pygame.WINDOWENTER, pygame.WINDOWLEAVE, pygame.WINDOWFOCUSLOST, pygame.WINDOWFOCUSGAINED, pygame.WINDOWICCPROFCHANGED, pygame.WINDOWTAKEFOCUS, pygame.WINDOWMINIMIZED, pygame.QUIT, pygame.KEYDOWN])
                            for event in pygame.event.get():
                                if event.type == pygame.WINDOWFOCUSLOST:
                                    focused = False
                                if event.type == pygame.WINDOWFOCUSGAINED:
                                    focused = True
                                if event.type == pygame.QUIT:
                                    run = False
                                    quit = True
                                elif event.type == pygame.KEYDOWN:
                                    if event.key == pygame.K_ESCAPE:
                                        run = False
                                    elif event.key == pygame.K_RETURN:
                                        text = text.strip()

                                        for i in ["con", "prn", "aux", "nul",
                                                  "com1", "com2", "com3", "com4", "com5", "com6", "com7", "com8", "com9",
                                                  "lpt1", "lpt2", "lpt3", "lpt4", "lpt5", "lpt6", "lpt7", "lpt8", "lpt9"]:
                                            if text.lower() == i or text.startswith(i + ".") or text.endswith("." + i):
                                                text = ""

                                        if text.strip() != "" and not os.path.exists(helper.get_data_path() + "/saves/" + text):
                                            done = True
                                        elif os.path.exists(helper.get_data_path() + "/saves/" + text):
                                            text = ""
                                        else:
                                            text = text.strip()
                                    elif event.key == pygame.K_BACKSPACE:
                                        text = text[:-1]
                                    else:
                                        if event.unicode not in ["/", "\\", "<", ">", ":", "\"", "'", "|", "?", "*"] and len(text) < 60:
                                            text += event.unicode

                            text_surf = helper.text(text + "|", 20, (255, 255, 255))
                            canvas.blit(text_surf, (50, 75))
                            canvas.blit(helper.text("Enter world name:", 20, (255, 255, 255)), (50, 50))

                            scaled_win = pygame.transform.scale(canvas, (width, height))
                            screen.blit(scaled_win, (screen.get_size()[0] / 2 - width / 2, screen.get_size()[1] / 2 - height / 2))
                            if focused:
                                debug.show_debug(clock)
                                pygame.display.flip()

                            clock.tick(25)

                        if quit:
                            pygame.quit()
                        elif done:
                            audio.stop(1)
                            audio.play_sfx("menu")

                            canvas.fill("black")
                            canvas.blit(helper.text("Generating world...", 20, (255, 255, 255)), (50, 50))

                            scaled_win = pygame.transform.scale(canvas, (width, height))
                            screen.blit(scaled_win, (screen.get_size()[0] / 2 - width / 2, screen.get_size()[1] / 2 - height / 2))
                            debug.show_debug(clock)
                            pygame.display.flip()

                            audio.wait_for_sfx()

                            import game
                            game.run(screen, helper.get_data_path() + "/saves/" + text)

                            return
                        else:
                            audio.play_sfx("back")
                    elif 45 < mouse[0] < 205 and 40+25 < mouse[1] < 70+25:
                        audio.play_sfx("action")
                        print("Load game")
                        screen.fill("black")
                        canvas = pygame.Surface((1280, 720))
                        canvas.fill("black")

                        width = screen.get_size()[0]
                        height = width / (16/9)

                        if width > screen.get_size()[0] or height > screen.get_size()[1]:
                            height = screen.get_size()[1]
                            width = height * (16/9)

                        run = True
                        done = False
                        quit = False
                        world_name = ""

                        saves = list(filter(lambda x: not x.startswith(".") and os.path.exists(helper.get_data_path() + "/saves/" + x + "/level.dat"), os.listdir(helper.get_data_path() + "/saves")))

                        while run and not done:
                            canvas.fill("black")

                            pygame.event.set_allowed([pygame.APPMOUSEFOCUS, pygame.APPINPUTFOCUS, pygame.WINDOWRESIZED, pygame.WINDOWEXPOSED, pygame.WINDOWENTER, pygame.WINDOWLEAVE, pygame.WINDOWFOCUSLOST, pygame.WINDOWFOCUSGAINED, pygame.WINDOWICCPROFCHANGED, pygame.WINDOWTAKEFOCUS, pygame.WINDOWMINIMIZED, pygame.QUIT, pygame.MOUSEBUTTONDOWN, pygame.KEYDOWN])
                            for event in pygame.event.get():
                                if event.type == pygame.WINDOWFOCUSLOST:
                                    focused = False
                                if event.type == pygame.WINDOWFOCUSGAINED:
                                    focused = True
                                if event.type == pygame.QUIT:
                                    run = False
                                    quit = True
                                elif event.type == pygame.MOUSEBUTTONDOWN:
                                    cursor = helper.get_real_mouse(screen)

                                    for i in range(len(saves)):
                                        save_name = saves[i]

                                        if 45 < cursor[0] < 550 and 65+25*i < cursor[1] < 95+25*i:
                                            print(save_name)
                                            world_name = save_name
                                            done = True
                                            break

                                elif event.type == pygame.KEYDOWN:
                                    if event.key == pygame.K_ESCAPE:
                                        run = False

                            canvas.blit(helper.text("Select a world to load:", 20, (255, 255, 255)), (50, 50))

                            last_i = 0

                            for i in range(len(saves)):
                                save_name = saves[i]
                                canvas.blit(helper.text(save_name, 20, (255, 255, 255)), (75, 75 + (25 * i)))
                                last_i = i

                            canvas.blit(helper.text("Notice:", 20, (255, 255, 255)), (50, 75 + (25 * (last_i + 2))))
                            canvas.blit(helper.text("Worlds from Blocks 0.1a are not compatible and", 20, (255, 255, 255)), (75, 75 + (25 * (last_i + 3))))
                            canvas.blit(helper.text("do not show up here.", 20, (255, 255, 255)), (75, 75 + (25 * (last_i + 4))))
                            canvas.blit(helper.text("Worlds from Blocks 0.2a are not compatible and", 20, (255, 255, 255)), (75, 75 + (25 * (last_i + 5))))
                            canvas.blit(helper.text("will crash the game.", 20, (255, 255, 255)), (75, 75 + (25 * (last_i + 6))))

                            scaled_win = pygame.transform.scale(canvas, (width, height))
                            screen.blit(scaled_win, (screen.get_size()[0] / 2 - width / 2, screen.get_size()[1] / 2 - height / 2))
                            if focused:
                                debug.show_debug(clock)
                                pygame.display.flip()

                            clock.tick(25)
                        if quit:
                            pygame.quit()
                        elif done:
                            audio.stop(1)
                            audio.play_sfx("menu")

                            canvas.fill("black")
                            canvas.blit(helper.text("Loading world...", 20, (255, 255, 255)), (50, 50))

                            scaled_win = pygame.transform.scale(canvas, (width, height))
                            screen.blit(scaled_win, (screen.get_size()[0] / 2 - width / 2, screen.get_size()[1] / 2 - height / 2))
                            debug.show_debug(clock)
                            pygame.display.flip()

                            loaded = save.load_world(helper.get_data_path() + "/saves/" + world_name)

                            if loaded['version'] != constants.VERSION:
                                print("WARNING: World was last loaded in a different version.")

                            audio.wait_for_sfx()

                            import game
                            game.run(screen, helper.get_data_path() + "/saves/" + world_name)

                            return
                        else:
                            audio.play_sfx("back")
                    elif 45 < mouse[0] < 125 and 40+25*2 < mouse[1] < 70+25*2:
                    #    audio.play_sfx("action")
                    #    print("Settings")
                    #elif 45 < mouse[0] < 90 and 40+25*3 < mouse[1] < 70+25*3:
                        audio.play_sfx("action")
                        audio.wait_for_sfx()
                        print("Exit")
                        running = False

            if event.type == pygame.QUIT:
                running = False

        screen.fill("black")
        canvas = pygame.Surface((1280, 720))
        canvas.fill("red")

        width = screen.get_size()[0]
        height = width / (16/9)

        if width > screen.get_size()[0] or height > screen.get_size()[1]:
            height = screen.get_size()[1]
            width = height * (16/9)

        canvas.blit(helper.text("Start new game", 20, (255, 255, 255)), (50, 50))
        canvas.blit(helper.text("Load saved game", 20, (255, 255, 255)), (50, 75))
        #canvas.blit(helper.text("Settings", 20, (255, 255, 255)), (50, 100))
        #canvas.blit(helper.text("Exit", 20, (255, 255, 255)), (50, 125))
        canvas.blit(helper.text("Exit", 20, (255, 255, 255)), (50, 100))

        scaled_win = pygame.transform.scale(canvas, (width, height))

        if opacity >= 1:
            result = scaled_win
        else:
            result = helper.transparent(scaled_win, opacity)

        screen.blit(result, (screen.get_size()[0] / 2 - width / 2, screen.get_size()[1] / 2 - height / 2))

        audio.play_menu()
        if focused:
            debug.show_debug(clock)
            pygame.display.flip()

        if opacity < 1:
            opacity += 1/30

        if opacity >= 1:
            opacity = 1

        clock.tick(25)