c# - Console.ReadKey() looping by itself in while loop -


i have loop , inside switch case basicly user interface lets people choose yes , continue game left , right arrows.

i added console.beep() give feedback user. reason once game ends(the game uses arrows(its snake)) option yes , no keep switching , hear beeping 10 20 times , stops. know reason? code :

while (true) {     console.clear();     console.writeline("~~~~~~~~~~~~~~~~~~~~~~~~~\n\n~~~~~~~~you lost!~~~~~~~~\n\n~~~~~~~~~~~~~~~~~~~~~~~~~\nyour score was: " + snakebody.count);     console.writeline("\ndo want continue?\n\n      " + (continue ? ">yes  /  no" : " yes  / >no"));     switch (console.readkey().key)     {         case consolekey.leftarrow:             if (!continue)             {                 console.beep(soundfrequency, soundlength);                 continue = true;             }             continue;         case consolekey.rightarrow:             if (continue)             {                 console.beep(soundfrequency, soundlength);                 continue = false;             }             continue;         case consolekey.enter:             console.beep(soundfrequency + 200, soundlength);             if (continue)                 snakegame();             break;         default: continue;     }     break; } 

when press , hold key, generates repeating keystrokes frequency , puts them input queue. looks when play game, generate lot of keystrokes way, application unable process them in time when think you've completed playing, application still has lot (about 10 20 times write) of keys yet process, that's reason.


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -