我使用Ncurses庫在C++中製作Pacman。我可以用我的代碼移動Pacman,但我想移動它,以便即使在我不按任何鍵時pacman仍在移動,並且當我按另一個方向鍵時,它會改變方向。現在,當我按下按鍵時,pacman只需要一步。在pacman向那個方向移動之前,我還必須按兩次或三次按鍵。即使沒有按下按鍵,我如何在屏幕上發生什麼?
if (ch==KEY_LEFT)
{
int b,row,column;
getyx(stdscr,row,column);
int h;
do // do-whileloop to move the pacman left until it hits the wall
{
column-=1;
mvprintw(row,column,">"); //print the ">" symbol
refresh();
waitf(0.1); //this pauses the game for 0.1sec
attron(COLOR_PAIR(1));
mvprintw(row,column,">");
attroff(COLOR_PAIR(1));
refresh();
waitf(0.1);
mvprintw(row,(b),"O"); //showing the open mouth of pacman
refresh();
waitf(0.1);
attron(COLOR_PAIR(1));a
mvprintw(row,column,"O");
attroff(COLOR_PAIR(1));
h = getch();
}
while(h == KEY_LEFT);
}
right = getch();
循環向右移動在if條件
up = getch();
循環,在if條件
down = getch();
接力拉昇在一個if條件下移動。
放置這樣一個條件,當用戶移動pacman時,它會進入一個循環並停下來,只有當它到達牆壁時纔等待下一條指令。 –
但我希望只要按下另一個鍵,pacman就會改變它的方向。 – User14229754