-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
450 lines (359 loc) · 13.3 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
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#########################
# #
# Python Night Funkin #
# #
#########################
# Imports
from json import load, dump
from os import chdir
from pygame import QUIT, MOUSEBUTTONDOWN, init
from pygame.display import set_mode, set_caption, flip, get_surface
from pygame.draw import rect as drawrect
from pygame.event import get
from pygame.font import SysFont
from pygame.image import load as imageload
from pygame.time import Clock
from pygame.transform import scale
init() # Init pygame
chdir('Assets') # Change dir to Assets
font = SysFont('Arial Black', 64) # Init font
BUTTON = (230, 230, 230) # Init button color
BLACK = (0, 0, 0) # Init black color
class Object: # Class object for another classes
def __init__(self):
self.rect = None
self.pos = None
def setpos(self, **kwargs):
ws, hs = get_surface().get_size()
pos = kwargs.get('pos')
(x, y), (w, h) = pos, (ws / self.rect[2], hs / self.rect[3])
self.pos = (int(ws * x), int(hs * y))
x, y = self.pos
self.rect = (x, y, w, h)
def getrect(self):
ws, hs = get_surface().get_size()
x, y, w, h = self.rect
rect = (x / ws, y / hs, w / ws, h / hs)
return rect
def getpos(self):
ws, hs = get_surface().get_size()
x, y, w, h = self.rect
pos = (x / ws, y / hs)
return pos
class Rect(Object): # Class for rect objects
def __init__(self, **kwargs):
ws, hs = get_surface().get_size()
self.origin = kwargs.get('origin') or (1, 1)
rect = kwargs.get('rect') or (0.1, 0.1, 0.1, 0.1)
x, y, w, h = rect
self.rect = (
int(ws * (x - w / 2 + w / 2 * self.origin[0])), int(hs * (y - w / 2 + h / 2 * self.origin[1])), int(ws * w),
int(hs * h))
self.color = kwargs.get('color') or (0, 0, 0)
self.onclick = kwargs.get('onclick')
self.loop = kwargs.get('loop')
def rerender(self, **kwargs):
ws, hs = get_surface().get_size()
self.origin = kwargs.get('origin') or self.origin
rect = kwargs.get('rect')
if rect:
x, y, w, h = rect
self.rect = (
int(ws * (x - w / 2 + w / 2 * self.origin[0])), int(hs * (y - w / 2 + h / 2 * self.origin[1])),
int(ws * w),
int(hs * h))
self.color = kwargs.get('color') or self.color
self.onclick = kwargs.get('onclick') or self.onclick
self.loop = kwargs.get('loop') or self.loop
def setrect(self, **kwargs):
ws, hs = get_surface().get_size()
pos = kwargs.get('pos')
x, y, w, h = self.rect
x, y = pos
self.origin = kwargs.get('origin') or self.origin
self.rect = int(ws * (x - w / 2 + w / 2 * self.origin[0])), int(hs * (y - w / 2 + h / 2 * self.origin[1])), w, h
def getpos(self):
ws, hs = get_surface().get_size()
x, y, w, h = self.rect
pos = (x / ws, y / hs)
return pos
class Text(Object): # Class for text objects
def __init__(self, **kwargs):
global font
self.text = kwargs.get('text') or 'None'
self.color = kwargs.get('color') or (0, 0, 0)
ws, hs = get_surface().get_size()
self.origin = kwargs.get('origin') or (1, 1)
rect = kwargs.get('rect') or (0.1, 0.1, 0.1, 0.1)
x, y, w, h = rect
self.rect = (
int(ws * (x - w / 2 + w / 2 * self.origin[0])), int(hs * (y - w / 2 + h / 2 * self.origin[1])), int(ws * w),
int(hs * h))
self.pos, self.size = (self.rect[0], self.rect[1]), (self.rect[2], self.rect[3])
self.onclick = kwargs.get('onclick')
self.loop = kwargs.get('loop')
self.surface = scale(font.render(self.text, True, self.color), self.size)
def rerender(self, **kwargs):
global font
self.text = kwargs.get('text') or self.text
self.color = kwargs.get('color') or self.color
ws, hs = get_surface().get_size()
self.origin = kwargs.get('origin') or self.origin
rect = kwargs.get('rect')
if rect:
x, y, w, h = rect
self.rect = (
int(ws * (x - w / 2 + w / 2 * self.origin[0])), int(hs * (y - w / 2 + h / 2 * self.origin[1])),
int(ws * w),
int(hs * h))
self.pos, self.size = (self.rect[0], self.rect[1]), (self.rect[2], self.rect[3])
self.onclick = kwargs.get('onclick') or self.onclick
self.loop = kwargs.get('loop') or self.loop
self.surface = scale(font.render(self.text, True, self.color), self.size)
class Image(Object): # Class for image objects
def __init__(self, **kwargs):
self.image = kwargs.get('image')
rect = kwargs.get('rect') or (0.1, 0.1, 0.1, 0.1)
ws, hs = get_surface().get_size()
self.origin = kwargs.get('origin') or (1, 1)
x, y, w, h = rect
self.rect = (
int(ws * (x - w / 2 + w / 2 * self.origin[0])), int(hs * (y - w / 2 + h / 2 * self.origin[1])), int(ws * w),
int(hs * h))
self.pos, self.size = (self.rect[0], self.rect[1]), (self.rect[2], self.rect[3])
self.onclick = kwargs.get('onclick')
self.loop = kwargs.get('loop')
self.surface = scale(imageload(self.image), self.size)
def rerender(self, **kwargs):
self.image = kwargs.get('image') or self.image
ws, hs = get_surface().get_size()
self.origin = kwargs.get('origin') or self.origin
rect = kwargs.get('rect')
if rect:
x, y, w, h = rect
self.rect = (
int(ws * (x - w / 2 + w / 2 * self.origin[0])), int(hs * (y - w / 2 + h / 2 * self.origin[1])),
int(ws * w),
int(hs * h))
self.pos, self.size = (self.rect[0], self.rect[1]), (self.rect[2], self.rect[3])
self.onclick = kwargs.get('onclick') or self.onclick
self.loop = kwargs.get('loop') or self.loop
self.surface = scale(imageload(self.image), self.size)
class Main: # Main application class
def __init__(self, **kwargs):
self.width = kwargs.get('width') or 1280 # Window width
self.height = kwargs.get('height') or 720 # Window height
self.fps = kwargs.get('fps') or 60 # In game FPS
self.title = kwargs.get('title') or 'Python Night Funkin' # Window title
self.loop = True # Main loop switcher
self.objs = [] # All objects
self.rects = [] # Rect objects
self.texts = [] # Text objects
self.images = [] # Image objects
self.onclicks = [] # On click events for objects
self.screen = set_mode((self.width, self.height)) # Init window
set_caption(self.title) # Set title
def event(self): # Event listener
for e in get():
if e.type == QUIT:
self.loop = False
if e.type == MOUSEBUTTONDOWN:
if e.button == 1:
for i in self.onclicks:
x, y, w, h = i.rect
if x <= e.pos[0] <= x + w and y <= e.pos[1] <= y + h:
i.onclick(i) # Call function if clicked
def mainloop(self): # Main loop
cl = Clock()
while self.loop:
self.screen.fill(BUTTON) # Fill screen
self.event() # Call events
for i in self.objs:
if i.loop:
i.loop(i)
self.render() # Call render
flip() # Update screen
cl.tick(self.fps) # FPS
def render(self): # Render objects
for i in self.rects:
drawrect(self.screen, i.color, i.rect) # Draw rects
for i in self.images:
self.screen.blit(i.surface, i.pos) # Draw images
for i in self.texts:
self.screen.blit(i.surface, i.pos) # Draw texts
def remove(self, obj): # Remove object from lists
if type(obj) == Rect:
self.rects.pop(self.rects.index(obj))
elif type(obj) == Text:
self.texts.pop(self.texts.index(obj))
elif type(obj) == Image:
self.images.pop(self.images.index(obj))
else:
return False
self.objs.pop(self.objs.index(obj))
self.onclicks.pop(self.onclicks.index(obj))
def rect(self, **kwargs): # Draw rect
obj = Rect(**kwargs)
if kwargs.get('onclick'):
self.onclicks += [obj]
self.rects += [obj]
self.objs += [obj]
return obj
def text(self, **kwargs): # Draw text
obj = Text(**kwargs)
if kwargs.get('onclick'):
self.onclicks += [obj]
self.texts += [obj]
self.objs += [obj]
return obj
def image(self, **kwargs): # Draw image
obj = Image(**kwargs)
if kwargs.get('onclick'):
self.onclicks += [obj]
self.images += [obj]
self.objs += [obj]
return obj
with open('data/settings.json', 'r') as f:
sets = load(f) # Import settings
with open('data/levels.json', 'r') as f:
lvls = load(f) # Import levels
##############################
# #
# Graphical User Interface #
# #
# ↓ ↓ ↓ #
# #
##############################
def playclick(obj): # On click play
obj.loop = playloop
def playloop(obj): # Play loop
x, y = settingsbtn.getpos()
settingsbtn.setpos(pos=(x - 0.005, y))
x, y = obj.getpos()
obj.setpos(pos=(x - 0.005, y))
if x <= 0:
obj.loop = None
global levelbtns
main.remove(obj)
main.remove(settingsbtn)
x, y = 0, 1
for i in lvls:
w, h = i['text_size']
y -= 0.1
lvl = main.text(text=i['text'], rect=(x, y, w, h), color=BUTTON, origin=(1, -1), loop=levelloop,
onclick=levelclick)
lvl.namespace = i['namespace']
levelbtns += [lvl]
def levelclick(obj): # On click level
global levelbtns
for i in levelbtns:
main.remove(i)
with open(f'data/game/{obj.namespace}/players.json', 'r') as f:
players = load(f)
with open(f'data/game/{obj.namespace}/song.json', 'r') as f:
song = load(f)
background.rerender(image=f'images/game/{obj.namespace}/background.png')
firstinit(players, song, obj)
secondinit(players, song, obj)
def firstinit(players, song, obj): # Init first player
if not len(players['first']):
return
x, y = players['first']['pos']
w, h = players['first']['size']
first = main.image(image=f"images/game/{obj.namespace}/players/first/{players['first']['images']['passive'][0]}",
loop=firstloop, rect=(x, y, w, h))
first.passiveimages = players['first']['images']['passive']
imgs = []
for i in first.passiveimages:
imgs += [f"images/game/{obj.namespace}/players/first/{i}"]
first.passiveimages = imgs
first.song = song
first.namespace = obj.namespace
first.rpt = True
def secondinit(players, song, obj): # Init second player
if not len(players['second']):
return
x, y = players['second']['pos']
w, h = players['second']['size']
second = main.image(image=f"images/game/{obj.namespace}/players/second/{players['second']['images']['passive'][0]}",
loop=secondloop, rect=(x, y, w, h))
second.passiveimages = players['second']['images']['passive']
imgs = []
for i in second.passiveimages:
imgs += [f"images/game/{obj.namespace}/players/second/{i}"]
second.passiveimages = imgs
second.song = song
second.namespace = obj.namespace
second.rpt = True
def levelloop(obj): # Level loop
x, y = obj.getpos()
obj.setpos(pos=(x + 0.005, y))
if x >= 0.1:
obj.loop = None
def firstloop(obj): # First player loop
if obj.rpt:
if len(obj.passiveimages) > obj.passiveimages.index(obj.image)+1:
obj.rerender(image=obj.passiveimages[obj.passiveimages.index(obj.image)+1])
else:
obj.rerender(image=obj.passiveimages[0])
def secondloop(obj): # Second player loop
if obj.rpt:
if len(obj.passiveimages) > obj.passiveimages.index(obj.image)+1:
obj.rerender(image=obj.passiveimages[obj.passiveimages.index(obj.image)+1])
else:
obj.rerender(image=obj.passiveimages[0])
def settingsclick(obj): # On click settings
obj.loop = settingsloop
def settingsloop(obj): # Settings loop
x, y = obj.getpos()
obj.setpos(pos=(x - 0.005, y))
x, y = playbtn.getpos()
playbtn.setpos(pos=(x - 0.005, y))
if obj.getpos()[0] <= 0:
global sets
rw, rh = sets['width'], sets['height']
fps = sets['fps']
obj.loop = None
main.remove(obj) # Remove settings button
main.remove(playbtn) # Remove play button
sts = [] # Settings buttons
backbtn = main.text(origin=(1, -1), rect=(0, 0.9, 0.11, 0.1), color=BUTTON, text='Back', onclick=backclick, loop=backloop) # Back button
sts += [main.text(origin=(-1, 0), rect=(1, 0.2, 0.15, 0.1), color=BUTTON, text='Resolution', loop=stsloop)] # Resolution button
sts += [main.text(origin=(-1, 0), rect=(0.95, 0.26, 0.10, 0.1), color=BUTTON, text=f'{rw}x{rh}', loop=stsloop)] # Resolution value
sts += [main.text(origin=(-1, 0), rect=(1, 0.42, 0.15, 0.1), color=BUTTON, text=f'FPS - {fps}', loop=stsloop)] # FPS button - value
backbtn.buttons = sts # Settings buttons
def stsloop(obj): # Settings buttons loop
x, y = obj.getpos()
obj.setpos(pos=(x - 0.005, y))
if x <= 0.8:
obj.loop = None
def backloop(obj): # Back loop
x, y = obj.getpos()
obj.setpos(pos=(x + 0.005, y))
if x >= 0.1:
obj.loop = None
def backoutloop(obj): # Second back loop
for i in obj.buttons:
x, y = i.getpos()
i.setpos(pos=(x + 0.005, y))
x, y = obj.getpos()
obj.setpos(pos=(x - 0.005, y))
if x <= 0:
global playbtn
global settingsbtn
main.remove(obj)
for i in obj.buttons:
main.remove(i)
playbtn = main.text(origin=(1, -1), rect=(0.1, 0.75, 0.12, 0.1), color=BUTTON, text='Play', onclick=playclick) # Play button
settingsbtn = main.text(origin=(1, -1), rect=(0.1, 0.9, 0.16, 0.1), color=BUTTON, text='Settings',
onclick=settingsclick) # Settings button
def backclick(obj): # On click back
obj.loop = backoutloop
levelbtns = [] # For level buttons
main = Main(width=sets['width'], height=sets['height'], fps=sets['fps'], title=sets['title']) # Creating window
# Objects
background = main.image(rect=(0, 0, 1, 1), image='images/menu/background.png') # Menu background
playbtn = main.text(origin=(1, -1), rect=(0.1, 0.75, 0.12, 0.1), color=BUTTON, text='Play', onclick=playclick) # Play button
settingsbtn = main.text(origin=(1, -1), rect=(0.1, 0.9, 0.16, 0.1), color=BUTTON, text='Settings', onclick=settingsclick) # Settings button
vertxt = main.text(origin=(1, -1), rect=(0.02, 1, 0.05, 0.03), text=sets['version'], color=BLACK) # Version label
main.mainloop() # Calling main loop