Added game over screen

This commit is contained in:
theo@manjaro 2021-09-13 19:13:54 +02:00
parent e2be9da9bb
commit 9591e73e92
4 changed files with 53 additions and 6 deletions

View File

@ -49,6 +49,7 @@ class Game():
self.globals["scamerax"] = 3
self.globals["scameray"] = 0
self.globals["debug"] = True
self.globals["highscore"] = 0
self.scaleCamera()
settings = {"sfx":1,"bgm":1}

View File

@ -0,0 +1,25 @@
from gamedata.objects.base import BaseObject
from gamedata.objects.button import Button
class GameOver(BaseObject):
def __init__(self,game):
super().__init__(0,0,game)
# Back button function
def fnBack(self,game):
game.scene = game.scenes.main
# Creating the back button
btn = Button(int(game.DISPLAY_WIDTH/4),int(game.DISPLAY_HEIGHT*0.8),game,game.DISPLAY_WIDTH//2,int(game.DISPLAY_HEIGHT*0.1))
btn.text = "Back"
btn.click = fnBack
game.gameloop.summon(btn)
# Updating highscore
self.highscore = False
if game.globals["highscore"]<game.globals["score"]:
game.globals["highscore"] = game.globals["score"]
self.highscore = True
def draw(self):
# Display score and highscore
pass

View File

@ -13,25 +13,35 @@ class Manager(BaseObject):
self.stepmargin = 2
self.spawntimer = game.lib.Timer(self.basetime) # Time elapsing each lemming spawn
self.deathtimer = game.lib.Timer(1) # Time betwin 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)
# Summon the tiles
self.tiles = Tiles(50,50,game)
game.gameloop.summon(self.tiles)
# Spawn the first one
self.game.gameloop.summon(Spawner(self.game,self.tiles))
def step(self):
nblemmings = len(self.game.gameloop.findname("Lemming"))
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 and 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)
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)
if self.invincible:
if self.deathtimer.tick(self.game.dt):
@ -49,7 +59,12 @@ class Manager(BaseObject):
def afterdraw(self):
txtsurfacescore = self.game.fontfile.render("Score : "+str(int(self.score)),False,[150,255,150])
txtsurfacelives = self.game.fontfile.render("Lives : "+str(int(self.lives)),False,[255,150,150])
txtsurfacelives = self.game.fontfile.render("Lives : "+str(int(max(0,self.lives))),False,[255,150,150])
self.game.realwindow.blit(txtsurfacescore,[20,20])
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])

View File

@ -6,6 +6,7 @@ from gamedata.objects.sliders.bgmslider import BGMSlider
from gamedata.objects.sliders.sfxslider import SFXSlider
from gamedata.objects.ingame.lemmings import Lemming
from gamedata.objects.ingame.manager import Manager
from gamedata.objects.gameover import GameOver
def main(game):
game.scaleCamera()
@ -28,3 +29,8 @@ def options(game):
menu = OptionMenu(round(game.DISPLAY_WIDTH/8),round(game.DISPLAY_HEIGHT*9/10),game,round(game.DISPLAY_WIDTH*6/8),round(game.DISPLAY_HEIGHT/10-game.DISPLAY_HEIGHT/30))
game.gameloop.summon(s)
game.gameloop.summon(s2)
def gameover(game):
game.gameloop.reinit()
go = GameOver(game)
game.gameloop.summon(go)