Am I using the getch() function from the ncurses library incorrectly?

Posted by user1692446 on Stack Overflow See other posts from Stack Overflow or by user1692446
Published on 2012-09-23T15:24:04Z Indexed on 2012/09/23 15:37 UTC
Read the original article Hit count: 219

Filed under:
|

I am writing a Pacman game in c++ using the ncurses library, but I am not able to move the Pacman properly. I have used getch() to move it it up, down, left and right, but it only moves right and does not move anywhere else when I press any other key.

This is a code snippet for moving up. I have written similar code with some conditions altered accordingly for moving left, right and down.

int ch = getch(); 
if (ch == KEY_RIGHT)
{
  int i,row,column;
  //getting position of cursor by getyx function
  for (i=column; i<=last_column; i+=2)
  {
    //time interval of 1 sec

    mvprintw(row,b,"<");   //print < in given (b,row) coordinates

    //time interval of 1 sec

    mvprintw(row,(b+1),"O");  //print "O" next to "<"
    int h = getch();   //to give the option for pressing another key 
    if (h != KEY_RIGHT)  //break current loop if another key is pressed
    {
      break;
    }
  }
}
if (condition)
{
  //code to move left
}

Am I using getch() wrong, or is there something else I have to do?

© Stack Overflow or respective owner

Related posts about c++

Related posts about ncurses