3

I'm coding a snake game in C and my compiler is turbo C. I've got a problem with moving the snake. I need a function or a way of coding so that I can move my snake without the keyboard waiting for a key to be pressed. but if a key is pressed some specific actions are to be done. I used the structure below but it doesn't work as I explained above:

while (gameStatus == CONTINUE)
{
   if(kbhit)
   {
      char c = getch();
      .....
   }
   .....
}

can anyone help?

Damien
  • 1,490
  • 1
  • 8
  • 17
Melika Barzegaran
  • 429
  • 2
  • 9
  • 25

2 Answers2

2

kbhit is a function. When you write if(kbhit) you are testing if the function exists. Instead call the function by writing if(kbhit()).

Melika Barzegaran
  • 429
  • 2
  • 9
  • 25
0
while (gameStatus == CONTINUE)
{
    if (kbhit())
    {
         char c = '\0';

         /*a keystroke return 0*/
         if ((c=getch())!=0) 
         {
            c=getch(); /*then you capture the actual directional key*/

            ......conditionals 
         }
         //else if not was a directional, you Can Pause or Something  
     .....
    }
.....
}
Jeans K. Real
  • 180
  • 4
  • 13