C++ 如何使程序仅注册一个具有getch或其他功能的按键?
我想使用getch()或类似的方法在while()函数中注册击键C++ 如何使程序仅注册一个具有getch或其他功能的按键?,c++,for-loop,while-loop,getch,C++,For Loop,While Loop,Getch,我想使用getch()或类似的方法在while()函数中注册击键 while() { . . . if(kbhit()) k=getch(); else cout<<"no input"; cout<<k<<endl; k=0; Sleep(1200); . . .
while()
{
.
.
.
if(kbhit()) k=getch();
else cout<<"no input";
cout<<k<<endl;
k=0;
Sleep(1200);
.
.
.
}
while()
{
.
.
.
如果(kbhit())k=getch();
否则就不会简单了。像这样
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
using namespace std;
#define KB_UP 72
#define KB_DOWN 80
#define KB_LEFT 75
#define KB_RIGHT 77
#define KB_ESCAPE 27
#define KB_F8 66
bool QuitGame=false;
char KB_code;
void simple_keyboard_input();
int main(void)
{
while(!QuitGame)
{
/* simple keyboard input */
simple_keyboard_input();
}
return 0;
}
void simple_keyboard_input()
{
if (kbhit())
{
KB_code = getch();
switch (KB_code)
{
case KB_ESCAPE:
QuitGame=true;
break;
}
}
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
#定义KB_UP 72
#定义KB_向下80
#定义KB_左75
#定义KB_RIGHT 77
#定义KB_ESCAPE 27
#定义KB_F8 66
bool-QuitGame=false;
字符KB_码;
无效简单键盘输入();
内部主(空)
{
而(!退出游戏)
{
/*简单键盘输入*/
简单键盘输入();
}
返回0;
}
无效简单键盘输入()
{
if(kbhit())
{
KB_code=getch();
开关(KB_代码)
{
案件KB_逃逸:
QuitGame=true;
打破
}
}
}
简单。像这样
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
using namespace std;
#define KB_UP 72
#define KB_DOWN 80
#define KB_LEFT 75
#define KB_RIGHT 77
#define KB_ESCAPE 27
#define KB_F8 66
bool QuitGame=false;
char KB_code;
void simple_keyboard_input();
int main(void)
{
while(!QuitGame)
{
/* simple keyboard input */
simple_keyboard_input();
}
return 0;
}
void simple_keyboard_input()
{
if (kbhit())
{
KB_code = getch();
switch (KB_code)
{
case KB_ESCAPE:
QuitGame=true;
break;
}
}
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
#定义KB_UP 72
#定义KB_向下80
#定义KB_左75
#定义KB_RIGHT 77
#定义KB_ESCAPE 27
#定义KB_F8 66
bool-QuitGame=false;
字符KB_码;
无效简单键盘输入();
内部主(空)
{
而(!退出游戏)
{
/*简单键盘输入*/
简单键盘输入();
}
返回0;
}
无效简单键盘输入()
{
if(kbhit())
{
KB_code=getch();
开关(KB_代码)
{
案件KB_逃逸:
QuitGame=true;
打破
}
}
}
就我个人而言,我会使用以下内容:
int support_code = 0; //global variable
void input()
{
if (GetKeyState('W') & 0x8000)
{ int helpful_button = "1"; // unique number for every button!
if (helpful_button != support_code)
{ support_code = helpful_button;
// do something
}
}
else if (...)
...
// End of "key registering" "if's"
}
// And then somewhere else in your code you can "reset" 'support_code' for it to be 0
// (so you can use the same button again)
// But if you don't want a worm to be able to change direction from "UP" to "UP"
// Then you don't even have to implement "support_code = 0" part from it
int main()
{
...
while (!gameover)
{
input();
calculate();
draw();
...
support_code = 0;
}
...
}
对于一个“蠕虫游戏”,你可能不需要太多的按键(我想个人来说,我会使用这样的方式:
int support_code = 0; //global variable
void input()
{
if (GetKeyState('W') & 0x8000)
{ int helpful_button = "1"; // unique number for every button!
if (helpful_button != support_code)
{ support_code = helpful_button;
// do something
}
}
else if (...)
...
// End of "key registering" "if's"
}
// And then somewhere else in your code you can "reset" 'support_code' for it to be 0
// (so you can use the same button again)
// But if you don't want a worm to be able to change direction from "UP" to "UP"
// Then you don't even have to implement "support_code = 0" part from it
int main()
{
...
while (!gameover)
{
input();
calculate();
draw();
...
support_code = 0;
}
...
}
对于“蠕虫游戏”,您可能不需要太多的击键操作(我想试试这个:。如果您遵循某些链接,您将被指向ncurses库。因此,基本上我需要一种方法在获得密钥后刷新缓冲区(或不刷新缓冲区)所以下次它不会注册或使用不同的功能。睡眠1.2秒会使问题变得更糟。只是不要,按键会重复,当它已经向上移动时,意味着再次“爬虫”,所以没有区别。使用时钟更新游戏状态,睡眠(1)为避免烧掉100%核心。请尝试以下操作:。如果您遵循某些链接,您将被指向ncurses库。因此,基本上,我需要一种方法在获得密钥后刷新缓冲区(或不刷新)所以下次它不会注册或使用不同的功能。睡眠1.2秒会使问题变得更糟。只是不要,按键会重复,当它已经向上移动时意味着再次“爬虫”,所以没有区别。使用时钟更新游戏状态,睡眠(1)以避免燃烧100%的核心。