gpt4 book ai didi

c++ - 如何在不等待输入的情况下使用 getch()?

转载 作者:可可西里 更新时间:2023-11-01 09:24:25 26 4
gpt4 key购买 nike

 for (;;)
{
cout << "You are playing for:" << playtime << "seconds." << endl;
cout << "You have " << bytes << " bytes." << endl;
cout << "You are compiling " << bps << " bytes per second." << endl;
cout << "Press a to buy assembler monkey (produces 1 byte per second)/(cost 10 bytes)" << endl;
switch(getch())
{
case 'a': bytes = bytes - 10; bps++; break;
}
bytes = bytes + bps;
playtime++;
Sleep(1000);
system("cls");
}

假设这是我的增量游戏。我想在 1 秒后刷新我的游戏。我怎样才能让 getch() 等待输入而不停止所有其他东西?

最佳答案

使用kbhit() 函数来检测是否按下了一个键:)

类似于:

 for (;;)
{
cout << "You are playing for:" << playtime << "seconds." << endl;
cout << "You have " << bytes << " bytes." << endl;
cout << "You are compiling " << bps << " bytes per second." << endl;
cout << "Press a to buy assembler monkey (produces 1 byte per second)/(cost 10 bytes)" << endl;
if(kbhit()){ //is true when a key was pressed
char c = getch(); //capture the key code and insert into c

switch(c)
{
case 'a': bytes = bytes - 10; bps++; break;
}
}
bytes = bytes + bps;
playtime++;
Sleep(1000);
system("cls");
}

关于c++ - 如何在不等待输入的情况下使用 getch()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24848755/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com