mirror of
https://github.com/dylanbenzi/pacmen-sfml.git
synced 2026-11-27 00:17:01 +00:00
add pacman life indicators
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
#include <iostream>
|
||||
#include <SFML/Audio.hpp>
|
||||
#include <string>
|
||||
|
||||
#include "Game.h"
|
||||
#include "MazeMap.h"
|
||||
@@ -22,6 +23,8 @@ void Game::run() {
|
||||
// Pellet data: 0=none, 1=pellet, 2=power
|
||||
resources.loadMap("pelletMap", "assets/game/pellets.txt");
|
||||
|
||||
resources.loadFont("bitFont", "assets/fonts/PressStart2P.ttf");
|
||||
|
||||
resources.loadSound("credit", "assets/sounds/credit.wav");
|
||||
resources.loadSound("death_0", "assets/sounds/death_0.wav");
|
||||
resources.loadSound("death_1", "assets/sounds/death_1.wav");
|
||||
@@ -57,6 +60,7 @@ void Game::run() {
|
||||
{912, 0},
|
||||
{0, 0});
|
||||
|
||||
|
||||
Pacman pacman;
|
||||
|
||||
Ghost blinky;
|
||||
@@ -114,6 +118,20 @@ void Game::run() {
|
||||
|
||||
sf::Time initialGameplayPause;
|
||||
|
||||
int score = 0;
|
||||
sf::Font bitFont = resources.getFont("bitFont");
|
||||
sf::Text scoreText(bitFont);
|
||||
scoreText.setString(std::to_string(score));
|
||||
scoreText.setPosition({16, 32 * 31 + 16});
|
||||
|
||||
sf::Sprite pacmanLifeOne(resources.getTexture("all_textures"), {{2340, 68}, {52, 52}});
|
||||
pacmanLifeOne.setOrigin({52, 0});
|
||||
pacmanLifeOne.setPosition({32 * 28, 32 * 31});
|
||||
|
||||
sf::Sprite pacmanLifeTwo(resources.getTexture("all_textures"), {{2340, 68}, {52, 52}});
|
||||
pacmanLifeTwo.setOrigin({52, 0});
|
||||
pacmanLifeTwo.setPosition({30 * 28, 32 * 31});
|
||||
|
||||
while (window.isOpen())
|
||||
{
|
||||
while (const std::optional event = window.pollEvent())
|
||||
@@ -191,6 +209,8 @@ void Game::run() {
|
||||
|
||||
if (map.hasPellet(currentPacmanTile.x, currentPacmanTile.y)) {
|
||||
map.eatPellet(currentPacmanTile.x, currentPacmanTile.y);
|
||||
score += 10;
|
||||
scoreText.setString(std::to_string(score));
|
||||
|
||||
if (pelletSoundCount % 2) {
|
||||
pellet1.play();
|
||||
@@ -204,8 +224,13 @@ void Game::run() {
|
||||
}
|
||||
|
||||
window.clear();
|
||||
|
||||
window.draw(map);
|
||||
window.draw(pacman);
|
||||
window.draw(scoreText);
|
||||
window.draw(pacmanLifeOne);
|
||||
window.draw(pacmanLifeTwo);
|
||||
|
||||
window.display();
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <SFML/Audio/SoundBuffer.hpp>
|
||||
#include <SFML/Graphics/Font.hpp>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <filesystem>
|
||||
#include <memory>
|
||||
@@ -43,6 +44,10 @@ public:
|
||||
return &sounds[soundName];
|
||||
};
|
||||
|
||||
sf::Font& getFont(const std::string& fontName) {
|
||||
return fonts[fontName];
|
||||
};
|
||||
|
||||
//load gameplay info
|
||||
|
||||
private:
|
||||
|
||||
Reference in New Issue
Block a user