Ir al contenido
Descargar

7. Fin del juego

  1. Botón derecho en la carpeta rooms › Nova room…, con el nombre GameOver.

  2. Crea el objeto GameOverScreen, sin sprite, y arrástralo a la room GameOver.

  3. En Ações de entrada, crea confirmar (Enter, Pad:Start) y sair (Escape, elegido en la lista Tecla, y Pad:Back).

En el archivo del estado, una función que centra texto y otra que reinicia el juego:

src/game.lua
function game.centeredX(text, screenWidth)
return (screenWidth - #text * 8) / 2
end
function game.newGame()
game.score = 0
game.lives = 3
goTo("Main")
end
return game

En Player, en hurt, cuando se acaban las vidas:

objects/Player.lua
if game.lives <= 0 then
goTo("GameOver")
self:destroy()
objects/GameOverScreen.lua
local game = require("src.game")
function onUpdate(self)
if pressed("confirmar") then game.newGame() end
if pressed("sair") then quit() end
end
function onRenderGui(self)
local tela = roomWidth()
local titulo = "FIM DE JOGO"
local pontos = "PONTOS " .. game.score
local denovo = "ENTER JOGAR DE NOVO"
local sair = "ESC SAIR"
drawText(titulo, game.centeredX(titulo, tela), 60, colors.red)
drawText(pontos, game.centeredX(pontos, tela), 85)
drawText(denovo, game.centeredX(denovo, tela), 120)
drawText(sair, game.centeredX(sair, tela), 135, Color(150, 150, 150))
end
  • pressed vale solo en el paso en que se pulsó la acción: un toque, una acción.
  • goTo cambia de room al final del cuadro; las instancias de la room actual se destruyen.
  • C++: fuera de un objeto, el cambio de room es brasa::goToScene.

Play. Al perder las tres vidas, la room GameOver muestra la puntuación. Enter reinicia; Esc cierra.