How would I implement a restart?

I’d like to add the ability to press a key to restart after winning or losing the game so the player doesn’t have to close and reopen the game. Would someone be able to explain this to me? I tried to find steps for this online and I didn’t see anything.

I feel like I’m halfway to getting how one would do that but not entirely. Any help would be appreciated! :raised_hands:

I’m going to leave some of this as a challenge for you to implement but I’ll break-down how restarts work for any game with some context for Dapper Dasher to make it easier.

First, let’s break down the tasks needed for a restart to work in any game on a high level. Whether this be restarting a level or restarting the whole game.

  • Identify the variables in the game’s state that would trigger a game over, this being the state of the player and any other objects in the game.
  • Determine what their default state(s) would be at the start of the game
  • Reset those states to their default values from the start of the game, usually after the press of a button or click of a UI element that was shown to the player.

Let’s take a look at the first task for Dapper Dasher:

  • Our game is over when either there is a collision with the nebulae or we reach the finish line.
  • As a recap, collision is set to true when any of the nebulae come into contact with the player and ends the game. Our other game over condition is when scarfy’s x position is greater than the finish line we set.

Which means that in order for a game over case to be false we need both the collision variable to be false and for scarfyData.pos.x >= finishLine to also be returning false.

Alright, now let’s outline the challenge with a little psuedo-code. I’ll help out by placing it in the area where it would be best suited.

if (collision)
{
    // lose the game
    DrawText("Game Over!", windowDimensions[0]/4, windowDimensions[1]/2, 40, RED);

    //Prompt the player to restart the game
        //If yes, restart the game
            //Reset collision
            //Reset scarfy
            //Reset nebulae and finish line
        //If no, close the game
}
else if (scarfyData.pos.x >= finishLine)
{
    // win the game
    DrawText("You Win!", windowDimensions[0]/4, windowDimensions[1]/2, 40, RED);
    //Prompt the player to restart the game
        //If yes, restart the game
            //Reset collision
            //Reset scarfy
            //Reset nebulae and finish line
       //If no, close the game
}

I’ll leave the implementation to you as a challenge. Everything you’ve learned so far will be enough to tackle this. Good luck and happy learning!

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms