-1

I want to make a game and I'd like to put a timer in it while it's running, a way in which the player can enter the desired duration and it would end the program at the end of that time. How can I fill the while condition in the following code, to achieve this?

ConsoleKeyInfo info;
jatek j = new jatek();
do
{
    Console.Clear();
    Console.WriteLine(j.Jatekter());
    info = Console.ReadKey(true);
    switch (info.Key)
    {
        case ConsoleKey.UpArrow: j.Mozgas(-1, 0); break;
        case ConsoleKey.DownArrow: j.Mozgas(1, 0); break;
        case ConsoleKey.LeftArrow: j.Mozgas(0, -1); break;
        case ConsoleKey.RightArrow: j.Mozgas(0, 1); break;
        case ConsoleKey.Escape: j.Vege = true; break;
    }
} while ();
WeSt
  • 2,628
  • 5
  • 22
  • 37
Cilinder
  • 21
  • 1
  • Why was my question edited and -1'd? And I have to wait 4(!) days to ask another question when I might need to ask more considering I have my project due next friday... I've been nothing but polite with my answers and you just -1 me. Feels rather annoying and unwelcoming if you ask me. – Cilinder Nov 22 '14 at 23:29
  • Your question was edited because it used incorrect formatting. For potential reasons of downvoting, look at the tooltip of the downvote button. – Mark Rotteveel Nov 23 '14 at 12:02

1 Answers1

0

I would recommend putting in a starttime at the beginning, and you can reference it in the while loop like this:

DateTime startTime = DateTime.Now;
do
{
   // Game play
} while(((TimeSpan) (DateTime.Now - startTime)).TotalMinutes < gameInterval);

You can give gameInterval the necessary value for how long the game should run for. If you reach the while loop and that total duration between now and when you started exceeds that interval, you won't loop again.

EDIT

I don't have my IDE open, but I don't believe the above is proper syntax, however it should still push you in the right direction. This question has a good answer for getting the duration, which should help you.

EDIT 2 I have changed the above code to use the TimeSpan, and it makes the assumption gameInterval is in minutes, however you can change that.

Community
  • 1
  • 1
AdamMc331
  • 16,492
  • 10
  • 71
  • 133
  • DateTime.Now() doesn't seem to work indeed. I may be dumb, but I couldn't get the solution I'm looking for from the other question you shared. I get how the TimeSpan works now and it does, but I still don't know how to make use of it in this particular situation. How do I get the starting time? – Cilinder Nov 22 '14 at 19:58
  • @Cilinder please see edit. I'm sorry, all of my C# code is at work and I'm kind of going off memory. – AdamMc331 Nov 22 '14 at 20:02
  • It says: 'Cannot convert type 'System.DateTime' to 'System.TimeSpan''. Any advice? – Cilinder Nov 22 '14 at 20:08
  • Wrap parens around the dates too? `((TimeSpan) (DateTime.now - startTime))? – AdamMc331 Nov 22 '14 at 20:10
  • No problem! Feel free to accept this answer (click the checkmark next to it) if this did solve your problem. – AdamMc331 Nov 22 '14 at 20:12