Snake Game Chrono Problem

Write your question here.
I'm trying to implement powerups in Snake, for example: when the snake collects the powerup, the speed increases for 10 sec (using Sleep() ). The code repeats this piece each frame so the powerup will stop when 10 sec have passed. I cannot seem to figure out how chrono and the functions work. So, basically, I need to figure out how to test when 10 sec have passed since collecting the powerup, but if not, the program must continue like usual.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  if (headX == speedX && headY == speedY)
         {
            powerupInUse = true;
            auto startTime = std::chrono::steady_clock::now();
            movSpeed /= 2;
            speedX = speedY = 0;

         }

    if (powerupInUse)
    {
        //this part is not working properly
        auto diff = std::chrono::steady_clock::now() - startTime;
        auto t1 = std::chrono::duration_cast<std::chrono::seconds>(diff);

        if (t1.count() > 9 && t1.count() < 11)
        {
            powerupInUse = false;
            movSpeed *= 2;
        }
    }
This program keeps polling in the while loop until four seconds have elapsed, at which point it terminates.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <chrono>

int main() {

	using type_precision = int;
	using type_duration = std::chrono::duration<type_precision>;

	using clock = std::chrono::steady_clock;
	using time_point = clock::time_point;

	const type_precision number_of_seconds = 4;

	time_point point = clock::now();
	type_duration duration = std::chrono::duration_cast<type_duration>(clock::now() - point);
	
	while (duration.count() < number_of_seconds) {
		duration = std::chrono::duration_cast<type_duration>(clock::now() - point);
	}

	return 0;
}
Topic archived. No new replies allowed.