1

so I've just made a simple snake game in WinForms and I've only got one problem left. The idea is that I'm gonna implement a power-up that speeds up the player for 10 seconds. I've tried using thread. sleep method to create a timer but that sleeps the entire program. I've also tried playing with the task.delay method but without success.

Am I going about this problem the wrong way or am I just missing something? Do you have any tips to get me on the right path? I'm quite new to this so this might be an easy fix.

    var watch = new Stopwatch();
    watch.Start();

    using (var task = Task.Delay(10000))
    {
        speedUp = true;
        task.Wait();
    }

    watch.Stop();
    speedUp = false;

This is my attempt at the delay. But it seems to pause the entire program for 10 seconds before it continues.

Thanks

mjwills
  • 23,389
  • 6
  • 40
  • 63

1 Answers1

2

You may want to check the past time by looking at a clock within your game loop. The property Environment.TickCount provides you with the milliseconds since the last boot or you could do it with DateTime or Timespan. Now instead of sleeping you could check if 10'000 milliseconds have past since the activation of the power-up. This way nothing will get blocked and collecting multiple power-ups within 10 seconds won't screw up the deactivation process.

Mr.Yellow
  • 692
  • 5
  • 15