-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
216 lines (179 loc) · 8.09 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
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
import pygame
import sys
from states.intro import Intro
from states.menu import Menu
from states.game import Game
from states.transition import Transition
from states.editor import Editor
from utils import music_manager as Music_Manager
import random
class Main(object):
def __init__(self):
pygame.init()
self.SCREEN_W = 960
self.SCREEN_H = 640
self.SCREEN_C = (self.SCREEN_W / 2, self.SCREEN_H / 2)
self.SCREEN = pygame.display.set_mode((self.SCREEN_W, self.SCREEN_H))
self.CLOCK = pygame.time.Clock()
self.DISPLAY = pygame.Surface((self.SCREEN_W, self.SCREEN_H), flags=pygame.SRCALPHA)
pygame.display.set_icon(pygame.image.load('assets/icons/icon.ico'))
pygame.display.set_caption('Followed')
self.music_manager = Music_Manager()
self.fonts = {
"general": pygame.font.Font("assets/fonts/oswald.ttf", 25)
}
self.sfx = {
"transtion": pygame.mixer.Sound("assets/sfx/transtion.wav"),
"select": pygame.mixer.Sound("assets/sfx/select.wav"),
}
self.screen_shake = {
"intensity": 0,
"duration": 0,
"active": False
}
self._events = {}
self._states = {
"intro": Intro(self),
"menu": Menu(self),
"game": Game(self),
"transition": Transition(self),
"level_editor": Editor(self)
}
self._state = "intro"
def events(self):
self._events.clear()
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.shutdown()
if event.type == pygame.MOUSEBUTTONDOWN:
self._events["mousebuttondown"] = event
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
self._events["keydown-left"] = True
elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
self._events["keydown-right"] = True
elif event.key == pygame.K_UP or event.key == pygame.K_w:
self._events["keydown-up"] = True
elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
self._events["keydown-down"] = True
elif event.key == pygame.K_r:
self._events["restart"] = True
elif event.key == pygame.K_l:
self._events["level"] = True
elif event.key == pygame.K_y:
self._events["save"] = True
elif event.key == pygame.K_1:
self._events["1"] = True
elif event.key == pygame.K_2:
self._events["2"] = True
elif event.key == pygame.K_3:
self._events["3"] = True
elif event.key == pygame.K_4:
self._events["4"] = True
elif event.key == pygame.K_5:
self._events["5"] = True
elif event.key == pygame.K_6:
self._events["6"] = True
elif event.key == pygame.K_7:
self._events["7"] = True
elif event.key == pygame.K_8:
self._events["8"] = True
elif event.key == pygame.K_9:
self._events["9"] = True
elif event.key == pygame.K_0:
self._events["0"] = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
self._events["keydown-left"] = False
elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
self._events["keydown-right"] = False
elif event.key == pygame.K_UP or event.key == pygame.K_w:
self._events["keydown-up"] = False
elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
self._events["keydown-down"] = False
elif event.key == pygame.K_r:
self._events["restart"] = False
elif event.key == pygame.K_l:
self._events["level"] = False
elif event.key == pygame.K_y:
self._events["save"] = False
elif event.key == pygame.K_1:
self._events["1"] = False
elif event.key == pygame.K_2:
self._events["2"] = False
elif event.key == pygame.K_3:
self._events["3"] = False
elif event.key == pygame.K_4:
self._events["4"] = False
elif event.key == pygame.K_5:
self._events["5"] = False
elif event.key == pygame.K_6:
self._events["6"] = False
elif event.key == pygame.K_7:
self._events["7"] = False
elif event.key == pygame.K_8:
self._events["8"] = False
elif event.key == pygame.K_9:
self._events["9"] = False
elif event.key == pygame.K_0:
self._events["0"] = False
def update(self):
self._states[self._state].update(self._events)
self._states["transition"].update()
if self.screen_shake["active"]:
if self.screen_shake["duration"] > 0:
self.screen_shake["duration"] -= 1
else:
self.screen_shake["active"] = False
self.music_manager.update( self._state )
def render(self):
self.DISPLAY.fill((0, 0, 0))
self._states[self._state].render(self.DISPLAY)
self._states["transition"].render(self.DISPLAY)
display_offset = [0, 0]
if self.screen_shake["active"]:
display_offset[0] += random.randint(-self.screen_shake["intensity"][0], self.screen_shake["intensity"][0])
display_offset[1] += random.randint(-self.screen_shake["intensity"][1], self.screen_shake["intensity"][1])
self.SCREEN.blit(self.DISPLAY, display_offset)
pygame.display.update()
def screenshake(self, intensity=[2, 2], duration=20):
self.screen_shake["intensity"] = intensity
self.screen_shake["duration"] = duration
self.screen_shake["active"] = True
def render_text(self, surface, text, x, y, font="general", render_centerx=True, render_centery=True):
for i, line in enumerate(text.split("\n")):
text_surface = self.fonts[font].render(line, True, (255, 255, 255))
text_surface_3D = self.fonts[font].render(line, True, (120, 120, 120))
match render_centerx, render_centery:
case True, True:
text_rect = text_surface.get_rect(center=(x, y + text_surface.get_height() * i))
case True, False:
text_rect = text_surface.get_rect(centerx=x, top=y + text_surface.get_height() * i)
case False, True:
text_rect = text_surface.get_rect(left=x, centery=y + text_surface.get_height() * i)
case False, False:
text_rect = text_surface.get_rect(topleft=(x, y + text_surface.get_height() * i))
text_rect_3D = text_rect.copy()
text_rect_3D.x += 3
text_rect_3D.y -= 3
surface.blit(text_surface_3D, text_rect_3D)
surface.blit(text_surface, text_rect)
def loop(self):
while True:
self.events()
self.update()
self.render()
self.CLOCK.tick(60)
def transition_to(self, state, setup=True, speed=2):
if not self._states["transition"].active:
if setup:
self._states[state].setup()
self._states["transition"].setup()
self._states["transition"].speed = speed
self._states["transition"].active = True
self._states["transition"].endstate = state
def shutdown(self):
pygame.quit()
sys.exit()
if __name__ == "__main__":
Main().loop()