How do I make sure that my game runs at the same speed on all computers? The problem occurred when i transferred my game project from my old laptop to my new one. The game runs too fast and is unplayable.
Game background: Its a small top-down car game with three ai's trying to shoot you down. Game ends when a bullet collides.
If any more information is required I'll be more than happy to share.
int main()
{
sf::Clock clock;
bool end_game= false;
initialize_cars();
initialize_bullets();
initialize_explosion_sprite();
while (window.isOpen())
{
sf::Event event;
if (window.pollEvent(event))
{
if (event.type== sf::Event::Closed || (event.type== sf::Event::KeyReleased) && (event.key.code== sf::Keyboard::Escape))
window.close();
}
move_cars();
set_bullet_timer(&clock);
move_bullets();
end_game= bullet_collision(&clock, &black_car, &blue_bullet);
if(end_game== true)
break;
end_game= bullet_collision(&clock, &black_car, &red_bullet);
if(end_game== true)
break;
for(int i=0; i<2; i++)
{
end_game= bullet_collision(&clock, &black_car, &green_bullet[i]);
if(end_game== true)
break;
}
if(end_game== true)
break;
window.clear(sf::Color::White);
display_cars();
display_bullets();
window.display();
}
return 0;
}
You can store clock.restart().asSeconds();
to a float and pass this to your bullet timer.
multiply your speed(per seconds) * the float(e.g. 0,04). So you can be sure that you move exactly speed/second.
Something else.. You don't need the end_game variable at all and you should make you window-close if-statement smaller.
you can write instead:
//1. There's no need of keyReleased
if(event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)
window.close();
//2. The less variables you have to initialize above the while, the better people can read your code
if(bullet_collision(...))
break;