简体   繁体   English

使用graphics.h的小型c ++游戏,一次调用多个功能吗? (多线程?)

[英]Small c++ game using graphics.h , calling multiple functions at once ? (multi threading?)

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. 我有一个称为gravity的函数,还有一个while循环,用于检查用户是否要使用wsad键移动球。

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 . 我需要程序来启用重力,还需要能够接受输入以同时通过updateObjPosition()移动球。

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. 如果在Windows上执行此操作,则标准库中可能有一个_kbhit ,它将告诉您是否已按下键盘上的某个键。 If you're using curses, you can use nodelay to tell getch to return immediately, whether a key has been pressed or not. 如果您使用的是curses,则可以使用nodelay告诉getch立即返回,无论是否已按下某个键。 Other systems may do things in different ways still, but you get the general idea... 其他系统可能仍会以不同的方式执行操作,但是您已经有了大致的了解...

You don't need to multi-thread. 您不需要多线程。 You just need an API that returns TRUE if a specified key is currently pressed down. 您只需要一个API,如果当前按下指定的键,则该API返回TRUE。 That will likely be platform dependent. 那可能取决于平台。 What platform are you building/running on? 您在什么平台上构建/运行?

For example on Win32: GetAsyncKeyState 例如在Win32上: GetAsyncKeyState

As you have mentioned you are using old Borland compiler on TC 3.1 in the old conio.h there was a function 如前所述,您在旧conio.h中的TC 3.1上使用了旧的Borland编译器,

   kbhit(); 

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 该功能是一键式解决方案,用于获取是否在键盘上按下了任何键,因此这是您获取键输入的循环

  int getKey(){
     if(kbhit){
        return getch();
     }
     return -1;
 }

By they way you should upgrade to VC 08 Atleast! 通过这种方式,您应该升级到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 .. 对于您的简单项目,我想它的逻辑循环会非常小,这将为您解决所有的麻烦,但是如果您编写带有硬逻辑的更长的应用程序,则必须为IO使用更好的异步方法,这些方法基本上涉及在2个线程上1得到输入,另一个进行逻辑运算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM