c++ 小游戏如何实现斜方向移动(同时检测两个按键
问题:在控制player移动时使用_getch()获取按键->判断->更改坐标。但无法同时获取两个按键事件
改进:使用GetKeyState(nVirtKey)检测是否按下对应虚拟键
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#define isKeyPressed(nVirtKey) (GetKeyState(nVirtKey) & (1<<(sizeof(SHORT)*8-1))) != 0 using namespace std; int main() { const char* str = "hello world"; while (1) { if (isKeyPressed(87)) cout << "w down"; else if(!isKeyPressed(87)) cout << "w up"; cout << endl; if (isKeyPressed(68)) cout << "d down"; else if (!isKeyPressed(68)) cout << "d up"; system("cls"); } system("pause"); }
结果:按下w,不按d
最后
以上就是疯狂耳机最近收集整理的关于c++ 小游戏如何实现斜方向移动(同时检测两个按键的全部内容,更多相关c++内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复