forked from quasar098/midi-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
135 lines (121 loc) · 5.1 KB
/
main.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
from utils import *
from menu import Menu
from game import Game
from configpage import ConfigPage
from songselector import SongSelector
from os import startfile, getcwd
from config import save_to_file
import webbrowser
import pygame
def main():
# pygame and other boilerplate
pygame.init()
pygame.mixer.music.load("./assets/mainmenu.mp3")
pygame.mixer.music.set_volume(Config.volume/100)
pygame.mixer.music.play(loops=-1, start=2)
clock = pygame.time.Clock()
screen = pygame.display.set_mode(
[Config.SCREEN_WIDTH, Config.SCREEN_HEIGHT],
pygame.FULLSCREEN | pygame.HWACCEL | pygame.HWSURFACE | pygame.SCALED,
vsync=1
)
# the big guns
menu = Menu()
song_selector = SongSelector()
config_page = ConfigPage()
game = Game()
# game loop
running = True
while running:
# thanks to TheCodingCrafter for the implementation
if Config.theme == "rainbow":
to_set_as_rainbow = pygame.Color((0, 0, 0))
to_set_as_rainbow2 = pygame.Color((0, 0, 0))
to_set_as_rainbow.hsva = (((pygame.time.get_ticks()/1000)*Config.rainbow_speed) % 360, 100, 75, 100)
to_set_as_rainbow2.hsva = ((((pygame.time.get_ticks()/1000)*Config.rainbow_speed)+180) % 360, 100, 75, 100)
get_colors()["background"] = to_set_as_rainbow
get_colors()["hallway"] = to_set_as_rainbow2
get_colors()["square"][0] = to_set_as_rainbow
screen.fill(get_colors()["background"])
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
if song_selector.active:
song_selector.active = False
menu.active = True
if song_selector.selected_index+1:
pygame.mixer.music.load("./assets/mainmenu.mp3")
pygame.mixer.music.set_volume(Config.volume/100)
pygame.mixer.music.play(loops=-1, start=2)
song_selector.selected_index = -1
continue
if game.active:
game.active = False
song_selector.active = True
pygame.mixer.music.load("./assets/mainmenu.mp3")
pygame.mixer.music.set_volume(Config.volume/100)
pygame.mixer.music.play(loops=-1, start=2)
song_selector.selected_index = -1
continue
if config_page.active:
config_page.active = False
menu.active = True
continue
running = False
# handle menu events
option_id = menu.handle_event(event)
if option_id:
if option_id == "open-songs-folder":
startfile(join(getcwd(), "songs"))
continue
if option_id == "contribute":
webbrowser.open("https://github.com/quasar098/midi-playground")
continue
menu.active = False
if option_id == "config":
config_page.active = True
if option_id == "play":
song_selector.active = True
if option_id == "quit":
running = False
continue
# handle song selector events
song = song_selector.handle_event(event)
if song:
if isinstance(song, bool):
menu.active = True
if song_selector.selected_index+1:
pygame.mixer.music.load("./assets/mainmenu.mp3")
pygame.mixer.music.set_volume(Config.volume/100)
pygame.mixer.music.play(loops=-1, start=2)
song_selector.selected_index = -1
continue
# starting song now
Config.midi_file_name = song.midi_file_name
Config.audio_file_name = song.audio_file_name
game.active = True
if game.start_song(screen):
game.active = False
song_selector.active = True
pygame.mixer.music.load("./assets/mainmenu.mp3")
pygame.mixer.music.set_volume(Config.volume/100)
pygame.mixer.music.play(loops=-1, start=2)
# handle config page events
if config_page.handle_event(event):
config_page.active = False
menu.active = True
# handle game events
if game.handle_event(event):
game.active = False
song_selector.active = True
# draw stuff here
game.draw(screen)
song_selector.draw(screen)
config_page.draw(screen)
menu.draw(screen)
pygame.display.flip()
clock.tick(FRAMERATE)
pygame.quit()
save_to_file()
if __name__ == '__main__':
main()