PinmikPanik/gamedata/objects/ingame/manager.py

106 lines
4.2 KiB
Python

from gamedata.objects.base import BaseObject
from gamedata.objects.ingame.spawner import Spawner
from gamedata.objects.ingame.tiles import Tiles
from gamedata.objects.ingame.clouds import Clouds
from gamedata.objects.ingame.skies import Skies
from gamedata.objects.ingame.demo import Demo
class Manager(BaseObject):
def __init__(self,game):
super().__init__(0,0,game)
self.score = 0
self.lives = 5
self.basetime = 10
self.steptime = 2
self.speedmargin = 7
self.speedstep = 2.2
self.spawntimer = game.lib.Timer(self.basetime) # Time elapsing each lemming spawn
self.deathtimer = game.lib.Timer(1) # Time between each life loss
self.started = False
self.invincible = False
self.scoreratio = 0.2 # Points earned per seconds and per lemming
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)
# Sky gradients
self.game.gameloop.summon(Skies(game))
# Clouds in the background
self.clouds = Clouds(game,game.DISPLAY_WIDTH,speed=30,cameraratio=0.8)
self.cloudsdark = Clouds(game,game.DISPLAY_WIDTH,speed=10,cameraratio=0.4,spritename="cloudsdark.png")
self.game.gameloop.summon(self.cloudsdark)
self.game.gameloop.summon(self.clouds)
# Summon the tiles
self.tiles = Tiles(288,0,game)
game.gameloop.summon(self.tiles)
# Spawn the first one
self.game.gameloop.summon(Spawner(self.game,self.tiles))
# Demo
self.demo = None
self.demotimer = game.lib.Timer(4)
def step(self):
nblemmings = len(self.game.gameloop.findname("Lemming")) + len(self.game.gameloop.findname("Spawner"))
# Updating score
for i in range(nblemmings):
self.score+=self.scoreratio*self.game.dt
# Spawning more lemmings
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.speedmargin+=self.speedstep
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"] = int(self.score)
self.game.scenes.gameover(self.game)
if self.invincible:
if self.deathtimer.tick(self.game.dt):
self.invincible = False
if not self.demo:
if self.demotimer.tick(self.game.dt):
self.demo = self.game.gameloop.summon(Demo(self.game))
def death(self):
if not self.invincible:
self.lives-=1
self.invincible = True
else:
self.deathtimer.tick(self.deathtimer.getmax()/3)
def draw(self):
pass
def afterdraw(self):
# Draw vignette
self.game.realwindow.blit(self.game.sprite_lib["vignette.png"],[0,0])
# Draw score and lives with outline
txtsurfacescore = self.game.fontfile.render("Score : "+str(int(self.score)),False,[150,255,150])
txtsurfacescoreblack = self.game.fontfile.render("Score : "+str(int(self.score)),False,[0]*3)
txtsurfacelives = self.game.fontfile.render("Lives : "+str(int(self.lives)),False,[255,150,150])
txtsurfacelivesblack = self.game.fontfile.render("Lives : "+str(int(self.lives)),False,[0]*3)
border = 2
for x in range(-border,border+1):
for y in range(-border,border+1):
self.game.realwindow.blit(txtsurfacescoreblack,[20+x,20+y])
self.game.realwindow.blit(txtsurfacescore,[20,20])
for x in range(-border,border+1):
for y in range(-border,border+1):
self.game.realwindow.blit(txtsurfacelivesblack,[20+x,20*2+y+txtsurfacescore.get_height()])
self.game.realwindow.blit(txtsurfacelives,[20,20*2+txtsurfacescore.get_height()])
self.endrect.set_alpha((1-self.endtimer.getratio())*255)
self.game.realwindow.blit(self.endrect,[0,0])