How to restart the code

I finished my game but I don’t know how to make the program restart from the beginning when it finish. any suggestions on how I could make the program restart?

Put all the code that initiates the game state in its own function, gameSetup or whatever, and then call that. Better yet, build a simple state engine. So you might decide that the possible states your program can be in are:

  • levelSelect (waiting for player to choose a level)
  • game (player is actually playing)
  • and gameOver (player is dead, and morosely staring at the “game over” screen, their high scores etc).

Then put all the corresponding code into sets of functions, like this:

game={} --an array to hold the functions

function game.init()
  --setup a new game here

function game.draw()
  --all your game drawing

function game.touched(touch)
  --your game touch routine

Have the same with the other states, eg levelSelect.init(), levelSelect.draw(), levelSelect.touched()

Then, your main setup and draw loops have a state pointer:

function setup()
  state = game --or levelSelect

function draw()

function touched(touch)

If you organise your code like this, starting a new game is as simple as calling game.init(). You might do this, for instance, from the touched routine of your gameOver state:

function gameOver.touched(touch)
  state = game

Alternatively, you can use the restart() function built in

true! i guess it depends on how quickly your code starts up, whether you load/ process assets, how much of a sense of continuity you want to keep between game cycles etc… I think it’s good practice though to have a built in game cycle restart, if only to practice modularising the code, separating game cycle specific setup from general setup etc. I noticed @Ignatz latest two blog posts were on this topic actually, @Codeabeginer you should look at the latest to posts on coolcodea