Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
646 lines (515 loc) · 23.1 KB
/
Copy pathmain.py
File metadata and controls
646 lines (515 loc) · 23.1 KB
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
import pygame
import pyperclip
pygame.init()
import os
import random
import sys
from camera import *
from enemy import *
from fx import *
from levels import *
from objects import *
from overlay import *
from player import *
from sfx import *
from stgs import *
from menu import *
import colors
loadSave(saveFile)
from stgs import *
#### Game object ####
class game:
#### Initialize game object ####
#
# Groups each sprite type to perform targetted tasks
# All sprites go into the sprites group
# Sets up window, font, gravity, and cam
# Loads data for the game levels and the player
def __init__(self):
self.layer1 = pygame.sprite.Group()
self.layer2 = pygame.sprite.Group()
self.fxLayer = pygame.sprite.Group()
self.overlayer = pygame.sprite.Group()
self.rendLayers = [self.layer1, self.layer2]
self.mixer = gameMixer()
self.mixer.setMusicVolume(musicVolume) # between 0 and 1
self.mixer.setFxVolume(fxVolume)
self.antialiasing = aalias
pygame.display.set_icon(pygame.image.load(asset('logo.png')))
self.win = pygame.display.set_mode((winWidth, winHeight))
pygame.display.set_caption("Cyber Space")
pygame.display.set_icon(pygame.image.load(asset('logo.png')))
self.font1 = pygame.font.Font(os.path.join('fonts', 'YuseiMagic-Regular.ttf'), 30)
self.font2 = pygame.font.SysFont('Comic Sans MS', 23)
self.menuFont = pygame.font.Font(os.path.join('fonts', 'YuseiMagic-Regular.ttf'), 15)
self.gameOverFont = pygame.font.Font(os.path.join('fonts', 'YuseiMagic-Regular.ttf'), 60)
self.victoryFont = pygame.font.Font(os.path.join('fonts', 'YuseiMagic-Regular.ttf'), 72)
self.lastPause = pygame.time.get_ticks()
self.won = False
self.points = 0
self.gravity = 1.6
self.currentFps = 0
self.showFps = SHOWFPS
self.fullScreen = False
self.cam = cam(winWidth, winHeight)
self.clock = pygame.time.Clock()
self.new()
def new(self):
self.enemies = pygame.sprite.Group()
self.sprites = pygame.sprite.Group()
self.pSprites = pygame.sprite.Group()
self.colliders = pygame.sprite.Group()
self.dmgRects = pygame.sprite.Group()
self.pBullets = pygame.sprite.Group()
self.eBullets = pygame.sprite.Group()
self.items = pygame.sprite.Group()
self.levels = gameLevels
self.player = player(self, asset('Space-ManR.png'), 'Cyber Man', imgSheet =
{'active': True, ## Will become deprecated but is usefulin current development. Allows use of sample image.
'tileWidth': 64,
'r': asset('player/idleR(2).png'),
'l': asset('player/idleL(2).png'),
'idleR': asset('player/idleR(2).png'),
'idleL': asset('player/idleL(2).png'),
'flyR': asset('player/flyR(2).png'),
'flyL': asset('player/flyL(2).png')})
self.player.gravity = self.gravity
self.end = False
self.pause = False
self.pauseScreen = pauseOverlay(self)
#### Determines how the run will function ####
def run(self):
self.menuLoop()
self.mixer.playMusic(asset('sounds/track 1.wav'))
self.mainLoop()
self.mixer.stop()
if self.won:
self.victoryLoop()
else:
self.gameOver()
#### Controls how the levels will load ####
def loadLevel(self, levelNum, *args):
for enemy in self.enemies:
enemy.kill()
for sprite in self.colliders:
sprite.kill()
for sprite in self.eBullets:
sprite.kill()
for sprite in self.pBullets:
sprite.kill()
for sprite in self.dmgRects:
sprite.kill()
for sprite in self.items:
sprite.kill()
if len(args) < 1:
try:
for obj in self.level.objects:
obj.kill()
except:
pass
self.level = self.levels[levelNum-1]
self.level.load(self)
self.cam.width, self.cam.height = self.level.rect.width, self.level.rect.height
#try:
self.player.rect.topleft = self.level.entrance.rect.center
# except:
# print("No player Pos")
else:
if args[0] is None:
levelDir = importLevelFile()
else:
levelDir = importLevel(args[0])
if levelDir is None:
self.reset()
else:
self.level = level(rendType=1, mapDir=levelDir)
self.level.load(self)
self.cam.width, self.cam.height = self.level.rect.width, self.level.rect.height
try:
self.player.rect.topleft = self.level.entrance.rect.center
except:
print("No player Pos")
self.reset()
self.player.gun.recenter()
#### Main game loop ####
def mainLoop(self):
while not self.end:
self.clock.tick(FPS)
self.refresh()
##Updates Game
self.runEvents()
self.update()
def update(self):
self.getFps()
self.getPause()
if self.pause:
self.pSprites.update()
self.pauseScreen.update()
else:
self.sprites.update()
self.checkHits()
self.cam.update(self.player)
self.render()
pygame.display.update()
def render(self):
self.win.blit(self.level.image, self.cam.apply(self.level))
for layer in self.rendLayers:
for sprite in layer:
try:
self.win.blit(sprite.image, self.cam.apply(sprite))
except:
pass
for fx in self.fxLayer:
self.win.blit(fx.image, fx.rect)
self.win.blit(transparentRect((winWidth, 60), 120), (0, 0))
visPoints = fonts['6'].render("Score: " + str(self.points), self.antialiasing, (255, 255, 255))
visLives = fonts['2'].render("Lives: " + str(self.player.lives), self.antialiasing, (255, 255, 255))
self.win.blit(visPoints, (100, 15))
self.win.blit(visLives, (700, 15))
for sprite in self.overlayer:
self.win.blit(sprite.image, sprite.rect)
if self.showFps:
fpsText = fonts['6'].render(str(self.currentFps), self.antialiasing, (255, 255, 255))
self.win.blit(fpsText, (1100, 5))
def checkHits(self):
### Checks for bullet collision among enemies and bullets
hits = pygame.sprite.groupcollide(self.enemies, self.pBullets, False, True)
for hit in hits:
hit.health -= self.player.gun.damage
if hit.health <= 0:
self.points += hit.points
hits = pygame.sprite.spritecollide(self.player, self.enemies, False)
for hit in hits:
self.player.takeDamage(hit.damage)
hits = pygame.sprite.spritecollide(self.player, self.dmgRects, False)
for hit in hits:
self.player.takeDamage(hit.damage)
hits = pygame.sprite.spritecollide(self.player, self.eBullets, False)
for hit in hits:
self.player.takeDamage(hit.damage)
hits = pygame.sprite.groupcollide(self.pBullets, self.colliders, True, False)
for hit in hits:
particles(self, hit.rect, lifeSpan = 300, tickSpeed=2, size = 5).setParticleKwargs(speed=1.5, shrink=0.6, life=140, color=colors.orangeRed)
pygame.sprite.groupcollide(self.colliders, self.eBullets, False, True)
items = pygame.sprite.spritecollide(self.player, self.items, True)
for item in items:
if isinstance(item, coin):
self.points += item.value
self.player.coinMeter.addCoin()
elif isinstance(item, gunConsumable):
self.player.gun.kill()
self.player.gun = item.gun(self, self.player)
self.points += 50
elif isinstance(item, hpPack1):
self.player.health += item.value
tpCols = pygame.sprite.spritecollide(self.player, self.level.teleporters, False)
for tp in tpCols:
fadeOut(self, speed = 20, alpha = 120, fadeBack = True)
self.player.rect.topleft = tp.target
self.player.gun.recenter()
### DEcryptor/key collision detection
try:
if pygame.sprite.collide_rect(self.player, self.level.key):
self.level.keyObtained = True
self.level.key.kill()
except:
pass
if pygame.sprite.collide_rect(self.player, self.level.door) and self.level.keyObtained:
self.pause = True
fadeOut(self, speed = 8, alpha = 40, onEnd = lambda:self.nextLevel())
if self.player.health <= 0 and not self.pause:
self.player.lives -= 1
if self.player.lives <= 0:
def endF():
self.end = True
self.pause = True
fadeOut(self, speed = 2, alpha = 40, color = colors.dark(colors.red, 60), noKill = True, onEnd = endF)
else:
self.pause = True
fadeOut(self, speed = 2, alpha = 40, color = colors.dark(colors.red, 60), noKill = True, onEnd = self.died)
def cont(self):
self.pause = False
self.loadLevel(self.getLvlNum())
self.player.health = self.player.maxHp
self.fxLayer.empty()
for s in self.pSprites:
s.kill()
def died(self):
button(self, (400, 400), groups = [self.pSprites, self.overlayer], text = "Continue", onClick=self.cont, instaKill = True, center = True, colors = (colors.cyan, colors.white))
def end():
self.end = True
button(self, (400, 500), groups = [self.pSprites, self.overlayer], text = "Return to menu", onClick=end, instaKill = True, center = True, colors = (colors.cyan, colors.white))
def getLvlNum(self, offSet=0):
return self.levels.index(self.level) + 1 + offSet
def unPause(self):
self.pause = False
self.pauseScreen.deactivate()
self.overlayer.remove(self.pauseScreen)
def reset(self):
for sprite in self.sprites:
sprite.kill()
for sprite in self.pSprites:
sprite.kill()
self.new()
self.run()
def nextLevel(self):
if DEBUG:
try:
self.loadLevel(self.levels.index(self.level) + 2)
fadeIn(self, speed = 20, onEnd = lambda:self.unPause())
except IndexError:
self.end = True
self.won = True
else:
try:
self.loadLevel(self.levels.index(self.level) + 2)
fadeIn(self, onEnd = lambda:self.unPause())
except:
self.end = True
self.won = True
def quit(self):
saveData(saveFile, self)
pygame.quit()
sys.exit()
def runEvents(self):
## Catch all events here
self.events = pygame.event.get()
for event in self.events:
if event.type == pygame.QUIT:
self.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
if self.fullScreen:
self.win = pygame.display.set_mode((winWidth, winHeight))
self.fullScreen = False
else:
self.quit()
self.getFullScreen()
def getFps(self):
self.currentFps = round(self.clock.get_fps(), 1)
return self.currentFps
def toggleFps(self):
if self.showFps:
self.showFps = False
else:
self.showFps = True
def toggleAalias(self):
if self.antialiasing:
self.antialiasing = False
else:
self.antialiasing = True
self.pauseScreen.loadComponents()
def getFullScreen(self):
keys = pygame.key.get_pressed()
if keys[keySet['fullScreen']]:
pygame.display.set_icon(pygame.image.load(asset('logo.png')))
if self.fullScreen:
self.win = pygame.display.set_mode((winWidth, winHeight))
self.fullScreen = False
else:
self.win = pygame.display.set_mode((winWidth, winHeight), pygame.FULLSCREEN)
self.fullScreen = True
pygame.display.set_icon(pygame.image.load(asset('logo.png')))
#pygame.display.toggle_fullscreen()
def getPause(self):
if pygame.time.get_ticks() - self.lastPause >= 180:
keys = pygame.key.get_pressed()
if keys[keySet['pause']]:
if self.pause:
self.unPause()
else:
self.pause = True
self.overlayer.add(self.pauseScreen)
self.pauseScreen.activate()
self.lastPause = pygame.time.get_ticks()
def getSprBylID(self, lID):
for sprite in self.sprites:
try:
if sprite.lID == lID:
return sprite
except:
pass
return False
#### First menu loop ####
def menuLoop(self):
def dipMenu():
returnButton = button(self, (winWidth/2, 100), text="Return", center = True)
slider1 = settingSlider(self, (800, 600))
comps = pygame.sprite.Group(returnButton, slider1)
while True:
pygame.time.delay(50)
self.runEvents()
self.refresh()
comps.update()
for comp in comps:
self.win.blit(comp.image, comp.rect)
if returnButton.clicked:
break
if checkKey(keySet['start']):
self.loadLevel(1)
break
text2 = fonts['1'].render('Cyber Space', self.antialiasing, (50, 255, 255))
self.win.blit(text2, (30,30))
keys = pygame.key.get_pressed()
pygame.display.update()
def compendiumMenu():
x, y = 100, 100
stepX, stepY = 120, 120
returnButton = button(self, (winWidth-240, 70), text="Return", center = True)
#menuItem(self, (x, y), asset(''), desc='', text=''),
itemCompendium = [
menuItem(self, (x, y), asset('objects/gun.png'), desc='The Standard Blaster -- Your first weapon.', text='Cyber Blaster'),
menuItem(self, (x+stepX, y), asset('objects/gun.png'), desc='The Triple Blaster -- THREE TIMES THE POWER \n Found In level 2', text='Cyber Blaster'),
menuItem(self, (x+stepX*2, y), asset('objects/lazgun.png'), desc='The Cyber Lazer 1 -- Incinerate your enemies with a beam that can travel through multiple enemies \n Found in level 6', text='Cyber Blaster'),
]
comps = pygame.sprite.Group(itemCompendium, returnButton)
descText = ''
text2 = text('2', descText, colors.cyan, self.antialiasing, (winWidth- 1200,winHeight - 240), True)
while True:
descText = ''
pygame.time.delay(50)
self.runEvents()
self.refresh()
comps.update()
for comp in comps:
self.win.blit(comp.image, comp.rect)
for i in itemCompendium:
if i.hover:
descText = i.desc
if returnButton.clicked:
break
if checkKey(keySet['start']):
self.loadLevel(1)
break
text1 = fonts['1'].render('Cyber Space', self.antialiasing, (50, 255, 255))
# text2 = fonts['2'].render(descText, self.antialiasing, (50, 255, 255))
text2 = text('4', descText, colors.cyan, self.antialiasing, (winWidth- 1200,winHeight - 240), True)
self.win.blit(text1, (30,winHeight - 70))
self.win.blit(text2.image, text2.pos)
keys = pygame.key.get_pressed()
pygame.display.update()
def settingsMenu():
returnButton = button(self, (winWidth-240, 70), text="Return", center = True)
comps = pygame.sprite.Group(returnButton)
audioSlider1 = settingSlider(self, (100, 350), addGroups = [comps])
audioSlider2 = settingSlider(self, (100, 500), addGroups = [comps])
audioSlider1.image.set_colorkey((0,0,0))
audioSlider2.image.set_colorkey((0,0,0))
fpsButton = button(self, (800, 250), text = 'Toggle FPS', onClick = lambda:self.toggleFps() ,groups = [comps], center = True)
aaliasButton = button(self, (800, 330), text = 'Toggle Anti - Aliasing', onClick = lambda:self.toggleAalias() ,groups = [comps], center = True)
texts = [
text('5', 'Paused', colors.cyan, self.antialiasing, (winWidth/2.4, 10)),
text('1', 'Audio Control', colors.cyan, self.antialiasing, (75, 250)),
text('6', 'Music Volume', colors.cyan, self.antialiasing, (75, 325)),
text('6', 'Fx Volume', colors.cyan, self.antialiasing, (75, 475))
]
def applyComps():
self.mixer.setMusicVolume(audioSlider1.get_ratio())
self.mixer.setFxVolume(audioSlider2.get_ratio())
audioSlider1.setRatio(self.mixer.musicVolume)
audioSlider2.setRatio(self.mixer.fxVolume)
while True:
pygame.time.delay(50)
self.runEvents()
self.refresh(asset('objects/genBg/genBg/Nether.jpg'))
applyComps()
comps.update()
for comp in comps:
self.win.blit(comp.image, comp.rect)
for t in texts:
self.win.blit(t.image, t.pos)
if returnButton.clicked:
break
if checkKey(keySet['start']):
self.loadLevel(1)
break
text2 = fonts['1'].render('Cyber Space', self.antialiasing, (50, 255, 255))
self.win.blit(text2, (30,30))
keys = pygame.key.get_pressed()
pygame.display.update()
def main():
startButton = button(self, (winWidth/2, 100), text="Start", center = True)
dipButton = button(self, (50, winHeight-120), text="Settings", center=True)
compendButton = button(self, (275, winHeight-120), text="Item Compendium", center=True)
loadCustomLevelBtn = button(self, (winWidth/2, 180), text="Load Custom Level (web)", center = True)
loadCustomLevelBtn2 = button(self, (winWidth/2, 260), text="Load Custom Level (file)", center = True)
comps = pygame.sprite.Group(startButton, loadCustomLevelBtn, loadCustomLevelBtn2, dipButton, compendButton) # Stands for components fyi
while True:
pygame.time.delay(50)
self.runEvents()
self.refresh(asset('objects/genBg/genBg/Nether.jpg'))
comps.update()
for comp in comps:
self.win.blit(comp.image, comp.rect)
if startButton.clicked:
self.loadLevel(1)
break
if loadCustomLevelBtn.clicked:
self.loadLevel(None, pyperclip.paste())
break
if loadCustomLevelBtn2.clicked:
self.loadLevel(None, None)
break
if dipButton.clicked:
settingsMenu()
dipButton.reset()
if compendButton.clicked:
compendiumMenu()
compendButton.reset()
text1 = self.font2.render('Press S to Start', self.antialiasing, (50, 255, 255))
text2 = self.font1.render('Welcome to Cyber Space', self.antialiasing, (50, 255, 255))
text3 = self.font1.render('Created by LGgameLAB', self.antialiasing, (50, 255, 255))
self.win.blit(text1, (30,30))
self.win.blit(text2, (100, 200))
self.win.blit(text3, (100, 300))
keys = pygame.key.get_pressed()
if keys[keySet['start']]:
self.loadLevel(1)
break
pygame.display.update()
main()
def victoryLoop(self):
menuButton = button(self, (winWidth/2, winHeight/2), text="Back to Menu", center = True, colors = (colors.yellow, colors.white))
buttons = pygame.sprite.Group(menuButton)
while True:
pygame.time.delay(50)
self.runEvents()
self.refresh()
buttons.update()
for btn in buttons:
self.win.blit(btn.image, btn.rect)
if menuButton.clicked:
self.reset()
break
text1 = self.victoryFont.render('Victory', self.antialiasing, colors.yellow, 20)
text2 = fonts['1'].render("Score: " + str(self.points), self.antialiasing, (colors.yellow))
self.win.blit(text2, (800, 70))
self.win.blit(text1, (winWidth/2 - text1.get_width()/2 ,30))
pygame.display.update()
def gameOver(self):
restartButton = button(self, (winWidth/2, winHeight/2), text="Back to Menu", center = True, colors = (colors.yellow, colors.white))
buttons = pygame.sprite.Group(restartButton)
while True:
pygame.time.delay(50)
self.runEvents()
self.refresh()
buttons.update()
for btn in buttons:
self.win.blit(btn.image, btn.rect)
if restartButton.clicked:
self.reset()
break
text1 = self.gameOverFont.render('Game Over', self.antialiasing, colors.dark(colors.red, 20))
text2 = fonts['1'].render("Score: " + str(self.points), self.antialiasing, (colors.yellow))
self.win.blit(text1, (50,50))
self.win.blit(text2, (800, 70))
pygame.display.update()
def refresh(self, bg = False):
if bg:
self.win.blit(pygame.transform.scale(pygame.image.load(bg), (winWidth, winHeight)).convert(), (0, 0))
else:
self.win.fill((0, 0, 0))
#### Creates and runs game ####
game1 = game()
while __name__ == '__main__':
game1.run()