Morris
Morris

Reputation: 189

c# console app press spacebar without displaying it on screen

I want to make a real simple cookie clicker using c# console app. the idea is that everytime you click spacebar you'll get one cookie (endless) but my code doesn't work, does anyone know what I did wrong or forgot?

This is my code :

    static void Main(string[] args)
    {
        int cookie = 0;
        Console.WriteLine("Coockie Clicker, druk op spatie om te beginnen...");
        Console.ReadKey();
        Console.Clear();
        while (cookie <1000000)
        {
            Console.ReadLine();

        if(Console.ReadLine() == " ")
             {
                 cookie++;
                 Console.WriteLine("Cookies ="+cookie);
             }
            else
            {

            }
        Console.ReadKey();

        }

    }

what i am expecting to work?= everytime i press spacebar ( if(Console.ReadLine() == " ") <--- that bit over there it should add +1 to int cookie and do this until 1.000.000 but there is clearly something not working

Upvotes: 1

Views: 1535

Answers (3)

Thilina Koggalage
Thilina Koggalage

Reputation: 1084

Hope this is what you are looking for.

    static void Main(string[] args)
    {
        int cookie = 0;
        Console.WriteLine("Coockie Clicker, druk op spatie om te beginnen...");

        Console.Clear();
        while (cookie < 1000000)
        {

            if (Console.ReadKey(true).Key == ConsoleKey.Spacebar)
            {
                cookie++;
                Console.Clear();
                Console.WriteLine("Cookies =" + cookie);
            }

        }
    }

Upvotes: 4

Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34160

First of all, if you want to make it endless you should use something like

while (true)

instead of

while (cookie <1000000)

but note that although I doubt that you will get more cookies than int.MaxValue but however technically it could overflow.

Secondly Console.ReadLine() reads a line so it requires an Enter key at the end, you should just use Console.ReadKey()

an the third one is that you have an extra read line:

  Console.ReadLine();
  if(Console.ReadLine() == " ")

so it expects you enter a line (which you don't use, and just waiting for user to finally click enter) and then you read another line and compare it with " "

 if(Console.ReadKey().Key == ConsoleKey.Spacebar)
 {
      //generate your cookie here
 }

Upvotes: 1

CPerson
CPerson

Reputation: 1222

If you are simply looking to give a cookie per space bar click, then you can use Console.ReadKey. There is an overload which takes a boolean indicating whether you want to "intercept" the key press. When you intercept the key press, the value is not shown to the user.

Console.ReadLine, on the other hand, only returns a value if you hit return. The value is displayed to the user no matter what.

Here is an updated code sample:

static void Main(string[] args)
{
    int cookie = 0;
    Console.WriteLine("Coockie Clicker, druk op spatie om te beginnen...");
    Console.ReadKey();
    Console.Clear();
    while (cookie <1000000)
    {
        var ch = Console.ReadKey(true);

        if(ch.KeyChar == ' ')
        {
            cookie++;
            Console.WriteLine("Cookies ="+cookie);
        }
    }
}

Upvotes: 2

Related Questions