5. Pontos e vidas
Estado do jogo
Seção intitulada “Estado do jogo”Pontos e vidas são usados por vários objetos e ficam num arquivo comum. Botão direito na pasta 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;}No Enemy, crie a variável pontos, do tipo inteiro, com o valor 100. No código, carregue o estado e some em 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();}No Player: encostar num inimigo tira uma vida e dá 90 passos de invencibilidade, em que a nave pisca.
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;}hurtdevolvefalsequando a nave está invencível; só então o inimigo continua vivo.onRendersubstitui o desenho padrão: pula alguns passos para a nave piscar e chamadrawSelfnos outros.
Crie o objeto Hud, sem sprite, e arraste-o para a 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 desenha em coordenadas da tela, por cima de tudo. Um placar montado no editor, sem desenhar pelo código, está em HUD e menu de pausa.
Play. Cada inimigo vale 100 pontos; encostar num deles tira uma vida e a nave pisca. Sem vidas, a nave some.
Próxima parte: 6. Som
