我是靠谱客的博主 疯狂耳机,这篇文章主要介绍c++ 小游戏如何实现斜方向移动(同时检测两个按键,现在分享给大家,希望可以做个参考。

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++内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(97)

评论列表共有 0 条评论

立即
投稿
返回
顶部