Add zombo hp bar

This commit is contained in:
Reimar 2025-11-05 12:07:16 +01:00
parent 7cc2b4c64f
commit 9b15ebc1a5
5 changed files with 22 additions and 3 deletions

View File

@ -55,9 +55,12 @@ void Game::update(std::stop_token stop_token)
arrow.x > zombo.x && arrow.x < zombo.x + zombo_sprite->width && arrow.x > zombo.x && arrow.x < zombo.x + zombo_sprite->width &&
arrow.y > zombo.y && arrow.y < zombo.y + zombo_sprite->height arrow.y > zombo.y && arrow.y < zombo.y + zombo_sprite->height
) { ) {
zombo.hp--;
zombo.x += std::cos(arrow.angle) * 8;
zombo.y += std::sin(arrow.angle) * 8;
std::erase(arrows, arrow); std::erase(arrows, arrow);
zombo.hp--;
if (zombo.hp == 0) { if (zombo.hp == 0) {
std::erase(zombos, zombo); std::erase(zombos, zombo);
player.gold += 10; player.gold += 10;
@ -159,7 +162,7 @@ void Game::run()
if (e.type == SDL_MOUSEBUTTONDOWN && e.button.button == 1 && player.shooting_cooldown == 0) { if (e.type == SDL_MOUSEBUTTONDOWN && e.button.button == 1 && player.shooting_cooldown == 0) {
arrows.emplace_back(player.x, player.y, player.angle); arrows.emplace_back(player.x, player.y, player.angle);
player.shooting_cooldown = 40; player.shooting_cooldown = 20;
} }
int mouse_x, mouse_y; int mouse_x, mouse_y;

View File

@ -89,6 +89,14 @@ void GameRenderer::draw_sprite_rotated(const Sprite &sprite, const int x, const
SDL_RenderCopyEx(renderer, sprite.texture, nullptr, &rect, angle, nullptr, SDL_FLIP_NONE); SDL_RenderCopyEx(renderer, sprite.texture, nullptr, &rect, angle, nullptr, SDL_FLIP_NONE);
} }
void GameRenderer::draw_rect(int x, int y, int width, int height, Color color) const
{
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
const SDL_Rect rect = { .x = x, .y = y, .w = width, .h = height };
SDL_RenderFillRect(renderer, &rect);
}
void GameRenderer::clear_screen(Color color) const void GameRenderer::clear_screen(Color color) const
{ {
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a); SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);

View File

@ -40,6 +40,8 @@ public:
void draw_sprite_rotated(const Sprite &sprite, int x, int y, double angle) const; void draw_sprite_rotated(const Sprite &sprite, int x, int y, double angle) const;
void draw_rect(int x, int y, int width, int height, Color color) const;
void clear_screen(Color color) const; void clear_screen(Color color) const;
void flush() const; void flush() const;

View File

@ -33,4 +33,9 @@ void Zombo::draw(const GameRenderer *renderer, const Sprite &sprite, double play
renderer->draw_sprite(sprite, draw_x, draw_y); renderer->draw_sprite(sprite, draw_x, draw_y);
else else
renderer->draw_sprite_flipped(sprite, draw_x, draw_y); renderer->draw_sprite_flipped(sprite, draw_x, draw_y);
if (hp < max_hp) {
renderer->draw_rect(draw_x, draw_y - 10, sprite.width, 5, rgb(0, 0, 0));
renderer->draw_rect(draw_x, draw_y - 10, hp * sprite.width / max_hp, 5, rgb(255, 0, 0));
}
} }

View File

@ -9,8 +9,9 @@ class Zombo
public: public:
double x; double x;
double y; double y;
int hp = 3; int hp = max_hp;
static constexpr int max_hp = 3;
static constexpr double speed = 0.5; static constexpr double speed = 0.5;
Zombo(const GameRenderer *renderer, const Sprite &sprite, double player_x, double player_y); Zombo(const GameRenderer *renderer, const Sprite &sprite, double player_x, double player_y);