getch()是否有替代方法?哪个不会暂停程序? [英] Is there an alternative to getch(); which won't pause the program?
问题描述
我正在以ncurses编写游戏,我有一艘由用户控制的飞船,还有一个自动"装置,可以缓慢移动以杀死您.但是,我正在使用一个包含所有内容的while循环,每次使用getch()时,循环都会暂停并等待输入,这意味着敌人只有在用户输入后才移动.
I'm writing a game currently in ncurses and I have a ship that is controlled by the user and an 'automated' that moves slowly down the lines to kill you. However, I'm using a while loop containing everything and each time I use getch() the loop pauses and waits for input, meaning that the enemy only moves after input from the user.
c=getch(); //causes a pause until a button is pressed before the next action
if(c==97) //if a is pressed, move position of '*' left
{
dir=-1;
}
if(c==100) //if d is pressed, move position of '*' right
{
dir=1;
}
推荐答案
由于您使用的是诅咒,只需调用nodelay
或halfdelay
或timeout
即可使getch
成为非阻塞状态,或者让它返回稍等片刻后,如果未按任何键.请参见inopts(3ncurses)手册页.
Since you're using curses, just call nodelay
or halfdelay
or timeout
to make getch
non-blocking, or have it return after a short wait if no key has been pressed. See the inopts(3ncurses) man page.
这篇关于getch()是否有替代方法?哪个不会暂停程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!