我正在使用ncurses庫在C++中編寫Pacman遊戲,但我無法正確移動Pacman。我使用getch()
將它向上,向下,向左和向右移動,但只有向右移動,並且在按其他任何按鍵時不會移動到其他任何地方。我是否正確使用ncurses庫中的getch()函數?
這是用於向上移動的代碼片段。我寫了類似的代碼,其中一些條件相應地改變了向左,向右和向下移動。
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
}
我使用getch()錯誤,還是有其他事情我必須做?
所以我需要存儲這兩個值,並檢查if語句,如果他們等於向上的箭頭。 – User14229754
是的,這就是你應該做的 – meirrav
它不工作。 – User14229754