-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·829 lines (692 loc) · 30.4 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
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
#! /usr/bin/env python
import pygame, sys, fov, time
from pygame.locals import KEYDOWN, KEYUP, K_DOWN, K_UP, K_LEFT, K_RIGHT, \
K_ESCAPE, K_RETURN
from constants import *
from player import Player
from agent import Agent
from crystal import Crystal
class Game:
def __init__(self):
pygame.init()
pygame.display.set_caption("Light Eater")
self.numRows = 0
self.numCols = 0
self.player = None
# The level stored as a 2d matrix
self.level = None
self.spawnPos = (0,0)
self.yMargin = 0
self.xMargin = 0
self.numOfColors = 0
self.deathCount = 0
self.agents = []
self.crystals = []
# A 2d matrix of same size as level where each element indicates weather
# that tile has been explored or not and is currently visible or not
self.visibilityMap = None
# A 2d matrix of same size as level where each elem is a list containing
# the agents currently lighting up that tile
self.lightMap = None
self.colorDict = {'R' : RED, 'B' : BLUE, 'P' : PURPLE,
'Y' : YELLOW, 'G' : GREEN, 'O' : ORANGE}
# Holds the alpha of all colors in a level, indicating progress
# and determines the alpha of colors in the lightbar
self.currAlphaDict = {}
# Holds the amound that the alpha of a color should increase with
# when a light of that color is eaten
self.incAlphaDict = {}
# Colors that are safe to interavt with, also the colors currently in
# the lightbar
self.colorsFound = []
self.bgColor = None
self.updateLightBar = False
self.levelSurf = None
self.workSurf = None
self.lightBarSurf = None
self.displaySurf = None
self.width = 0
self.height = 0
self.maxLevel = 4
self.currLevel = 1
self.bestTimes = [[MAXTIME]*3 for i in range(self.maxLevel)]
# The lightbar is made up of 6 elements with lightbarElemWid width
self.lightbarElemWid = 0
self.initWindow()
def initWindow(self):
self.displaySurf = pygame.display.set_mode((0,0))
self.width = self.displaySurf.get_width()
self.height = self.displaySurf.get_height()
self.displaySurf = pygame.display.set_mode((self.width, self.height),
pygame.FULLSCREEN)
# Enter main game loop
def run(self):
fpsClock = pygame.time.Clock()
#self.playMusic() #TODO ENABLE
self.readBestTimes()
gameMode = MENU
while gameMode is not QUIT:
self.displaySurf.fill(BLACK)
if gameMode is MENU:
gameMode = self.showMainMenu(fpsClock)
elif gameMode is LEVEL:
gameMode = self.showLevelsMenu(fpsClock)
elif gameMode is PLAY:
gameMode = self.playing(fpsClock)
elif gameMode is CREDITS:
gameMode = self.showCredits(fpsClock)
def readBestTimes(self):
dataFile = open('data.txt')
i = 0
for line in dataFile:
line = line.split()
self.bestTimes[i] = line
i += 1
dataFile.close()
# Start playing and endlessly loop the music
def playMusic(self):
pygame.mixer.init()
pygame.mixer.music.load('music.mp3')
pygame.mixer.music.play(-1)
def showMainMenu(self, fpsClock):
MIN_ALPHA = 70
activeChoice = LEVEL
playAlpha = MAX_ALPHA
creditsAlpha = MIN_ALPHA
exitAlpha = MIN_ALPHA
while 1:
if activeChoice is LEVEL:
if playAlpha < MAX_ALPHA:
playAlpha += 10
elif playAlpha > MIN_ALPHA:
playAlpha -= 10
if activeChoice is CREDITS:
if creditsAlpha < MAX_ALPHA:
creditsAlpha += 10
elif creditsAlpha > MIN_ALPHA:
creditsAlpha -= 10
if activeChoice is QUIT:
if exitAlpha < MAX_ALPHA:
exitAlpha += 10
elif exitAlpha > MIN_ALPHA:
exitAlpha -= 10
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_DOWN and activeChoice < QUIT:
activeChoice += 1
elif event.key == K_UP and activeChoice > LEVEL:
activeChoice -= 1
elif event.key == K_ESCAPE:
return QUIT
elif event.key == K_RETURN:
return activeChoice
elif event.key == pygame.K_SPACE: # TODO REMOVE
self.playMusic()
if event.type == pygame.locals.QUIT:
self.quitGame()
self.renderMainMenu(playAlpha, creditsAlpha, exitAlpha)
fpsClock.tick(FPS)
def renderMainMenu(self, playAlpha, creditsAlpha, exitAlpha):
MENU_ITEM_HI = TILESIZE * 5
MENU_ITEM_WID = TILESIZE * 19
playPos = (self.width / 2 - MENU_ITEM_WID, self.height / 5)
creditsPos = (self.width / 2 - MENU_ITEM_WID/4,
self.height / 5 + MENU_ITEM_HI*2)
exitPos = (self.width / 2 + (MENU_ITEM_WID*6)/7,
self.height / 5 + MENU_ITEM_HI*4)
playSurf = pygame.Surface((MENU_ITEM_WID, MENU_ITEM_HI)).convert_alpha()
creditsSurf = pygame.Surface((MENU_ITEM_WID, MENU_ITEM_HI)).convert_alpha()
exitSurf = pygame.Surface((MENU_ITEM_WID, MENU_ITEM_HI)).convert_alpha()
surf = pygame.Surface((200, 100))
font = pygame.font.Font(None,80)
infoFont = pygame.font.Font(None,40)
text = font.render("Play", 1, BLUE)
surf.fill(BLACK)
surf.set_alpha(playAlpha)
surf.blit(text, (0, 0))
playSurf.blit(surf, (0,0))
text = font.render("Credits", 1, YELLOW)
surf.fill(BLACK)
surf.set_alpha(creditsAlpha)
surf.blit(text, (0, 0))
creditsSurf.blit(surf, (0,0))
text = font.render("Exit", 1, RED)
surf.fill(BLACK)
surf.set_alpha(exitAlpha)
surf.blit(text, (0, 0))
exitSurf.blit(surf, (0,0))
infoStr = "Move with arrow keys, select with enter, back up with esc"
infoText = infoFont.render(infoStr, 1, PURPLE)
infoTextX = self.width / 2 - infoText.get_width() / 2
infoTextY = self.height - infoText.get_height()
self.displaySurf.blit(infoText, (infoTextX, infoTextY))
self.displaySurf.blit(playSurf, playPos)
self.displaySurf.blit(creditsSurf, creditsPos)
self.displaySurf.blit(exitSurf, exitPos)
pygame.display.update()
def showLevelsMenu(self, fpsClock):
HOR_OFFSET = 150
VER_OFFSET = 50
levelTextX = self.width / 2 - 150
levelTextY = self.height / 2 - 20
font = pygame.font.Font(None, 40)
while 1:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_DOWN and self.currLevel < self.maxLevel:
self.currLevel += 1
elif event.key == K_UP and self.currLevel > 1:
self.currLevel -= 1
elif event.key == K_ESCAPE:
return MENU
elif event.key == K_RETURN:
return PLAY
if event.type == pygame.locals.QUIT:
self.quitGame()
self.displaySurf.fill(BLACK)
levelText = font.render("Level " + str(self.currLevel), 1, ORANGE)
bestTimeText = font.render("Best times: ", 1, GREEN)
self.displaySurf.blit(bestTimeText,
(levelTextX+HOR_OFFSET, levelTextY-VER_OFFSET))
for i in range(3):
timeText = str(self.bestTimes[self.currLevel-1][i])
if timeText == str(MAXTIME):
timeText = "--"
bestTimeText = font.render(timeText + " seconds", 1, GREEN)
self.displaySurf.blit(bestTimeText,
(levelTextX+HOR_OFFSET, levelTextY+i*VER_OFFSET))
self.displaySurf.blit(levelText, (levelTextX, levelTextY))
pygame.display.update()
fpsClock.tick(FPS)
def showCredits(self, fpsClock):
font = pygame.font.Font(None,40)
textX = self.width / 2 - 200
textY = self.height / 2 - 20
while 1:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
return MENU
elif event.key == K_RETURN:
return MENU
if event.type == pygame.locals.QUIT:
self.quitGame()
text = font.render("Made by Olle Olsson", 1, YELLOW)
self.displaySurf.fill(BLACK)
self.displaySurf.blit(text, (textX, textY))
pygame.display.update()
fpsClock.tick(FPS)
# Load and play a level
def playing(self, fpsClock):
fovCounter = 0
startTime = time.time()
self.loadLevel(self.currLevel)
# Called by Fov alg to check if a tile blocks LoS on tiles behind it
def tileBlockes(x, y):
return self.level[x][y] == WALL
# Called by FoV algorithm for each tile that the player has LOS on and
# is within manhattan distance of player.
# Does squaresum distance check to make the FoV area a circle rather
# than square.
def markVisible(x, y):
if self.getDist(x*TILESIZE, y*TILESIZE) / TILESIZESQ \
< PLAYER_RANGE_SQ:
self.visibilityMap[x][y] = [EXPLORED, LIT]
while 1:
escPressed = self.handleInput()
if escPressed is True:
return LEVEL
self.movePlayer(self.player)
if self.fellIntoHole():
self.respawn()
for agent in self.agents:
agent.move()
if self.checkInLight():
fovCounter = 0
if fovCounter == 0:
self.updateFOV(tileBlockes, markVisible)
fovCounter = FOV_UPDATE_RATE
else:
fovCounter -= 1
self.checkIfFoundCrystal()
if self.checkLevelCompleted():
if self.onLevelCompleted(startTime):
self.currLevel += 1
self.loadLevel(self.currLevel)
startTime = time.time()
fovCounter = 0
else:
return MENU
self.render()
fpsClock.tick(FPS)
#print fpsClock.get_fps()
# Get level data from file and store in various attributes
def loadLevel(self, levelNum):
playerSpawnOffset = (TILESIZE - PLAYERSIZE) / 2
level = self.getLevel(levelNum)
levelDims = level[0].split()
# +2 for surrounding walls
self.numCols = int(levelDims[0]) + 2
self.numRows = int(levelDims[1]) + 2
self.spawnPos = (int(level[1].split()[0]), int(level[1].split()[1]))
self.player = Player(TILESIZE*self.spawnPos[0] + playerSpawnOffset,
TILESIZE*self.spawnPos[1] + playerSpawnOffset,
PLAYERSPEED, PLAYERSIZE)
self.yMargin = (self.height - TILESIZE * self.numRows) / 2
self.xMargin = (self.width - TILESIZE * self.numCols) / 2
self.level = [[OPEN]*self.numRows for i in range(self.numCols)]
self.lightMap = [[None]*self.numRows for i in range(self.numCols)]
self.bgColor = BGCOLORS[1]
self.updateLightBar = True
self.agents = []
self.crystals = []
self.colorsFound = []
self.currAlphaDict = {}
self.incAlphaDict = {}
self.deathCount = 0
crystalsList = level[2].split()
# Each crystal is specified by 3 ints
numOfCrystals = len(crystalsList) / 3
self.setCrystals(crystalsList, numOfCrystals)
# Determine number of colors a game has depending on number of crystals
if numOfCrystals is 3:
self.numOfColors = 6
elif numOfCrystals is 2:
self.numOfColors = 3
else:
self.numOfColors = 1
agentCount = int(level[3])
colorCountDict = self.addAgents(level, agentCount)
self.setColorAlphas(colorCountDict)
self.setMap(level, agentCount)
self.setSurfaces()
self.setVisibilityMap()
# Read level data from file and return it as a list
def getLevel(self, levelNum):
levelFile = open('levels.txt')
line = levelFile.readline()
level = []
levelString = "level " + str(levelNum) + "\n"
# Find block of level levelNum
while line != levelString:
line = levelFile.readline()
assert line != "EOF\n", "Error. Level not found on file" \
+ str(levelNum)
# Store each line as an element in the list level
line = levelFile.readline()
while line != "\n":
level.append(line.strip())
line = levelFile.readline()
levelFile.close()
return level
def setCrystals(self, crystalsList, numOfCrystals):
for i in range(numOfCrystals):
self.crystals.append(Crystal(int(crystalsList[i*3]), \
int(crystalsList[i*3 + 1]), crystalsList[i*3 + 2]))
def addAgents(self, level, agentCount):
colorCountDict = {'B':0, 'R':0, 'Y':0, 'P':0, 'O':0, 'G':0}
for i in range(agentCount):
patrolList = []
coordList = level[4+i].split()
for j in range((len(coordList)-3)/2):
patrolList.append((int(coordList[j*2]), int(coordList[j*2+1])))
self.agents.append(Agent(patrolList, coordList[-3], \
coordList[-2], coordList[-1]))
colorCountDict[coordList[-3]] += 1
return colorCountDict
# Calculate by how much the alpha in the lightbar should increase when
# the player eats a light, for that color
def setColorAlphas(self, agentColorDict):
alphaToFill = MAX_ALPHA - BASE_ALPHA
for color in agentColorDict:
self.currAlphaDict[color] = 0
if agentColorDict[color] is not 0:
# +1 to ensure alpha is >= max alpha when all lights are eaten
self.incAlphaDict[color] = \
(alphaToFill / agentColorDict[color]) + 1
def setMap(self, level, agentCount):
self.setSurroundingWall()
levelDescriptLine = 4 + agentCount
# set inner area of map
for row in range(1, self.numRows-1):
line = list(level[levelDescriptLine])
for col in range(1, self.numCols-1):
if line[col-1] == 'W':
self.level[col][row] = WALL
elif line[col-1] == '-':
self.level[col][row] = OPEN
elif line[col-1] == 'H':
self.level[col][row] = HOLE
else: #S
self.level[col][row] = SAFE
levelDescriptLine += 1
def setSurroundingWall(self):
for i in range(self.numCols):
self.level[i][0] = WALL
self.level[i][self.numRows-1] = WALL
for i in range(self.numRows):
self.level[0][i] = WALL
self.level[self.numCols-1][i] = WALL
# Create surfaces appropriate for the level size
def setSurfaces(self):
levelWid = self.numCols * TILESIZE
levelHi = self.numRows * TILESIZE
self.levelSurf = pygame.Surface((levelWid, levelHi))
self.workSurf = self.levelSurf.copy().convert_alpha()
self.lightBarSurf = pygame.Surface((levelWid, TILESIZE)).convert()
self.lightbarElemWid = levelWid / LIGHTBAR_ELEMS
# Prefer slightly too long lightbar over slightly too short
if self.lightbarElemWid % 2 is 1:
self.lightbarElemWid += 1
self.lightBarElemSurf = pygame.Surface((self.lightbarElemWid,
TILESIZE)).convert()
self.makeLevelSurf()
# Draw unchanging parts of a level to self.levelSurf
def makeLevelSurf(self):
SAFE_ICON_SIZE = TILESIZE / 4
CENTER_OFFSET = TILESIZE / 2
for row in range(self.numRows):
for col in range(self.numCols):
x = col * TILESIZE
y = row * TILESIZE
tileRect = pygame.Rect(x, y, TILESIZE, TILESIZE)
if self.level[col][row] == WALL:
pygame.draw.rect(self.levelSurf, GRAY, tileRect)
elif self.level[col][row] == OPEN:
pygame.draw.rect(self.levelSurf, self.bgColor, tileRect)
elif self.level[col][row] == HOLE:
pygame.draw.rect(self.levelSurf, BLACK, tileRect)
else: #Safe
pygame.draw.rect(self.levelSurf, self.bgColor, tileRect)
pygame.draw.circle(self.levelSurf, SAFECOLOR,
(x+CENTER_OFFSET, y+CENTER_OFFSET), SAFE_ICON_SIZE)
def setVisibilityMap(self):
self.visibilityMap = [[[UNEXPLORED, UNLIT] \
for j in range(self.numRows)] for i in range(self.numCols)]
# Return true if player died or absorbed a light
def checkInLight(self):
agentList = self.lightMap[self.getPlayerCoordX()][self.getPlayerCoordY()]
if not agentList:
return False
for agent in agentList:
if agent.color not in self.colorsFound:
self.respawn()
else:
self.absorbLight(agent)
return True
# Return true if all agents of the given color has been absorbed
def colorMaxed(self, color):
return self.currAlphaDict[color] >= MAX_ALPHA
# Remove light and update lightbar color alpha
def absorbLight(self, agent):
absColor = agent.color
self.currAlphaDict[absColor] += self.incAlphaDict[absColor]
if self.colorMaxed(absColor):
self.checkShouldMix(absColor)
self.agents.remove(agent)
self.updateLightBar = True
# Check if 2 colors were maxed and should merge into a new color
def checkShouldMix(self, absColor):
self.checkShouldMixHelper(absColor, ['B', 'Y'], 'G')
self.checkShouldMixHelper(absColor, ['B', 'R'], 'P')
self.checkShouldMixHelper(absColor, ['R', 'Y'], 'O')
# compColors is a list of two colors that merge into newColor
def checkShouldMixHelper(self, absColor, compColors, newColor):
if absColor in compColors:
if self.colorMaxed(compColors[0]) and \
self.colorMaxed(compColors[1]):
self.currAlphaDict[newColor] = BASE_ALPHA
# Add color elems to lightbar depeneding on num of colors in lvl
if self.numOfColors == MAX_NUM_COLORS:
self.colorsFound += list(newColor)
else:
self.colorsFound += list(newColor)*2
# If time is new top3 time, store it and move other times appropriately
def checkIfRecordTime(self, time):
time += self.deathCount*DEATH_PENALTY
i = 0
while i < 3:
if time < float(self.bestTimes[self.currLevel-1][i]):
# new top3 time found, move others down in record list
j = 2
while j > i:
self.bestTimes[self.currLevel-1][j] = \
self.bestTimes[self.currLevel-1][j-1]
j -= 1
self.bestTimes[self.currLevel-1][i] = time
break
i += 1
def respawn(self):
offset = (TILESIZE - PLAYERSIZE) / 2
self.player.setPos(self.spawnPos[0] * TILESIZE + offset,
self.spawnPos[1] * TILESIZE + offset)
self.deathCount += 1
# Check if a crystal was found, if so add it to found and remove from map
def checkIfFoundCrystal(self):
i = 0
xCoord = self.getPlayerCoordX()
yCoord = self.getPlayerCoordY()
while i < len(self.crystals):
crystal = self.crystals[i]
if xCoord is crystal.x and yCoord is crystal.y:
# The more colors exist in the map, the smaller the area of a
# color in the lightbar
if self.numOfColors is 6:
self.colorsFound.append(crystal.color)
elif self.numOfColors is 3:
self.colorsFound += list(crystal.color)*2
else:
self.colorsFound += list(crystal.color)*6
self.currAlphaDict[crystal.color] += BASE_ALPHA
del self.crystals[i]
self.updateLightBar = True
break
i += 1
# Return true if fell into hole
def fellIntoHole(self):
x = self.getPlayerCoordX()
y = self.getPlayerCoordY()
return self.level[x][y] == HOLE
# Return true if level is completed
def checkLevelCompleted(self):
if not self.agents:
return True
return False
def movePlayer(self, player):
nextPlayerX = self.player.nextX()
nextPlayerY = self.player.nextY()
if player.movingLeft:
if (self.isWall(nextPlayerX, self.player.y) or
self.isWall(nextPlayerX, self.player.y + PLAYERSIZE)):
self.player.clampLeft(TILESIZE)
else:
self.player.moveLeft()
if player.movingRight:
if (self.isWall(nextPlayerX + PLAYERSIZE, self.player.y) or
self.isWall(nextPlayerX + PLAYERSIZE, self.player.y + PLAYERSIZE)):
self.player.clampRight(TILESIZE)
else:
self.player.moveRight()
if player.movingUp:
if (self.isWall(self.player.x, nextPlayerY) or
self.isWall(self.player.x + PLAYERSIZE, nextPlayerY)):
self.player.clampUp(TILESIZE)
else:
self.player.moveUp()
if player.movingDown:
if (self.isWall(self.player.x, nextPlayerY + PLAYERSIZE) or
self.isWall(self.player.x + PLAYERSIZE, nextPlayerY + PLAYERSIZE)):
self.player.clampDown(TILESIZE)
else:
self.player.moveDown()
def tileLit(self, x, y):
if self.visibilityMap[x][y][1] is LIT:
return True
return False
def tileExplored(self, x, y):
if self.visibilityMap[x][y][0] is EXPLORED:
return True
return False
def render(self):
tileSurf = pygame.Surface((TILESIZE, TILESIZE))
tileSurf.fill(FOG_COLOR)
self.workSurf.fill(NOCOLOR)
self.renderPlayer()
for row in range(self.numRows):
for col in range(self.numCols):
x = col * TILESIZE
y = row * TILESIZE
tileRect = pygame.Rect(x, y, TILESIZE, TILESIZE)
if self.tileLit(col, row):
for agent in self.lightMap[col][row]:
agentDist = self.get2pDist(x, y, agent.x, agent.y)
# +1 means agentDist = 0 gives full alpha
alpha = MAX_ALPHA / (agentDist + 1)
tileSurf.fill(self.colorDict[agent.color])
tileSurf.set_alpha(alpha)
self.workSurf.blit(tileSurf, (x, y))
# Draw heavier fog the further the area is from the player
tileSurf.fill(FOG_COLOR)
alpha = (self.getDist(x, y) / TILESIZESQ)*ALPHA_FACTOR
if alpha > FOG_ALPHA:
tileSurf.set_alpha(FOG_ALPHA)
else:
tileSurf.set_alpha(alpha)
self.workSurf.blit(tileSurf, (x, y))
elif self.tileExplored(col, row):
tileSurf.set_alpha(FOG_ALPHA)
self.workSurf.blit(tileSurf, (x, y))
else:
pygame.draw.rect(self.workSurf, FOG_COLOR, tileRect)
if(self.updateLightBar):
self.renderLightbar()
self.updateLightBar = False
self.renderCrystals()
self.displaySurf.blit(self.levelSurf, (self.xMargin, self.yMargin))
self.displaySurf.blit(self.workSurf, (self.xMargin, self.yMargin))
pygame.display.update()
def renderLightbar(self):
self.lightBarSurf.fill(BLACK)
offset = 0
for color in self.colorsFound:
self.lightBarElemSurf.set_alpha(self.currAlphaDict[color])
self.lightBarElemSurf.fill(self.colorDict[color])
self.lightBarSurf.blit(self.lightBarElemSurf, (offset*self.lightbarElemWid, 0))
offset += 1
self.displaySurf.blit(self.lightBarSurf, (self.xMargin, self.yMargin - TILESIZE))
def renderCrystals(self):
for crystal in self.crystals:
if self.tileLit(crystal.x, crystal.y):
rect = pygame.Rect(crystal.x*TILESIZE, crystal.y*TILESIZE,
TILESIZE, TILESIZE)
pygame.draw.polygon(self.workSurf, self.colorDict[crystal.color], \
[[rect.midleft[0], rect.midleft[1]], \
[rect.midtop[0], rect.midtop[1]], [rect.midright[0],
rect.midright[1]], [rect.midbottom[0], rect.midbottom[1]]], 0)
def renderPlayer(self):
pygame.draw.rect(self.workSurf, WHITE, pygame.Rect(self.player.x, \
self.player.y, PLAYERSIZE, PLAYERSIZE))
# Return tilegrid x-coordinate
def getAgentCoordX(self, pos):
return (pos + HALF_GUARD_SIZE) / TILESIZE
# Return tilegrid y-coordinate
def getAgentCoordY(self, pos):
return (pos + HALF_GUARD_SIZE) / TILESIZE
# Return tilegrid x-coordinate of player center
def getPlayerCoordX(self):
return (self.player.x + HALFPLAYERSIZE) / TILESIZE
# Return tilegrid y-coordinate of player center
def getPlayerCoordY(self):
return (self.player.y + HALFPLAYERSIZE) / TILESIZE
# Return distance between player and (x,y) as squaresum
# TODO? Currently uses player topleft not center
def getDist(self, x, y):
return abs(self.player.x - x)**2 + abs(self.player.y - y)**2
def get2pDist(self, x, y, x1, y1):
return (abs(x1 - x)**2 + abs(y1 - y)**2) / TILESIZESQ
def setFogAlpha(self, fog, dist):
alpha = (dist / (TILESIZE**2)) * 7
fog.set_alpha(alpha)
# x, y coordinates without margins
def isWall(self, x, y):
return self.level[x / TILESIZE][y / TILESIZE] == WALL
# Return true if esc was pressed
def handleInput(self):
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_LEFT:
self.player.setMovingLeft(True)
if event.key == K_RIGHT:
self.player.setMovingRight(True)
if event.key == K_UP:
self.player.setMovingUp(True)
if event.key == K_DOWN:
self.player.setMovingDown(True)
if event.key == K_ESCAPE:
return True
if event.type == KEYUP:
if event.key == K_LEFT:
self.player.setMovingLeft(False)
if event.key == K_RIGHT:
self.player.setMovingRight(False)
if event.key == K_UP:
self.player.setMovingUp(False)
if event.key == K_DOWN:
self.player.setMovingDown(False)
if event.type == pygame.locals.QUIT:
self.quitGame()
return False
# Set all tiles to be unlit and uncolored
def resetFOV(self):
for col in range(self.numCols):
for row in range(self.numRows):
self.visibilityMap[col][row][1] = UNLIT
self.lightMap[col][row] = []
def updateFOV(self, tileBlockes, markVisible):
self.resetFOV()
xCoord = self.getPlayerCoordX()
yCoord = self.getPlayerCoordY()
fov.fieldOfView(xCoord, yCoord, self.numCols, self.numRows, PLAYER_RANGE, \
markVisible, tileBlockes)
for agent in self.agents:
if self.guardFovInRange(agent):
xCoord = self.getAgentCoordX(agent.x)
yCoord = self.getAgentCoordY(agent.y)
# Called by fov alg for every tile that an agent cast light on.
def markColored(x, y):
if self.tileLit(x, y) and not tileBlockes(x, y) and \
self.get2pDist(agent.x, agent.y, x*TILESIZE, y*TILESIZE) \
<= agent.range**2:
self.lightMap[x][y].append(agent)
fov.fieldOfView(xCoord, yCoord, self.numCols, self.numRows,
agent.range, markColored, tileBlockes)
def guardFovInRange(self, agent):
return self.getDist(agent.x, agent.y) <= \
((PLAYER_RANGE + agent.range) * TILESIZE)**2
def quitGame(self):
self.saveTimes()
pygame.quit()
sys.exit()
def saveTimes(self):
dataFile = open('data.txt', 'w')
output = ""
for i in self.bestTimes:
for j in i:
output += str(j) + " "
output += "\n"
dataFile.write(output)
dataFile.close()
# Return true if next level should be started immediatly
def onLevelCompleted(self, startTime):
# Truncate to 2 decimals
newTime = float((int((time.time() - startTime)*100)))/100.0
self.checkIfRecordTime(newTime)
if self.currLevel < self.maxLevel:
return True
return False
def main():
g = Game()
g.run()
g.quitGame()
if __name__ == '__main__':
main()