5. Puntos y vidas
Estado del juego
Sección titulada «Estado del juego»Puntos y vidas los usan varios objetos y quedan en un archivo común. Botón derecho en la carpeta src › Novo script…
local game = { score = 0, lives = 3,}
return game#pragma once
struct GameState { int score = 0; int lives = 3;};
GameState& state();#include "game.hpp"
GameState& state() { static GameState current; return current;}En Enemy, crea la variable pontos, de tipo inteiro, con el valor 100. En el código, carga el estado y suma en kill:
local game = require("src.game")
function kill(self) if not self:exists() then return end game.score = game.score + self.pontos self:destroy()end#include "Enemy.hpp"#include "src/game.hpp"
void Enemy::kill() { if (isDestroyed()) return; state().score += pontos; destroy();}En Player: tocar un enemigo quita una vida y da 90 pasos de invencibilidad, en los que la nave parpadea.
local game = require("src.game")
function onSpawn(self) self.animSpeed = 0.2 self.podeAtirar = true self.invencivel = 0 self:onCollide("Enemy", function(enemy) if self:hurt() then enemy:destroy() end end)end
function onUpdate(self) -- ... if self.invencivel > 0 then self.invencivel = self.invencivel - 1 endend
function onRender(self) if self.invencivel > 0 and (self.invencivel // 4) % 2 == 0 then return end self:drawSelf()end
function hurt(self) if self.invencivel > 0 or not self:exists() then return false end game.lives = game.lives - 1 if game.lives <= 0 then self:destroy() else self.invencivel = 90 end return trueend void onRender() override;
bool hurt();
private: bool canShoot_ = true; int invincible_ = 0;#include "Player.hpp"#include "src/game.hpp"
void Player::onSpawn() { animSpeed = 0.2f; onCollide<Enemy>([this](Enemy& enemy) { if (hurt()) enemy.destroy(); });}
void Player::onUpdate() { // ... if (invincible_ > 0) --invincible_;}
void Player::onRender() { if (invincible_ > 0 && (invincible_ / 4) % 2 == 0) return; drawSelf();}
bool Player::hurt() { if (invincible_ > 0 || isDestroyed()) return false; --state().lives; if (state().lives <= 0) { destroy(); } else { invincible_ = 90; } return true;}hurtdevuelvefalsecuando la nave está invencible; solo entonces el enemigo sigue vivo.onRendersustituye el dibujo por defecto: omite algunos pasos para que la nave parpadee y llama adrawSelfen los otros.
Marcador
Sección titulada «Marcador»Crea el objeto Hud, sin sprite, y arrástralo a la room Main.
local game = require("src.game")
function onRenderGui(self) drawText(string.format("PONTOS %05d", game.score), 4, 4) drawText("VIDAS " .. game.lives, 4, 14, colors.yellow)end#pragma once#include <brasa/brasa.hpp>
class Hud : public brasa::Entity {public: BRASA_VARS(Hud)
void onRenderGui() override;};#include "Hud.hpp"#include "src/game.hpp"#include <format>
using namespace brasa;
void Hud::onRenderGui() { drawText(std::format("PONTOS {:05}", state().score), 4.0f, 4.0f); drawText(std::format("VIDAS {}", state().lives), 4.0f, 14.0f, colors::yellow);}
BRASA_ENTITY(Hud);onRenderGui dibuja en coordenadas de pantalla, por encima de todo. Un marcador armado en el editor, sin dibujar por código, está en HUD y menú de pausa.
Play. Cada enemigo vale 100 puntos; tocar uno quita una vida y la nave parpadea. Sin vidas, la nave desaparece.
Siguiente parte: 6. Sonido
