1

I am making my own version of minesweeper using p5.

Right now, when I click on the bomb cell, I show all the bombs in the given grid.. but the game is still playable. Is there any way to restart the game without reloading the page?

I can produce an alert but that does not do much except say that "game is over", is there any way to restart when I click on OK on that?

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
bjoshi
  • 105
  • 1
  • 9

1 Answers1

1

You'd want to store the state of your game in some sketch-level variables. Then to reset the game, you'd want to set the variables back to their original values. You might do this in something like a reset() function. Here's a simple example:

var y;

function setup() {
  createCanvas(400, 400);
    reset();
}

function reset(){
    y = 0;
}

function mousePressed(){
    reset();
}

function draw() {
  background(220);

    ellipse(width/2, y, 20, 20);

    y++;
}

In your case, you'd probably want to call the reset() function after the user presses the okay button in the dialog.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107