我正在使用Ncurses庫在C++中製作Pacman。我能夠將Pacman與我的代碼一起移動,但在不同的方向上切換需要很長時間。例如,當Pacman向左移動並按下右箭頭鍵時,開始向右移動需要一些時間。C++遊戲沒有時間響應鍵
if (ch==KEY_LEFT)
{
int b,row,column;
getyx(stdscr,row,column);
for (b=column;b>=0;b-=1) //loop to move the pacman left until it hits the wall
{
mvprintw(row,b,">"); //print the ">" symbol
refresh();
waitf(0.2);
attron(COLOR_PAIR(1)); //this pauses the game for 1 second
mvprintw(row,b,">");
attroff(COLOR_PAIR(1));
refresh();
waitf(0.2);
mvprintw(row,(b),"O"); //showing the open mouth of pacman
refresh();
waitf(0.2);
attron(COLOR_PAIR(1));a
mvprintw(row,(b),"O");
attroff(COLOR_PAIR(1));
int h=0;
h=getch();
if (h!=KEY_LEFT)
{
break;
}
}
}
right=getch();
loop for right in an if condition
up=getch();
loop for up in an if condition
down=getch();
loop for moving down in an if condition
我對右,上,下做了同樣的事情。另外,我在每個if語句之前引入了新變量來存儲getch()的值。
「了大量的時間來切換」 ......什麼是「大量」?你自己的代碼有評論說「這會暫停1秒」。如果你的「很多」是「1秒」,那麼你的答案是。 –
需要那段時間時屏幕上會發生什麼?它還在畫什麼,即動畫? – user1201210
你不是依靠重複使pacman繼續移動,是嗎?這會違反pacman的精神,也可能是導致他「遲緩」的原因。 – paddy