Countdown time

Asked

Viewed 727 times

4

I’m creating a mini-game with several questions and I want you to have time, x until zero. The counter only needs to have seconds and minutes. Does anyone know of any function in C++ or some "gambiarra" to get a countdown?

1 answer

3


The best way to make an accountant in games is by using Threads, so it will be possible to count the time without the interference of Frames of the game (FPS).

void time_decrement(void *game_config){
    GameConfig *gc = (GameConfig*) game_config;

    while(gc->time > 0){
        sleep(1);
        gc->time--;
    }

    gc->game_loop = false;
}

main:

//...
pthread_create(&line,NULL,time_decrement,(void *) g_config);
//...

GameConfig represents a struct or classe that has game settings, (time, pause, end of game, etc). It’s an optional medium, but from my point of view, it’s better than leaving it in separate variables.

In this example the Thread will end the game as soon as the counter reaches 0.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.