我正在用Ncurses库在C++中制作吃豆人。我可以用我的代码移动吃豆人,但我想移动它,这样即使我没有按任何键,当我按下另一个方向键时,吃豆人也会继续移动,它会改变方向。现在,当我按下一个键时,吃豆人只会走一步。另外,在吃豆人朝那个方向移动之前,我必须按一个键2到3次。
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条件下向下移动的oop。
发布于 2012-09-24 23:56:14
在按下某个键之前,函数getch将被阻塞。如果你不想被阻塞,那么在getch之前调用_kbhit,确保在输入缓冲区中有一些东西。
编辑:看看ncurses函数nodelay和cbreak。它们支持异步输入。
https://stackoverflow.com/questions/12568391
复制相似问题