0

I made this game and I want to make a loop that continuously checks if a key has been pressed but when I make the loop it automatically feeds the character instead of feeding it X the amount you pressed the button. Without the loop you can only feed it once so how do I create a loop that just checks if the button has been pressed and then released.

{

        Console.WriteLine("Enter your name, please: ");

        string name = Console.ReadLine();

        Console.WriteLine("Nice to met you, " + name);

        Console.WriteLine("Press F to feed yourself(+10) and D to drink some water(+10)");

        int hunger = 60;
        int thirst = 60;

        ConsoleKeyInfo info = Console.ReadKey();
        //here is where I want it too loop but I dont want it too automatically feed every half seconds, how do I do this?
        {
            if (info.Key == ConsoleKey.F)
            {
                System.Threading.Thread.Sleep(500);
                {
                    hunger = hunger + 10;
                    Console.WriteLine("Food: {0:0.0}".PadRight(15), hunger);

                    Console.WriteLine("Water: {0:0.0}".PadRight(70), thirst);
                }
            }
            if (info.Key == ConsoleKey.D)
            {
                System.Threading.Thread.Sleep(500);
                {
                    thirst = thirst + 10;
                    Console.WriteLine("Food: {0:0.0}".PadRight(15), hunger);

                    Console.WriteLine("Water: {0:0.0}".PadRight(70), thirst);
                }
            }

        }
        while (hunger > 1 && hunger < 101 && thirst > 1 && thirst < 101)
        {
            System.Threading.Thread.Sleep(5000);
            Console.Write(" ");
            {
                hunger = hunger - 2;
                thirst = thirst - 4;
                Console.Write("Food: {0:0.0}".PadRight(15), hunger);

                Console.Write("Water: {0:0.0}".PadRight(70), thirst);

            }
Kara
  • 6,115
  • 16
  • 50
  • 57
Ravaen
  • 49
  • 1
  • 7
  • ReadKey needs to be inside the loop. If you need to constantly loop then check this answer : http://stackoverflow.com/questions/5620603/non-blocking-read-from-standard-i-o-in-c-sharp – PaulF Aug 11 '15 at 16:14
  • @PaulF If I do that it constantly activates that function instead of just activating it once. Is there a way for it to only activate the function if the key is pressed and allowing the key to be pressed multiple times – Ravaen Aug 11 '15 at 16:17
  • Check the solution at the bottom of the link I gave you. Inside the loop you check to see if a key press is available, if it is then you read the key (meaning it is no longer available) and then process it - so your "function" will be activated once per press of the particular key. – PaulF Aug 11 '15 at 16:32
  • did my answer solve your issue? – psoshmo Aug 11 '15 at 18:54

1 Answers1

1

You are only getting the value of Console.ReadKey() once, so once they press f or d, a loop would continue to feed them. try something like this:

do
{
    ConsoleKeyInfo info = Console.ReadKey();

        if (info.Key == ConsoleKey.F)
        {
            System.Threading.Thread.Sleep(500);
            {
                hunger = hunger + 10;
                Console.WriteLine("Food: {0:0.0}".PadRight(15), hunger);

                Console.WriteLine("Water: {0:0.0}".PadRight(70), thirst);
            }
        }
        if (info.Key == ConsoleKey.D)
        {
            System.Threading.Thread.Sleep(500);
            {
                thirst = thirst + 10;
                Console.WriteLine("Food: {0:0.0}".PadRight(15), hunger);

                Console.WriteLine("Water: {0:0.0}".PadRight(70), thirst);
            }
        }
}
while(info.Key != ConsoleKey.Escape);

this will do the loop, constantly checking for key presses until the user hits escape (you could make the cancelling condition be whatever you like, for example, some threshold amount of hunger or thirst being met)

psoshmo
  • 1,490
  • 10
  • 19