2

I'm trying to build a primitive snake game. The function void Input() calls _kbhit() and _getch() But the problem is that I can't implement these function 'cause conio.h isn't included in the gcc package in linux. Is there an alternative way to accomplish the tasks of _kbhit() and _getch() without using conio header file ?

void Input()  // handle controls
{
if(_kbhit()) // boolean positive if key is pressed
{
switch(_getch()) // gets ascii val of key pressed
{
        case 'a':
        dir = LEFT;
        break;

        case 'd':
        dir = RIGHT;
        break;

        case 'w':
        dir = UP;
        break;

        case 's':
        dir = DOWN;
        break;

        case 'x':
        gameOver = true;
        break;
    }
}   
}
Ali
  • 3,373
  • 5
  • 42
  • 54
insomniac_tk
  • 174
  • 1
  • 12

1 Answers1

2

These functions are sort of "illegal" and are not used anymore in standard C++.

The ncurses library can be helpful.

ncurses, by the way, defines TRUE and FALSE. A correctly configured ncurses will use the same data-type for ncurses' bool as the C++ compiler used for configuring ncurses.

Here is an example illustrating how ncurses could be used like the conio example:

#include <ncurses.h>
int main()
{
initscr();
cbreak();
noecho();
scrollok(stdscr, TRUE);
nodelay(stdscr, TRUE);
while (true) {
    if (getch() == 'g') {
        printw("You pressed G\n");
    }
    napms(500);
    printw("Running\n");
}
}
HarshitMadhav
  • 4,769
  • 6
  • 36
  • 45