getch方法,getch()是否有替代方法?哪个不会暂停程序?

在ncurses环境下开发游戏时,遇到一个问题:游戏循环因getch()函数暂停,导致敌人只有在用户输入后才移动。解决办法是通过调用nodelay或half_delay设置getch()为非阻塞模式,或者让它在等待一段时间后返回,即使没有按键被按下。这样可以确保敌人的移动不再依赖用户输入。
摘要由CSDN通过智能技术生成

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;

}

解决方案

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值