mirror of
https://github.com/dylanbenzi/pacmen-sfml.git
synced 2026-11-27 08:26:59 +00:00
add constant time game loop
This commit is contained in:
+9
-2
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user