求助kbhit函数怎么用
最近想做一个纯文字游戏,比如:我想一边让NPC 10秒执行一次任务(如食物每十秒钟增加10),一边让玩家免回车键盘输入(如操控玩家按k键砍树),并让两者同时进行。网上说最好用kbhit函数,请问该如何实现?(若其他函数或手写函数也行,版本为Windows)
程序代码:#include <conio.h>
#include <print>
#include <chrono>
#include <thread>
int main( void )
{
unsigned 食物 = 0;
auto last_time = std::chrono::steady_clock::now();
for( ; ; std::this_thread::yield() )
{
if( auto t=std::chrono::steady_clock::now(); t-last_time>=std::chrono::seconds(10) )
{
last_time = t;
食物 += 10;
std::println( "食物 = {}", 食物 );
}
if( _kbhit() )
{
int ch0=_getch(), ch1=0;
if( ch0==0x00 || ch0==0xE0 )
ch1 = _getch();
if( ch0 == 'k' )
{
std::println( "砍树" );
}
}
}
}