0

I have an assignment to create a game like Frogger (you know - the game where a frog has to cross the street). So far I've created the logic behind the movement of the frog and the cars but I can't seem to run the processes simultaneously. I know that multithreading is the correct way to do this, but I'm having some trouble in passing my parameters through _beginthread.

Here's what I have so far:

void moveCarsLeft(int x, int y) {
    if (y < 0) {
        field[x][y + 4] = '_';
        Sleep(600);
        refreshField();
        showField();
        if (y == -4) {
            moveCarsLeft(x, WIDTH-1);
        }
        moveCarsLeft(x, y - 1);
    }
    else {
        if (y > WIDTH-4) {
            field[x][y] = '-';
            Sleep(600);
            refreshField();
            showField();
            moveCarsLeft(x, y - 1);
        }
        else {
            field[x][y + 4] = '_';
            field[x][y] = '-';
            Sleep(600); // this is what's messing up the whole thing
                        // it stops the whole program
                        // and that's why I think I need multithreading
            refreshField();
            showField();
            moveCarsLeft(x, y - 1);
        }
    }
}

void moveCarsRight(int x, int y) {
... // the opposite of moveCarsLeft()
}

...
int main() {
...
    _beginthread(moveCarsLeft, 0, what do I put here?);
...
}

So, I'd be really grateful if someone could tell me the correct way to achieve this functionality. Thanks in advance : ]

zoska
  • 1,684
  • 11
  • 23
  • "what do I put here?" -- that pretty much depends on what you are trying to run. – n. m. could be an AI Dec 18 '13 at 13:23
  • you are not really clear on "why" this must be in multi-threading. and even if it does what do you want to do in each part. – Roee Gavirel Dec 18 '13 at 13:26
  • I've added more code for you to see, but actually I only wanted to know how to pass the parameters to my function using _beginthread. I'm not asking for more efficient ways of writing my program. – Dilyan Traykov Dec 18 '13 at 13:33
  • I don't like the multithreading approach but that's my particular perspective... however I tried to answer your question in my post below ;) – Gianluca Ghettini Dec 18 '13 at 13:36
  • Games don't work that way and for a good reason. Have a look [here](http://stackoverflow.com/questions/17440555/using-timer-and-game-loop/17440807) for an explanation. – nvoigt Dec 18 '13 at 13:43
  • I would claim that multithreading is the *incorrect* way of doing this, but if you insist, the internet is pretty full of threading tutorials. – molbdnilo Dec 18 '13 at 13:44
  • you DID NOT show us the definition of _beginthread (and why the underscore??) or didn't say what do you want to pass to _beinthread.This question is way too general. First try doing something simple with threads. – zoska Dec 19 '13 at 13:19

2 Answers2

4

You don't really need multithreading... a typical old-style game engine would be:

while(1)
{
   userInput = ReadUserInput();
   currentGameStatus = UpdateGameStatus(oldGameStatus, userInput);
   DrawScreen(currentGameStatus);
   oldGameStatus = currentGameStatus;
}

of course this is just pseudo-code to grasp the basic idea...

however if you want to use multithreading you could use a shared game status container which the running threads can access and/or modify (you need to protect the critical sections with some mutexes).

If you use multithreading then expect some minor concurrency issues (e.g. the frog being run over by a car before it actually happens on screen, or the converse) because you lost the perfect state sequentiality given by a single loop in which the game proceeds step-by-step

Gianluca Ghettini
  • 11,129
  • 19
  • 93
  • 159
0

"what do I put here?"

You put a pointer to a struct. You define the struct to contain all of the parameters you want to pass.

ScottMcP-MVP
  • 10,337
  • 2
  • 15
  • 15