0

I'm trying to write a simple console game, space shooter -ish. The game runs on main loop. I want to use multithreading to manage shooting projectiles and another for movement of the enemy. How should I set it up? The shooting function works on a loop and while projectile is flying you can't do anything else. I can't find similiar problem anywhere, any answer much appraciated.

I tried defining thread where you can see async and it would crush. I tried setting thread objects in main for game func. and shooting func. but didn't work either.

while(1) {
        if (_kbhit) {
            char ch = _getch();
            switch (ch) {
            case 'a':
                if (shipx > 3) {
                    shipx--;
                    drawShip();
                    break;
                }
            case 'd':
                if (shipx < screenw / 2 - 9) {
                    shipx++;
                    drawShip();
                    break;
                }
            case 's': {async(launch::async, fire); break;}
            }
-------------------------------------------------------------
void fire() {

        gotoxy(shipx + 4, screenh - 4);
        cout << "^";
        for (int i = screenh - 5;i > 0;i--) {
            gotoxy(shipx + 4, i);
            cout << "^";
            gotoxy(shipx + 4, i + 1);
            cout << " ";
            this_thread::sleep_for(40ms);
        }   text```


  • 2
    You don't need multithreading. You need the basics of game development. Implement a game loop, measure the time between two iterations and move all objects. https://gamedev.stackexchange.com/questions/651/what-should-a-main-game-loop-do – Thomas Sablik Feb 24 '23 at 02:45
  • Thanks for advice, I kind of picked up this game to practice multithreading. Is there a way to solve this problem using them? – Tom Cruise Feb 24 '23 at 15:36
  • What library do you use? Is it thread-safe? Do you really want to create a thread for each moving object? Maybe, it's technically possible, but it's the wrong approach. – Thomas Sablik Feb 24 '23 at 17:28

0 Answers0