PinmikPanik/gamedata/objects/ingame/manager.py

71 lines
2.7 KiB
Python
Raw Normal View History

2021-09-11 14:33:20 +02:00
from gamedata.objects.base import BaseObject
2021-09-13 10:34:34 +02:00
from gamedata.objects.ingame.spawner import Spawner
2021-09-11 15:12:53 +02:00
from gamedata.objects.ingame.tiles import Tiles
2021-09-11 14:33:20 +02:00
class Manager(BaseObject):
def __init__(self,game):
super().__init__(0,0,game)
self.score = 0
2021-09-11 15:12:53 +02:00
self.lives = 5
self.basetime = 10
self.steptime = 2
2021-09-13 13:13:38 +02:00
self.speedmargin = 7
self.stepmargin = 2
2021-09-11 15:12:53 +02:00
self.spawntimer = game.lib.Timer(self.basetime) # Time elapsing each lemming spawn
2021-09-13 11:14:58 +02:00
self.deathtimer = game.lib.Timer(1) # Time betwin each life loss
2021-09-13 19:13:54 +02:00
self.started = False
2021-09-13 11:14:58 +02:00
self.invincible = False
2021-09-13 13:13:38 +02:00
self.scoreratio = 0.2 # Points earned per seconds and per lemming
2021-09-11 15:12:53 +02:00
2021-09-13 19:13:54 +02:00
self.endtimer = game.lib.Timer(1) # Black fade if game's over
self.endrect = game.pygame.Surface((game.DISPLAY_WIDTH,game.DISPLAY_HEIGHT))
self.endrect.fill([0]*3)
2021-09-11 15:12:53 +02:00
# Summon the tiles
self.tiles = Tiles(50,50,game)
game.gameloop.summon(self.tiles)
2021-09-13 10:34:34 +02:00
# Spawn the first one
self.game.gameloop.summon(Spawner(self.game,self.tiles))
2021-09-11 14:33:20 +02:00
def step(self):
2021-09-13 19:13:54 +02:00
nblemmings = len(self.game.gameloop.findname("Lemming")) + len(self.game.gameloop.findname("Spawner"))
2021-09-13 11:14:58 +02:00
# Updating score
for i in range(nblemmings):
2021-09-11 14:33:20 +02:00
self.score+=self.scoreratio*self.game.dt
2021-09-13 11:14:58 +02:00
# Spawning more lemmings
2021-09-13 19:13:54 +02:00
if (self.lives>0 and nblemmings>0):
if self.spawntimer.tick(self.game.dt):
self.game.gameloop.summon(Spawner(self.game,self.tiles,speedmargin=int(self.speedmargin+0.5)))
self.spawntimer = self.game.lib.Timer(self.basetime+nblemmings*self.steptime)
else:
# Game over
if self.endtimer.tick(self.game.dt):
self.game.globals["score"] = self.score
self.game.scenes.gameover(self.game)
2021-09-11 14:33:20 +02:00
2021-09-13 11:14:58 +02:00
if self.invincible:
if self.deathtimer.tick(self.game.dt):
self.invincible = False
def death(self):
if not self.invincible:
self.lives-=1
self.invincible = True
2021-09-13 13:13:38 +02:00
else:
self.deathtimer.tick(self.deathtimer.getmax()/3)
2021-09-13 11:14:58 +02:00
2021-09-11 14:33:20 +02:00
def draw(self):
pass
2021-09-13 11:33:40 +02:00
def afterdraw(self):
txtsurfacescore = self.game.fontfile.render("Score : "+str(int(self.score)),False,[150,255,150])
2021-09-13 19:13:54 +02:00
txtsurfacelives = self.game.fontfile.render("Lives : "+str(int(max(0,self.lives))),False,[255,150,150])
2021-09-13 11:33:40 +02:00
self.game.realwindow.blit(txtsurfacescore,[20,20])
self.game.realwindow.blit(txtsurfacelives,[20,20*2+txtsurfacescore.get_height()])
2021-09-13 19:13:54 +02:00
self.endrect.set_alpha((1-self.endtimer.getratio())*255)
self.game.realwindow.blit(self.endrect,[0,0])