How to make your program reset?

I am working on a small simple maze game, and am completely stuck on a problem that is probably all too easily solved. The idea is that the game starts over when you click on the "try again" button. I have done some research and many people with the same question solved it by calling setup(), or by creating a reset() function that has all the starting variables in it. I tried this, but it doesn't work. The button itself works fine, but I can't for the life of me figure out how to get the program to reset. Any ideas?

Tagged:

Answers

  • edited July 2015

    Hopefully, your program is keeping track of what's going on in lots of different variables. Your reset() function simply needs to reset those variables to the values they had when setup() ended. Then call your reset() function when your button is pressed.

    It's easy to describe and understand, but we can't debug "it doesn't work" without seeing the code of your attempt.

  • That is exactly what I tried, but it simply doesn´t do anything. I would link you the code, but I don't have it anywhere online, and it is far too much to copy/paste into a comment. Everything else works, I tested it with a println where I called the reset() function, and that worked just fine. I think the problem might be related to the fact the "game over" screen witht he button doesn't use any of the variables that are being reset, and so it can't be reset by resetting those? In which case I would to find a way to basically re-run the entire program...

  • Make the following changes:

    1) Do not assign any variables at the global level. If you are assigning variables at the global level, copy those assignments into setup(). Also add assignments for variables that start with the default value.

    2) Add a call to reset() as the last thing in setup().

    3) Move ALL variable assignments from setup() into reset().

    4) Move back any assignments that start with "load" back into setup.

    Example:

    START:

    String myName;
    color bestColor = color(0,128,0);
    int y = 90;
    int state = 1;
    boolean b;
    
    void setup(){
      size(400,400);
      myName = "TfGuy44";
    }
    
    void draw(){
      background(bestColor);
      text(myName,20,20);
      fill(255);
      y+=state;
      if ( y > 300 || y < 40 ) state *=-1;
      ellipse(40,y,20,20);
      line(0,90,width,90);
      if(b){
        background(0);
        fill(255,0,0);
        text("GAME OVER", 100,100);
      }
    }
    
    void mousePressed(){
      b = true;
    }
    

    AFTER 1:

    String myName;
    color bestColor;
    int y;
    int state;
    boolean b;
    // No assignments at global level now!
    
    void setup(){
      size(400,400);
      // Assignments are here now.
      bestColor = color(0,128,0);
      y = 90;
      state = 1;
      b = false; // This is the default value.
      myName = "TfGuy44";
    }
    
    void draw(){
      background(bestColor);
      text(myName,20,20);
      fill(255);
      y+=state;
      if ( y > 300 || y < 40 ) state *=-1;
      ellipse(40,y,20,20);
      line(0,90,width,90);
      if(b){
        background(0);
        fill(255,0,0);
        text("GAME OVER", 100,100);
      }
    }
    
    void mousePressed(){
      b = true;
    }
    

    AFTER 2:

    String myName;
    color bestColor;
    int y;
    int state;
    boolean b;
    // No assignments at global level now!
    
    void setup(){
      size(400,400);
      // Assignments are here now.
      bestColor = color(0,128,0);
      y = 90;
      state = 1;
      b = false; // This is the default value.
      myName = "TfGuy44";
      reset();
    }
    
    void reset(){
      // Doesn't do anything yet.
    }
    
    void draw(){
      background(bestColor);
      text(myName,20,20);
      fill(255);
      y+=state;
      if ( y > 300 || y < 40 ) state *=-1;
      ellipse(40,y,20,20);
      line(0,90,width,90);
      if(b){
        background(0);
        fill(255,0,0);
        text("GAME OVER", 100,100);
      }
    }
    
    void mousePressed(){
      b = true;
    }
    

    AFTER 3:

    String myName;
    color bestColor;
    int y;
    int state;
    boolean b;
    // No assignments at global level now!
    
    void setup(){
      size(400,400);
      reset();
    }
    
    void reset(){
      // Assignments are here now.
      bestColor = color(0,128,0);
      y = 90;
      state = 1;
      b = false; // This is the default value.
      myName = "TfGuy44";
    }
    
    void draw(){
      background(bestColor);
      text(myName,20,20);
      fill(255);
      y+=state;
      if ( y > 300 || y < 40 ) state *=-1;
      ellipse(40,y,20,20);
      line(0,90,width,90);
      if(b){
        background(0);
        fill(255,0,0);
        text("GAME OVER", 100,100);
      }
    }
    
    void mousePressed(){
      b = true;
    }
    

    AFTER 4:

    String myName;
    color bestColor;
    int y;
    int state;
    boolean b;
    
    void setup(){
      size(400,400);
      // Load assets only once, in setup(), like:
      // img = loadImage( "myIcon.PNG" );
      // strings = loadStrings( "myPoem.txt" );
      reset();
    }
    
    void reset(){
      // Ok, so I don't do any loading in this example...
      // But you would not need to reload assets in reset!
      bestColor = color(0,128,0);
      y = 90;
      state = 1;
      b = false;
      myName = "TfGuy44";
    }
    
    void draw(){
      background(bestColor);
      text(myName,20,20);
      fill(255);
      y+=state;
      if ( y > 300 || y < 40 ) state *=-1;
      ellipse(40,y,20,20);
      line(0,90,width,90);
      if(b){
        background(0);
        fill(255,0,0);
        text("GAME OVER", 100,100);
      }
    }
    
    void mousePressed(){
      if(b){
        reset(); // ADD LOGIC TO CALL YOUR RESET FUNCTION!!!
      } else {
        b = true;
      }
    }
    
  • I've got this online example which resets by calling createBalloons():
    http://studio.processingtogether.com/sp/pad/export/ro.9V7R1wu55fOiN/latest

  • Thank you for your hlp, TfGuy44, but if I do that the game breaks. The character won't move, the walls become transparent. I guess there's no easy answer to this.

Sign In or Register to comment.