7. Fin del juego
En el editor
Sección titulada «En el editor»-
Botón derecho en la carpeta
rooms› Nova room…, con el nombreGameOver. -
Crea el objeto
GameOverScreen, sin sprite, y arrástralo a la roomGameOver. -
En Ações de entrada, crea
confirmar(Enter,Pad:Start) ysair(Escape, elegido en la lista Tecla, yPad:Back).
Funciones del juego
Sección titulada «Funciones del juego»En el archivo del estado, una función que centra texto y otra que reinicia el juego:
function game.centeredX(text, screenWidth) return (screenWidth - #text * 8) / 2end
function game.newGame() game.score = 0 game.lives = 3 goTo("Main")end
return game#pragma once#include <string_view>
struct GameState { int score = 0; int lives = 3;};
GameState& state();
float centeredX(std::string_view text, float screenWidth);
void newGame();#include "game.hpp"#include <brasa/brasa.hpp>
GameState& state() { static GameState current; return current;}
float centeredX(std::string_view text, float screenWidth) { return (screenWidth - static_cast<float>(text.size()) * 8.0f) / 2.0f;}
void newGame() { state() = GameState{}; brasa::goToScene("Main");}Ir al fin del juego
Sección titulada «Ir al fin del juego»En Player, en hurt, cuando se acaban las vidas:
if game.lives <= 0 then goTo("GameOver") self:destroy() if (state().lives <= 0) { goTo("GameOver"); destroy();La pantalla
Sección titulada «La pantalla»local game = require("src.game")
function onUpdate(self) if pressed("confirmar") then game.newGame() end if pressed("sair") then quit() endend
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#pragma once#include <brasa/brasa.hpp>
class GameOverScreen : public brasa::Entity {public: BRASA_VARS(GameOverScreen)
void onUpdate() override; void onRenderGui() override;};#include "GameOverScreen.hpp"#include "src/game.hpp"#include <format>#include <string>#include <string_view>
using namespace brasa;
void GameOverScreen::onUpdate() { if (pressed("confirmar")) newGame(); if (pressed("sair")) quit();}
void GameOverScreen::onRenderGui() { const float screenW = static_cast<float>(roomWidth()); constexpr std::string_view kTitle = "FIM DE JOGO"; constexpr std::string_view kRetry = "ENTER JOGAR DE NOVO"; constexpr std::string_view kQuit = "ESC SAIR"; const std::string score = std::format("PONTOS {}", state().score); drawText(kTitle, centeredX(kTitle, screenW), 60.0f, colors::red); drawText(score, centeredX(score, screenW), 85.0f); drawText(kRetry, centeredX(kRetry, screenW), 120.0f); drawText(kQuit, centeredX(kQuit, screenW), 135.0f, Color{150, 150, 150, 255});}
BRASA_ENTITY(GameOverScreen);pressedvale solo en el paso en que se pulsó la acción: un toque, una acción.goTocambia 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.
Después
Sección titulada «Después»- Exportar el juego a un ejecutable.
- Cambiar el marcador dibujado por una interfaz armada en el editor.
- Una room más grande con la cámara siguiendo la nave.
- Las plantillas Plataforma y Zumbis muestran tiles, interfaz, tienda y oleadas de enemigos.
- Muestra el resultado en Discord.
