5. Score and lives
Game state
Section titled “Game state”Score and lives are used by several objects and live in a shared file. Right-click the src folder › New 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;}On Enemy, create the variable pontos, of type integer, with the value 100. In code, load the state and add in 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();}On Player: touching an enemy takes a life and gives 90 steps of invincibility, during which the ship blinks.
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;}hurtreturnsfalsewhile the ship is invincible; only then does the enemy stay alive.onRenderreplaces the default drawing: it skips some steps so the ship blinks and callsdrawSelfon the others.
Score display
Section titled “Score display”Create the object Hud, with no sprite, and drag it into the 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 draws in screen coordinates, on top of everything. A score display built in the editor, with no drawing code, is in HUD and pause menu.
Play. Each enemy is worth 100 points; touching one takes a life and the ship blinks. With no lives left, the ship disappears.
Next part: 6. Sound
