小c++游戏使用graphics.h,一次调用多个函数? (多线程?)
我正在写一个小球赛。我有一个名为 gravity
的函数,还有一个 while 循环来检查用户是否想要使用 wsad 键移动球。
我需要多线程还是有其他出路?我删除了一些与设置程序无关的代码。这只是重要的事情:
while(1) {
enableGravity();
char ch = getch();// i know getch() is not going to cut it
//maybe 2 different f() multi threaded , for gravity and position,
f(ch == 'w' || ch == 'W')
updateObjPosition('U');
else if(ch == 's' || ch == 'S')
updateObjPosition('D');
else if(ch == 'a' || ch == 'A')
updateObjPosition('L');
else if(ch == 'd' || ch == 'D')
updateObjPosition('R');
}
我在 main 中有这些函数。我需要程序启用重力,并且能够接受输入以通过 updateObjPosition()
同时移动球。
I'm writing a small ball game. I have a function called gravity
and I also have a while loop that checks whether the user wants to move the ball using wsad keys.
Do I need to multi thread this or is there another way out? I cut out some of the irrelevant code for setting up the program. Here is just the stuff that matters:
while(1) {
enableGravity();
char ch = getch();// i know getch() is not going to cut it
//maybe 2 different f() multi threaded , for gravity and position,
f(ch == 'w' || ch == 'W')
updateObjPosition('U');
else if(ch == 's' || ch == 'S')
updateObjPosition('D');
else if(ch == 'a' || ch == 'A')
updateObjPosition('L');
else if(ch == 'd' || ch == 'D')
updateObjPosition('R');
}
I have these functions in main. I need the program to enable gravity and also be able to accept input to move the ball through updateObjPosition()
simultaneously.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用多个线程。更明显的可能性是非阻塞键盘读取。
如果您在 Windows 上执行此操作,您的标准库中可能有一个
_kbhit
,它会告诉您是否按下了键盘上的某个键。如果您使用的是curses,则可以使用nodelay
告诉getch 立即返回,无论是否按下了某个键。其他系统可能仍然以不同的方式做事,但你明白了一般的想法......You could use multiple threads. A more obvious possibility would be a non-blocking keyboard read.
If you're doing this on Windows, you probably have a
_kbhit
in your standard library that will tell you if a key on the keyboard has been pressed. If you're using curses, you can usenodelay
to tell getch to return immediately, whether a key has been pressed or not. Other systems may do things in different ways still, but you get the general idea...您不需要多线程。您只需要一个 API,如果当前按下了指定的键,该 API 将返回 TRUE。这可能取决于平台。您在什么平台上构建/运行?
例如,在 Win32 上: GetAsyncKeyState< /a>
You don't need to multi-thread. You just need an API that returns TRUE if a specified key is currently pressed down. That will likely be platform dependent. What platform are you building/running on?
For example on Win32: GetAsyncKeyState
正如您所提到的,您在旧的 conio.h 中的 TC 3.1 上使用旧的 Borland 编译器,有一个函数,
该函数是一次性解决方案,用于获取是否按下键盘上的任何键
所以这是你获取关键输入的循环
通过他们的方式你应该至少升级到VC 08!
对于你的简单项目,我猜逻辑循环会非常小,这将为你解决所有的痛苦,但如果你编写一个具有硬逻辑的较长应用程序,那么你将不得不使用更好的异步 IO 方法,这些方法基本上涉及工作在2个线程上,1个获取输入,另一个执行逻辑运算..
As you have mentioned you are using old Borland compiler on TC 3.1 in the old conio.h there was a function
the function was a one shot solution for getting if any key is pressed on the keyboard
so here is your loop to get key input
By they way you should upgrade to VC 08 Atleast!
For your simple project where the logic loop i guess will be quite small this will handle all the pain for you but if you write a longer application with hard logic then you will have to use better and Asynchronous methods for IO and those methods basically involve working on 2 threads 1 getting Input and the other doing logic operations ..