PinmikPanik/gamedata/objects/gameover.py

152 lines
6.4 KiB
Python
Raw Permalink Normal View History

2021-09-13 19:13:54 +02:00
from gamedata.objects.base import BaseObject
from gamedata.objects.button import Button
2021-09-19 16:58:39 +02:00
import random
2021-09-13 19:13:54 +02:00
class GameOver(BaseObject):
def __init__(self,game):
super().__init__(0,0,game)
# Back button function
def fnBack(self,game):
game.scene = game.scenes.main
2021-09-19 22:12:52 +02:00
self.game.sound_lib["bgm/gameover.ogg"].stop()
self.game.sound_lib["bgm/menu.ogg"].play(-1)
2021-09-13 19:13:54 +02:00
# 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)
2021-09-19 21:32:42 +02:00
self.sfxscroll = game.sound_lib["sfx/scorescroll.wav"]
self.sfxscore = game.sound_lib["sfx/score.wav"]
self.sfxtimer = game.lib.Timer(1)
2021-09-19 16:58:39 +02:00
self.depth = -1
2021-09-13 19:13:54 +02:00
# Updating highscore
self.highscore = False
self.newunlocks = []
2021-09-13 19:13:54 +02:00
if game.globals["highscore"]<game.globals["score"]:
self.pastunlocks = game.lib.getunlocks(game.globals["highscore"])
self.currentunlocks = game.lib.getunlocks(game.globals["score"])
2021-09-13 19:13:54 +02:00
game.globals["highscore"] = game.globals["score"]
self.highscore = True
2021-09-17 11:03:32 +02:00
self.game.lib.savescore(self.game.datadir,self.game.globals["highscore"],self.game.dataname)
# Get if there's an unlock
self.newunlocks = []
self.unlocksprites = {}
for unlocktype in ["normal","specials"]:
for unlock in self.currentunlocks[unlocktype]:
if not unlock in self.pastunlocks[unlocktype]:
self.newunlocks.append(unlock)
sprite = game.sprite_lib["lemmings/"+unlock+"/falling.png"]
self.unlocksprites[unlock] = game.pygame.transform.scale(sprite,[sprite.get_width()*3,sprite.get_height()*3])
2021-09-16 09:11:55 +02:00
self.displayscore = 0
self.launched = False
self.launchtimer = game.lib.Timer(0.5)
self.color = [255]*3
self.scale = 1
2021-09-16 09:33:38 +02:00
# Highscore texts
text = "New Highscore !"
if len(self.newunlocks)>0:
text+=" You unlocked :"
self.highscorewhite = game.fontfilebig.render(text,False,[255]*3)
self.highscoreflash = game.fontfilebig.render(text,False,[255,230,55])
2021-09-16 09:33:38 +02:00
self.flashtimer = game.lib.Timer(0.4)
2021-09-16 09:11:55 +02:00
def step(self):
2021-09-19 21:32:42 +02:00
update_value = (self.game.globals["score"]-self.displayscore)*self.game.dt*0.4+self.game.dt*2
self.displayscore+= update_value
2021-09-16 09:11:55 +02:00
self.displayscore = min(self.game.globals["score"],self.displayscore)
2021-09-16 09:33:38 +02:00
colorvalue = self.displayscore*0.9
2021-09-16 09:11:55 +02:00
self.scale = min(2,colorvalue**0.9/250+1)
if colorvalue>255: # Green to red
colorvalue-=255
2021-09-16 09:33:38 +02:00
colorvalue = min(255,colorvalue**0.95)
2021-09-16 09:11:55 +02:00
self.color = [colorvalue,255-colorvalue,0]
else: # White to green
self.color = [255-colorvalue,255,255-colorvalue]
2021-09-19 21:32:42 +02:00
# Scroll sfx
if int(self.displayscore)!=int(self.game.globals["score"]):
if self.sfxtimer.tick(min(update_value,0.5)):
self.sfxscroll.play()
2021-09-16 09:11:55 +02:00
if not self.launched and int(self.displayscore)==int(self.game.globals["score"]):
2021-09-19 21:32:42 +02:00
2021-09-16 09:11:55 +02:00
if self.launchtimer.tick(self.game.dt):
# Launch particles
2021-09-16 09:33:38 +02:00
self.launched = True
2021-09-19 16:58:39 +02:00
if self.highscore: # Balloons
offy = self.game.DISPLAY_HEIGHT+40
for i in range(2):
for direction in [-1,1]:
for color in ["yellow","red","blue"]:
sprite = [self.game.sprite_lib["particles/balloons/"+color+".png"]]
offx = self.game.DISPLAY_WIDTH/2-direction*self.game.DISPLAY_WIDTH/2*1.2
velx = (random.random()*3+1+2*i)*direction/2
vely = (random.random()+2)*-1
self.game.addParticle(sprite,offx,offy,velx=velx,vely=vely,flipx=direction==-1,fps=0.3)
# Confettis
for j in range(30):
for i in range(8):
color = random.choice(["red","blue","yellow"])
sprites = self.game.getSpriteDir("particles/confetti/"+color+"/")
offx = random.randint(0,self.game.DISPLAY_WIDTH)
offy = -40*i
velx = random.random()*2-1
vely = random.random()*5+2
self.game.addParticle(sprites,offx,offy,velx=velx,vely=vely,flipx=random.randint(0,1),fps=2)
2021-09-19 21:32:42 +02:00
self.sfxscore.play()
2021-09-19 22:12:52 +02:00
self.game.sound_lib["bgm/gameover.ogg"].play(-1)
2021-09-16 09:33:38 +02:00
if self.launched:
self.flashtimer.tick(self.game.dt)
2021-09-16 09:11:55 +02:00
if self.game.inputs["mouse"]["click"]==1:
# skip animation
self.displayscore = int(self.game.globals["score"])
2021-09-13 19:13:54 +02:00
def draw(self):
2021-09-19 16:58:39 +02:00
# Display night sky
self.game.window.blit(self.game.pygame.transform.scale(self.game.sprite_lib["skies/night.png"],[self.game.DISPLAY_WIDTH,self.game.DISPLAY_HEIGHT]),[0,0])
self.game.window.blit(self.game.sprite_lib["vignette.png"],[0,0])
2021-09-16 09:33:38 +02:00
# Display score
2021-09-16 09:11:55 +02:00
txt = self.game.fontfilebig.render("Score : "+str(int(self.displayscore)),False,self.color)
txt = self.game.pygame.transform.scale(txt,(round(txt.get_width()*self.scale),round(txt.get_height()*self.scale)))
self.game.lib.drawcenter(self.game,txt,self.game.DISPLAY_WIDTH/2,self.game.DISPLAY_HEIGHT*0.4)
2021-09-16 09:33:38 +02:00
# Display new highscore
if self.highscore:
if not self.launched:
alpha = (1-self.launchtimer.getratio())**2*255
2021-09-16 09:33:38 +02:00
else:
alpha = 255
txt = [self.highscorewhite,self.highscoreflash][self.flashtimer.getloops()%2]
txt.set_alpha(alpha)
2021-09-16 09:33:38 +02:00
self.game.lib.drawcenter(self.game,txt,self.game.DISPLAY_WIDTH/2,self.game.DISPLAY_HEIGHT*0.6)
# Draw unlocks
for i in range(len(self.newunlocks)):
posx = self.game.DISPLAY_WIDTH/(len(self.newunlocks)+1)*(i+1)
posy = self.game.DISPLAY_HEIGHT*0.7
sprite = self.unlocksprites[self.newunlocks[i]]
sprite.set_alpha(alpha)
self.game.lib.drawcenter(self.game,sprite,posx,posy)
2021-09-16 09:33:38 +02:00