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```


3
  • 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. gamedev.stackexchange.com/questions/651/… Commented Feb 24, 2023 at 2:45
  • Thanks for advice, I kind of picked up this game to practice multithreading. Is there a way to solve this problem using them? Commented Feb 24, 2023 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. Commented Feb 24, 2023 at 17:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.