Adding Timer to games.

At the moment I'm working on a knock off pong game and I was wondering how to add a timer in so the players can see how many seconds they've played for? Also just curious as how to add in names as well so the players know which paddle they are.

http://postimg.org/image/ebhv3jqih/

Here's a picture of the game so far, if you need code just ask! :)

Look at the documentation for sf::Clock and sf::Time

http://sfml-dev.org/documentation/2.1/classsf_1_1Clock.php
http://sfml-dev.org/documentation/2.1/classsf_1_1Time.php

So you do something like clock.getElapsedTime().asSeconds()
Then display this value on the screen, you can use a stream to help you.
Look at the void Game::updateStatistics(sf::Time elapsedTime) function in https://github.com/LaurentGomila/SFML-Game-Development-Book/blob/master/01_Intro/Source/Game.cpp to help see how you can make the float value into a string to be displayed on the screen.

See also the StringHelpers files in the Include section, like this function.
1
2
3
4
5
6
7
template <typename T>
std::string toString(const T& value)
{
    std::stringstream stream;
    stream << value;
    return stream.str();
}

If your compiler supports it you should be able to use to_string http://www.cplusplus.com/reference/string/to_string/
Last edited on
Topic archived. No new replies allowed.