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;
|
sf::Clock clock;
|
||||||
|
|
||||||
|
const sf::Time FIXED_TIMESTEP = sf::seconds(1.0f / 60.606061f);
|
||||||
|
sf::Time accumulator = sf::Time::Zero;
|
||||||
|
|
||||||
sf::Time initialGameplayPause;
|
sf::Time initialGameplayPause;
|
||||||
|
|
||||||
int score = 0;
|
int score = 0;
|
||||||
@@ -159,11 +162,15 @@ void Game::run() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sf::Time elapsedTime = clock.restart();
|
sf::Time elapsedTime = clock.restart();
|
||||||
|
accumulator += elapsedTime;
|
||||||
initialGameplayPause += elapsedTime;
|
initialGameplayPause += elapsedTime;
|
||||||
|
|
||||||
sf::Vector2i currentPacmanTile = map.getTileCoords(pacman.getPosition());
|
while (accumulator >= FIXED_TIMESTEP) {
|
||||||
|
accumulator -= FIXED_TIMESTEP;
|
||||||
|
|
||||||
if (initialGameplayPause > sf::seconds(4.5f)) {
|
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)) {
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W) || sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Up)) {
|
||||||
pacman.queueDirection(MovementDir::UP);
|
pacman.queueDirection(MovementDir::UP);
|
||||||
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A) || sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left)) {
|
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A) || sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left)) {
|
||||||
@@ -236,7 +243,7 @@ void Game::run() {
|
|||||||
|
|
||||||
pelletSoundCount++;
|
pelletSoundCount++;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
window.clear();
|
window.clear();
|
||||||
|
|||||||
Reference in New Issue
Block a user