7. Fim de jogo
No editor
Seção intitulada “No editor”-
Botão direito na pasta
rooms› Nova room…, com o nomeGameOver. -
Crie o objeto
GameOverScreen, sem sprite, e arraste-o para a roomGameOver. -
Em Ações de entrada, crie
confirmar(Enter,Pad:Start) esair(Escape, escolhido na lista Tecla, ePad:Back).
Funções do jogo
Seção intitulada “Funções do jogo”No arquivo do estado, uma função que centraliza texto e outra que recomeça o jogo:
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 para o fim de jogo
Seção intitulada “Ir para o fim de jogo”No Player, em hurt, quando as vidas acabam:
if game.lives <= 0 then goTo("GameOver") self:destroy() if (state().lives <= 0) { goTo("GameOver"); destroy();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 só no passo em que a ação foi apertada: um toque, uma ação.goTotroca de room no fim do quadro; as instâncias da room atual são destruídas.- C++: fora de um objeto, a troca de room é
brasa::goToScene.
Play. Ao perder as três vidas, a room GameOver mostra a pontuação. Enter recomeça; Esc fecha.
- Exportar o jogo para um executável.
- Trocar o placar desenhado por uma interface montada no editor.
- Uma room maior com a câmera seguindo a nave.
- Os modelos Plataforma e Zumbis mostram tiles, interface, loja e ondas de inimigos.
- Mostre o resultado no Discord.
