Implement score
This commit is contained in:
parent
42ee04203f
commit
fedbc153ae
5 changed files with 36 additions and 7 deletions
|
@ -17,14 +17,14 @@ Canvas::Canvas()
|
|||
game_font = loadFont("joystix.ttf");
|
||||
}
|
||||
|
||||
void Canvas::update(const PacMan & pacMan, const Pellets & pellets, const SuperPellets & superPellets) {
|
||||
void Canvas::update(const PacMan & pacMan, const Pellets & pellets, const SuperPellets & superPellets, const Score & score) {
|
||||
clear();
|
||||
|
||||
renderMaze();
|
||||
renderPellets(pellets);
|
||||
renderSuperPellets(superPellets);
|
||||
renderPacMan(pacMan);
|
||||
renderScore(0);
|
||||
renderScore(score.points);
|
||||
|
||||
render();
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "Position.hpp"
|
||||
#include "Score.hpp"
|
||||
#include <optional>
|
||||
|
||||
class PacMan;
|
||||
|
@ -10,7 +11,7 @@ class SuperPellets;
|
|||
class Canvas {
|
||||
public:
|
||||
Canvas();
|
||||
void update(const PacMan & pacMan, const Pellets & pellets, const SuperPellets & superPellets);
|
||||
void update(const PacMan & pacMan, const Pellets & pellets, const SuperPellets & superPellets, const Score &);
|
||||
std::optional<sf::Event> pollEvent();
|
||||
|
||||
private:
|
||||
|
|
27
lib/Game.cpp
27
lib/Game.cpp
|
@ -2,10 +2,21 @@
|
|||
|
||||
#include <chrono>
|
||||
|
||||
constexpr int DEFAULT_LIVES = 3;
|
||||
constexpr int NORMAL_PELLET_POINTS = 10;
|
||||
constexpr int POWER_PELLET_POINTS = 50;
|
||||
//constexpr int GHOST_POINTS[] = {200, 400, 800, 1600};
|
||||
|
||||
|
||||
|
||||
|
||||
Game::Game()
|
||||
: pacMan(board),
|
||||
pellets(board),
|
||||
superPellets(board) {}
|
||||
superPellets(board)
|
||||
{
|
||||
score.lives = DEFAULT_LIVES;
|
||||
}
|
||||
|
||||
auto Game::now() {
|
||||
return std::chrono::system_clock::now();
|
||||
|
@ -35,14 +46,22 @@ void Game::run() {
|
|||
accumulator -= delta_time;
|
||||
t += delta_time;
|
||||
}
|
||||
canvas.update(pacMan, pellets, superPellets);
|
||||
canvas.update(pacMan, pellets, superPellets, score);
|
||||
}
|
||||
}
|
||||
|
||||
void Game::eatPellets() {
|
||||
const auto pos = pacMan.positionInGrid();
|
||||
pellets.eatPelletAtPosition(pos);
|
||||
superPellets.eatPelletAtPosition(pos);
|
||||
if(pellets.eatPelletAtPosition(pos)) {
|
||||
score.eatenPellets++;
|
||||
score.points += NORMAL_PELLET_POINTS;
|
||||
}
|
||||
|
||||
if(superPellets.eatPelletAtPosition(pos)) {
|
||||
score.eatenPellets++;
|
||||
score.points += POWER_PELLET_POINTS;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void Game::processEvents(InputState & inputState) {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include "Score.hpp"
|
||||
#include "Board.hpp"
|
||||
#include "Canvas.hpp"
|
||||
#include "PacMan.hpp"
|
||||
|
@ -20,6 +21,7 @@ private:
|
|||
PacMan pacMan;
|
||||
Pellets pellets;
|
||||
SuperPellets superPellets;
|
||||
Score score;
|
||||
|
||||
void eatPellets();
|
||||
|
||||
|
|
7
lib/Score.hpp
Normal file
7
lib/Score.hpp
Normal file
|
@ -0,0 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
struct Score {
|
||||
int lives = 0;
|
||||
int points = 0;
|
||||
int eatenPellets = 0;
|
||||
};
|
Loading…
Reference in a new issue