Restart question...

Hi guys. Me and a few friend are working on a racing game in 2D as a project for school. Most of it is done and good to go, but we have one remaining (bigger) problem which we haven’t been able to solve yet.

First the game is basically a menu, and when you’ve set up the type of game etc you get to the racing itself which is basically the second part of the program. Also the frame which the game is shown in is sent to the class that controlls the actual game classes. So the problem is, when one player has finished the race (it’s a two player game) we’re supposed to get back to main menu.

No one really has an idea how to do this, we were thinking about “starting over” when the race is finished but even so, no idea how to do that. So does anyone have any suggestions how to basically restart the whole menu then playing procedure?

yes

menu = new RaceMenu();

And the second thing to do is move it to newbies clubies.

If you’ve written the game, then this really ought to be easy, although your code will need a restructure.

You need to split your initialisation code into two halves.

  1. Stuff that only needs to be initialised once
  2. Stuff that needs to be reset every game.

You can leave 1) in the constructor of the classes & put 2) in a separate method, e.g. public void reset(). You can put a call into reset() at the end of the constructor if needs be.

If a class contains references of other classes (e.g. if you have a Car class which contains references to four Wheel classes), you need to make sure that your (Car) reset() method calls the reset() method on those other (Wheel) classes. That way when you call reset() on the top level class, all the others get reset too.

If the Menu system is AWT or Swing and needs to be hidden when playing the game and then recreated, you can add member functions to your main class to do the job & call them from your game loop. You will also need to call CreateMenu from the constructor

Now you can structure your main loop long the lines of:

public void playGameOnce() {
  destroyMenu();
  reset();
  while (!gameOver) {
    movement();
    ai();
    render();
   }
   createMenu();
 }

[quote]yes

menu = new RaceMenu();

And the second thing to do is move it to newbies clubies.
[/quote]
yeah thanks alot I’ve always been curios about how to create new object references! sarcasm


Alan_W

thanks for the advice, we’ve been doing something like that already, but I’ll try it out! Only problem is we’re on a deadline now so it’s a bit stressful atm

You are wellcomed. I think you should review what I said and Alan extended to easily understable form.
And if you have problems even now, you should look at a final state machine.

[quote]You are wellcomed. I think you should review what I said and Alan extended to easily understable form.
And if you have problems even now, you should look at a final state machine.
[/quote]
Uh, finite state machine, surely?

Or FSM, or FSA. It has a lot of names.
Or are you trying to imply that your implementation of FSM didn’t entered a final state thus running in a tight loop and making the computer unresponsive? ~_^

I should however take care to don’t use that terms from Game dev forums…