1

I am trying to follow a snake tutorial game using C++. However, it seems they were using some windows libraries and in my case I am compiling it on Linux. The problem is in my Input() method, I tried changing it with a recommended code I found on here, but that did not quite worked for me. Is there a way to go around this or any recommendation? Thanks.

    #include <iostream>
    #include <stdio.h>
    #include <ncurses.h>
    #include <unistd.h>
    .....
    //Get the key input inorder to move the snake around
    void Input() {
        //Tried this from the stackoverlow recommendations, did not work for my situation
        if (getch() == '\033') { 
        getch(); 
        switch(getch()) { // the real value
            case 'A':
                // code for arrow up
                direction = UP;
                break;
            case 'B':
                // code for arrow down
                direction = DOWN;
                break;
            case 'C':
                // code for arrow right
                direction = RIGHT;
                break;
            case 'D':
                // code for arrow left
                direction = LEFT;
                break;
             case 'Q':
                gameOver = true;
                break; 
            default:
                break;
        }
    }

    }
    .....

The issue I am having with is that linux does not accept the kbhit() which what the snake game tutorial was using, so I tried to modify it with what I have above, however it does not move the snake.

KonoDDa
  • 41
  • 5
  • What "didn't work" ? – Brad S. Oct 11 '17 at 21:25
  • @BradS. My input method, the way the tutorial had it set up was using kbhit(), and that is for windows only. However I want to find a way do to do it in linux. – KonoDDa Oct 11 '17 at 21:28
  • so, your problem is that you have to hit return? – Brad S. Oct 11 '17 at 21:29
  • What *does* happen when you run this code? My guess is that the game freezes, not accepting input or advancing the snake or anything, and that this stays the case after you hit enter. If that’s the case, then I’d recommend looking into how to use ncurses to set raw instead of cooked input. – Daniel H Oct 11 '17 at 21:35
  • @nos I cut the code, and also added more detail on my problem. – KonoDDa Oct 11 '17 at 21:35
  • @DanielH Yes that what happens. I'll look into it. Thanks – KonoDDa Oct 11 '17 at 21:37
  • 1
    Helpful reading: [Using kbhit() and getch() on Linux](https://stackoverflow.com/questions/29335758/using-kbhit-and-getch-on-linux) – user4581301 Oct 11 '17 at 21:42
  • Since you are using C++, shouldn't you use [`std::istream::peek`](http://www.cplusplus.com/reference/istream/istream/peek/) or [`std::istream::get`](http://www.cplusplus.com/reference/istream/istream/get/) on `std::cin` (or whatever your input stream is)? – jww Oct 11 '17 at 22:13

1 Answers1

-2

This case 'A': expects capital letter to be typed. Which is not the case :) normally. So:

switch(getch()) { // the real value
            case 'A': // either capital
            case 'a': //  or low case 
                // code for arrow up
                direction = UP;
                break;
c-smile
  • 26,734
  • 7
  • 59
  • 86