How do i make a resart button for my game

Hi i really want to know how to make a restart button for my game i cant figure it out

Answers

  • edited May 20

    you could say if(key=='n.......

    With mouse

    or place a text on the screen

    textAlign(CENTER);
    text("restart",restartX,restartY);
    textAlign(LEFT);
    

    define

    int restartX; 
    int restartY;
    

    before setup()

    then to click it with the mouse say

    if(mousePressed&&dist(mouseX,mouseY,restartX,restartY) <50) {
        restart();
    }
    

    restart

    to restart write a function restart() and call it, here clear your screen with background and reset variables eg typeOfBrush, used colors, size of ellipse etc.

    in general when you ask something, better show your code or describe what you mean

    Best, Chrisir ;-)

  • Could you post the code you've already got? That way it will be easier to help you. Here is an example where a ball gets reset with a click of a mousebutton:

    float x, y, xSpeed, ySpeed;
    
    void setup(){
      size(400,400);
      ballSetup();
    }
    
    void ballSetup(){
      background(50);
      x = width/2;
      y = width/2;
      xSpeed = random(-1, 1);
      ySpeed = random(-1, 1);
      fill(random(255),random(255),random(255));
    }
    
    void draw(){
       x += xSpeed;
       y += ySpeed;
       ellipse(x,y,10,10);
    }
    
    void mousePressed(){
      ballSetup();
    }
    

    The idea is that you have a function that holds the information of how the game should start. If it is run, it will change all the variables so that everything is how it is supposed to be at the start of the game.

  • That depends on the complexity of the game.
    For simple games, you just need to reset some variables to initial values. For more complex ones, you'll have to create new instances of all the objects used.

Sign In or Register to comment.