7. Game over
In the editor
Section titled “In the editor”-
Right-click the
roomsfolder › New room…, namedGameOver. -
Create the object
GameOverScreen, with no sprite, and drag it into the roomGameOver. -
Under Input actions, create
confirmar(Enter,Pad:Start) andsair(Escape, picked from the Key list, andPad:Back).
Game functions
Section titled “Game functions”In the state file, one function that centers text and another that restarts the game:
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");}Go to game over
Section titled “Go to game over”On Player, in hurt, when the lives run out:
if game.lives <= 0 then goTo("GameOver") self:destroy() if (state().lives <= 0) { goTo("GameOver"); destroy();The screen
Section titled “The screen”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);pressedis true only on the step the action was pressed: one press, one action.goToswitches rooms at the end of the frame; the instances of the current room are destroyed.- C++: outside an object, the room switch is
brasa::goToScene.
Play. After losing the three lives, the room GameOver shows the score. Enter restarts; Esc quits.
- Export the game to an executable.
- Replace the drawn score with an interface built in the editor.
- A larger room with the camera following the ship.
- The Platformer and Zombies templates show tiles, interface, shop and enemy waves.
- Show the result on Discord.
