我使用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