add initial game delay during start

This commit is contained in:
2025-11-10 17:27:45 -05:00
parent 294781b449
commit cb3edad6da
+70 -59
View File
@@ -1,4 +1,5 @@
#include <SFML/Graphics/RenderWindow.hpp> #include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/System/Time.hpp>
#include <SFML/System/Vector2.hpp> #include <SFML/System/Vector2.hpp>
#include <iostream> #include <iostream>
#include <SFML/Audio.hpp> #include <SFML/Audio.hpp>
@@ -109,6 +110,10 @@ void Game::run() {
int pelletSoundCount = 0; int pelletSoundCount = 0;
sf::Clock clock;
sf::Time initialGameplayPause;
while (window.isOpen()) while (window.isOpen())
{ {
while (const std::optional event = window.pollEvent()) while (const std::optional event = window.pollEvent())
@@ -119,77 +124,83 @@ void Game::run() {
} }
} }
sf::Time elapsedTime = clock.restart();
initialGameplayPause += elapsedTime;
sf::Vector2i currentPacmanTile = map.getTileCoords(pacman.getPosition()); sf::Vector2i currentPacmanTile = map.getTileCoords(pacman.getPosition());
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W) || sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Up)) { if (initialGameplayPause > sf::seconds(4.5f)) {
pacman.queueDirection(MovementDir::UP); if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W) || sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Up)) {
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A) || sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left)) { pacman.queueDirection(MovementDir::UP);
pacman.queueDirection(MovementDir::LEFT); } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A) || sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left)) {
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S)|| sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Down)) { pacman.queueDirection(MovementDir::LEFT);
pacman.queueDirection(MovementDir::DOWN); } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S)|| sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Down)) {
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D) || sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right)) { pacman.queueDirection(MovementDir::DOWN);
pacman.queueDirection(MovementDir::RIGHT); } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D) || sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right)) {
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Q)) { pacman.queueDirection(MovementDir::RIGHT);
pacman.queueDirection(MovementDir::STATIC); } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Q)) {
} pacman.queueDirection(MovementDir::STATIC);
if (!pacman.isCurrentlyMoving()) {
MovementDir nextDir = MovementDir::STATIC;
MovementDir queued = pacman.getQueuedDirection();
if (queued != MovementDir::STATIC && map.entityCanMove(pacman, queued)) {
nextDir = queued;
pacman.clearQueuedDirection();
}
else if (pacman.getCurrentDirection() != MovementDir::STATIC &&
map.entityCanMove(pacman, pacman.getCurrentDirection())) {
nextDir = pacman.getCurrentDirection();
} }
if (nextDir != MovementDir::STATIC) { if (!pacman.isCurrentlyMoving()) {
sf::Vector2i currentTile = map.getTileCoords(pacman.getPosition()); MovementDir nextDir = MovementDir::STATIC;
sf::Vector2i targetTile = currentTile;
switch (nextDir) { MovementDir queued = pacman.getQueuedDirection();
case MovementDir::UP: if (queued != MovementDir::STATIC && map.entityCanMove(pacman, queued)) {
targetTile.y -= 1; nextDir = queued;
pacman.setActiveSprite("up_walking", 0); pacman.clearQueuedDirection();
break; }
case MovementDir::DOWN: else if (pacman.getCurrentDirection() != MovementDir::STATIC &&
targetTile.y += 1; map.entityCanMove(pacman, pacman.getCurrentDirection())) {
pacman.setActiveSprite("down_walking", 0); nextDir = pacman.getCurrentDirection();
break;
case MovementDir::LEFT:
targetTile.x -= 1;
pacman.setActiveSprite("left_walking", 0);
break;
case MovementDir::RIGHT:
targetTile.x += 1;
pacman.setActiveSprite("right_walking", 0);
break;
case MovementDir::STATIC:
break;
} }
sf::Vector2f targetCenter = map.getTargetTileCenter(targetTile); if (nextDir != MovementDir::STATIC) {
pacman.startMove(nextDir, targetCenter); sf::Vector2i currentTile = map.getTileCoords(pacman.getPosition());
} else { sf::Vector2i targetTile = currentTile;
pacman.setActiveSprite("static", 0);
}
}
pacman.update(); switch (nextDir) {
case MovementDir::UP:
targetTile.y -= 1;
pacman.setActiveSprite("up_walking", 0);
break;
case MovementDir::DOWN:
targetTile.y += 1;
pacman.setActiveSprite("down_walking", 0);
break;
case MovementDir::LEFT:
targetTile.x -= 1;
pacman.setActiveSprite("left_walking", 0);
break;
case MovementDir::RIGHT:
targetTile.x += 1;
pacman.setActiveSprite("right_walking", 0);
break;
case MovementDir::STATIC:
break;
}
if (map.hasPellet(currentPacmanTile.x, currentPacmanTile.y)) { sf::Vector2f targetCenter = map.getTargetTileCenter(targetTile);
map.eatPellet(currentPacmanTile.x, currentPacmanTile.y); pacman.startMove(nextDir, targetCenter);
} else {
pacman.setActiveSprite("static", 0);
}
}
if (pelletSoundCount % 2) { pacman.update();
pellet0.play();
} else { if (map.hasPellet(currentPacmanTile.x, currentPacmanTile.y)) {
pellet1.play(); map.eatPellet(currentPacmanTile.x, currentPacmanTile.y);
if (pelletSoundCount % 2) {
pellet1.play();
} else {
pellet0.play();
}
pelletSoundCount++;
} }
pelletSoundCount++;
} }
window.clear(); window.clear();