add constant time game loop

This commit is contained in:
2025-11-11 17:47:06 -05:00
parent 571d18dd43
commit c01591dd6c
+9 -2
View File
@@ -124,6 +124,9 @@ void Game::run() {
sf::Clock clock;
const sf::Time FIXED_TIMESTEP = sf::seconds(1.0f / 60.606061f);
sf::Time accumulator = sf::Time::Zero;
sf::Time initialGameplayPause;
int score = 0;
@@ -159,11 +162,15 @@ void Game::run() {
}
sf::Time elapsedTime = clock.restart();
accumulator += elapsedTime;
initialGameplayPause += elapsedTime;
sf::Vector2i currentPacmanTile = map.getTileCoords(pacman.getPosition());
while (accumulator >= FIXED_TIMESTEP) {
accumulator -= FIXED_TIMESTEP;
if (initialGameplayPause > sf::seconds(4.5f)) {
sf::Vector2i currentPacmanTile = map.getTileCoords(pacman.getPosition());
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W) || sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Up)) {
pacman.queueDirection(MovementDir::UP);
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A) || sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left)) {
@@ -236,7 +243,7 @@ void Game::run() {
pelletSoundCount++;
}
}
}
window.clear();