2
\$\begingroup\$

How would one go about implementing a method in which it calls the game to restart by pressing a button after game over?

I have my game class which puts the player in and starts the enemy sequence shown below. I already have my restart button as shown in the code snippet from the mouseinput class below and the problem is I don't know what to put in the area where the question marks are to get the thread to restart, if I'm using the appropriate language or not I'm not sure. I'm new please bare with me.

Thank you. What I'm doing now isn't working because it's glitching and the fps is shaky.

public Game()
{
    new Window(1000, 635, "Galaxy Monkeys", this);
    start();

    handler = new Handler();
    camera = new Camera(0, 0);
    this.addKeyListener(new KeyInput(handler));
    menu = new Menu(this);

    BufferedImageLoader loader = new BufferedImageLoader();
    sprite_sheet = loader.loadImage("/spritesheet.png");

    Enemy enemy = new Enemy(-20, r.nextInt(563), ID.Enemy, handler, this);
    Monkey p = new Monkey(500, 250, ID.Player, handler, this);

    handler.addObject(p);

    for(int i = 0; i < enemy_count; i++) 
    {
        handler.addObject(enemy);
    }

    this.addMouseListener(new MouseInput(handler, camera, this));
    background = loader.loadImage(("/background.jpg"));

    INSTANCE = this;        
}

Restart event on MouseInput.java:

public void mousePressed(MouseEvent e)
{
    int mx = (int) e.getX();
    int my = (int) e.getY();

    if(Game.state == STATE.GAMEOVER) 
    {
        if(GameOver.restartButton.contains(mx, my))
        {
            // Pressed Restart Button   
           Game.setState(Game.STATE.GAME);
            Game.INSTANCE = new Game();
        }
    }
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I'm not that familiar with developing on java, but I'd say you shouldn't create a new instance of Game since it is the class that is handling the game window and the rest of the instances (camera, menu..)

Right now, all the game info is initialized withing the Game class constructor, which is not convenient, since if you want to start all over again, your only alternative is to instantiate a new Game instance.

I would separate the logic of the scene from the logic of the engine, at least by creating new functions.

Seeing your project, I'd approach it like this:

  • SetupScene() - I'd create this new function to initialize the player and the enemies

  • ClearScene() - In this function you'd dispose of all the entities created in your current game state. This would include the enemies created in the SetupScene(), the player, and any new enemy created after the scene setup.

  • Game() - Keep the constructor to initialize the components that are the foundations of your engine. Once all components are initialized, call SetupScene()

  • Restart() - You would call this function when you wish to restart the game. In it you will call ClearScene() to remove all entities, and then call SetupScene() again. You could even add a loading screen here, in case the operation takes too much time.

When building big systems, it's important to keep things modular and clear. Even if it's your own system and you know perfectly understand and know how to work with it, yourself in 2 years or even 2 months could be extremely confused.

Not really game development, but I'd recommend this book to anyone who wants to step up their programming expertise: Clean Code

\$\endgroup\$
1
  • \$\begingroup\$ thank you, all this helped a lot. I just have to sort out the technical details now. \$\endgroup\$ Mar 7, 2018 at 18:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .