How to restart/reset the game?

edited October 2014 in How To...

Hi! I'm a 1styear college student so I'm really new at processing and I really need help for my game ASAP because the deadline is tomorrow. I need to restart my game, how can i do that? I am not using any class.

import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;

AudioPlayer player;
AudioPlayer player2;
Minim minim;

PImage gameover;
PImage gameover2;
PImage win;
PImage win2;
PImage bP;
PImage bg;
PImage instruction;
PImage creative;
PImage background;
PImage background2;
PImage background3;

int screenX, screenY;
int currentScreen;
int score = 0;
int gamestate = 0;
int WIN_STATE = 1;
int LOSE_STATE = 2;
int WIN_STATE2 = 3;
int LOSE_STATE2 = 4;
int clickcount = 30;
int clickcount2 = 30;
int clickcount3 = 10;

final int SPLASH_SCREEN = 0;
final int GAME_SCREEN = 1;
final int GAME_SCREEN2 = 2;
final int GAME_SCREEN3 = 3;

float timer = 10;
float timer2 = 8;
float timer3 = 8;
float squaretimer = 0;
float squaretimer2 = 0;
float squaretimer3 = 0;
float squarex = 0;
float squarey = 0;
float squaresize = 100;

    void setup()//WINDOWS DISPLAY AND SIZE
    { 
      screenX=round(displayWidth*0.80);
      screenY=round(displayHeight*0.80);
      size(screenX, screenY);
      loadMediaResources();
      loadSplashScreen();
      currentScreen = SPLASH_SCREEN;
    }


    void loadMediaResources() //load Image and sounds
    {  

      bP=loadImage("clicked.png"); 
      bg=loadImage("bg.jpg"); 
      creative= loadImage("creative.jpg"); 
      instruction=loadImage("instruction.jpg"); 
      gameover=loadImage("gameover.jpg");
      gameover2=loadImage("gameover2.jpg");
      win=loadImage("win.jpg");
      win2=loadImage("win2.jpg");
      background=loadImage("backgroound.jpg");
      background2=loadImage("backround2.jpg");
      background3= loadImage("background3.jpg");
      minim = new Minim(this);
      player = minim.loadFile("PushTheButton.mp3");
      player2 = minim.loadFile("JazzTip.mp3");
    }


    void loadSplashScreen()//first screen of my game
    {
      image(creative, 0, 0, screenX, screenY);//background image
      player.play();//music1 plays
      player.loop();//music1 loop
    }


    void loadMainMenuScreen() //Menu screen
    {
      image(bg, 0, 0, screenX, screenY);//background image
      player.pause();//pause music1
      player2.play(); //music2 plays
      player2.loop();//music2 loop
    }


    void loadGameScreen() //GAME 1 - EASY
    { 
      image(background, 0, 0, screenX, screenY); //background image of the game
      timer = timer - (1.0 / 30.0); //timer of the game
      squaretimer = squaretimer + (1.0 / 30.0); //timer of how the button move

    // directions of the button
      if (squaretimer < .75) 
      {
        if (squarex < width - squaresize)
        {
          squarex = squarex + (5 * squaretimer);
        }
        if (squarey > 0)
        {
          squarey = squarey - 2;
        }
      } else
      {
        if (squarex > 0)
        {
          squarex = squarex - 2;
        }
        if (squarey < height - squaresize)
        {
          squarey = squarey + (5 * squaretimer);
        }
      }

      // randomly change button's position
      if (squaretimer > 2)
      {
        squaretimer = 0;
        squarex = random(width - squaresize);
        squarey = random(0, height - squaresize);
      }


      if (bP.width > 0)
      {  
        image(bP, squarex, squarey, squaresize, squaresize); // the button
      }
     // timer and clicks to show on the screen
      fill(225);
      text("TIME: " + (int)timer, 10, 10);
      text("CLICKS: " + clickcount, 10, 20);

    // when the game ends, possible state.
      if (timer < 0)
      {
        if (clickcount <= 0)
        {
          gamestate = WIN_STATE;
        } else
        {
          gamestate = LOSE_STATE;
        }
      }
    }


    void loadGameScreen2() // GAME 2 - MODERATE
    { 
      image(background2, 0, 0, screenX, screenY); //background image of the game
      timer2 = timer2 - (1.0 / 30.0);  //timer of the game
      squaretimer2 = squaretimer2 + (1.0 / 25.0);  //timer of how the button move

    // directions of the button
      if (squaretimer2 < .75)
      {
        if (squarex < width - squaresize)
        {
          squarex = squarex + (5 * squaretimer2);
        }
        if (squarey > 0)
        {
          squarey = squarey - 2;
        }
      } else
      {
        if (squarex > 0)
        {
          squarex = squarex - 2;
        }
        if (squarey < height - squaresize)
        {
          squarey = squarey + (5 * squaretimer2);
        }
      }

      // randomly change square position 
      if (squaretimer2 > 1.2)
      {
        squaretimer2 = 0;
        squarex = random(width - squaresize);
        squarey = random(0, height - squaresize);
      }


      if (bP.width > 0)
      {
        image(bP, squarex, squarey, squaresize, squaresize);
      }
    // timer and clicks to show on the screen
      fill(225);
      text("TIME: " + (int)timer2, 10, 10);
      text("CLICKS: " + clickcount2, 10, 20);

    //when the game ends, possible state.
      if (timer2 < 0)
      {
        if (clickcount2 <= 0)
        {
          gamestate = WIN_STATE;
        } else
        {
          gamestate = LOSE_STATE;
        }
      }
    }

    void loadGameScreen3() GAME 3 - DIFFICULT
    { 
      image(background3, 0, 0, screenX, screenY); //background image of the game
      timer3 = timer3 - (1.0 / 30.0);  //timer of the game
      squaretimer3 = squaretimer3 + (1.0 / 10.0);  //timer of how the button move

    // directions of the button
      if (squaretimer3 < .75)
      {
        if (squarex < width - squaresize)
        {
          squarex = squarex + (5 * squaretimer3);
        }
        if (squarey > 0)
        {
          squarey = squarey - 2;
        }
      } else
      {
        if (squarex > 0)
        {
          squarex = squarex - 2;
        }
        if (squarey < height - squaresize)
        {
          squarey = squarey + (5 * squaretimer3);
        }
      }

      // randomly change button's position 
      if (squaretimer3 > 1)
      {
        squaretimer3 = 0;
        squarex = random(width - squaresize);
        squarey = random(0, height - squaresize);
      }


      if (bP.width > 0)
      {
        image(bP, squarex, squarey, squaresize, squaresize); // button of the game
      }

    // timer and clicks to show on the screen
      fill(225);
      text("TIME: " + (int)timer3, 10, 10);
      text("CLICKS: " + clickcount3, 10, 20);

    // when the game ends, posiible state
      if (timer3 < 0)
      {
        if (clickcount3 <= 0)
        {
          gamestate = WIN_STATE2;
        } else
        {
          gamestate = LOSE_STATE2;
        }
      }
    }


    void mousePressed() //WHEN THE MOUSE CLICK THE BUTTON (THIS IS THE REASON)
    { 
      if (clickcount > 0) //GAME 1
      {
        if (mouseX > squarex
          && mouseY > squarey
          && mouseX < (squarex + squaresize)
          && mouseY < (squarey + squaresize))
        {
          clickcount = clickcount - 1;
          squaresize = squaresize * .99;
        }
      }
      if (clickcount2 > 0)  //GAME 2
      {
        if (mouseX > squarex
          && mouseY > squarey
          && mouseX < (squarex + squaresize)
          && mouseY < (squarey + squaresize))
        {
          clickcount2 = clickcount2 - 1;
          squaresize = squaresize * 0.99;
        }
      }
      if (clickcount3 > 0) //GAME 3
      {
        if (mouseX > squarex
          && mouseY > squarey
          && mouseX < (squarex + squaresize)
          && mouseY < (squarey + squaresize))
        {
          clickcount3 = clickcount3 - 1;
          squaresize = squaresize * 0.99;
        }
      }
    }


    void restart()
    {

    }

    void draw()
    {

      if (currentScreen == GAME_SCREEN)
      { 
        loadGameScreen();
      } else if (currentScreen == GAME_SCREEN2)
      {
        loadGameScreen2();
      } else if (currentScreen == GAME_SCREEN3)
      {
        loadGameScreen3();
      }

      if (gamestate == LOSE_STATE)
      {
        image(gameover, 0, 0, screenX, screenY);
        player2.pause();
        player.play();
        keyPressed();

      }
      if (gamestate == WIN_STATE)
      {
        image(win, 0, 0, screenX, screenY);
        player2.pause();
        player.play();
        keyPressed();

      }

      if (gamestate == LOSE_STATE2)
      {
        image(gameover2, 0, 0, screenX, screenY);
        player2.pause();
        player.play();
        keyPressed();

      }
      if (gamestate == WIN_STATE2)
      {
        image(win2, 0, 0, screenX, screenY);
        player2.pause();
        player.play();
        keyPressed();

      }
    }



    void keyPressed() // SWITCH KEYPRESSED FUNCTION
    {
      switch (key) {
      case ' ' :
        image(instruction, 0, 0, screenX, screenY); //instruction image

        break;
      case 'E':
      case 'e':
        currentScreen = GAME_SCREEN;

        break;
      case 'M':
      case 'm':
        currentScreen = GAME_SCREEN2;
        break;
      case 'D':
      case 'd':
        currentScreen = GAME_SCREEN3;
        break;
      case ENTER : 
        loadMainMenuScreen();
        currentScreen = SPLASH_SCREEN;
        break;
      case ESC: 
        exit();
      }
    }

image alt textbackgroound background background3 bg button1 clicked creative gameover gameover2 instruction win win2

Tagged:

Answers

  • edited October 2014

    At some point, probably right after setup() ends, your program has loaded all the images and sounds it needs, and set up all the variables it's using with good starting values.

    Resetting is simple - just set all those variables back to what they were at that moment.

    A good way to do this is to pull out any setting variables that need to be reset - this EXCLUDES images and sounds that need to be loaded - into a separate reset() function. Note that you might also need to reset the values of some global variables that are set at the global level (not inside setup()). Now have setup() call reset(), and your program "starts" in that happy, just-started state, just as it did before.

    The perk, however, is that it should get back to that happy, just-started state when you call reset() too! So then you can just call reset() to "restart" your sketch whenever you like (depending on your game's state).

  • edited October 2014

    like this TfGuy44?

    final int RESET = 4;
    
    void restart()
    {
           setup();
           gamestate= 0;
          WIN_STATE = 1;
          LOSE_STATE = 2;
           WIN_STATE2 = 3;
          LOSE_STATE2 = 4;
          clickcount = 25;
          clickcount2 = 30;
          clickcount3 = 10;
          timer = 10; 
          timer2 = 8;
          timer3 = 8;
          squaretimer = 0;
          squaretimer2 = 0;
          squaretimer3 = 0;
          squaresize = 100;
    }
    
    void draw()
    {
    
    if (reset==RESET)
      {
      restart();
      }
    }
    
  • Instead of this:

    int globalVariable = 7;
    final int doesntChange = 9;
    int otherVariable;
    PImage img;
    
    void setup(){
      size(220,220);
      otherVariable = 0;
      img = loadImage("myImage.PNG");
    }
    

    Do this:

    int globalVariable;
    final int doesntChange = 9;
    int otherVariable;
    PImage img;
    
    void setup(){
      size(220,220);
      img = loadImage("myImage.PNG");
      reset();
    }
    
    void reset(){
      otherVariable = 0;
      globalVariable = 7;
    }
    
  • It is still not working TfGuy44 T.T

    void setup()//WINDOWS DISPLAY AND SIZE
    { 
      screenX=round(displayWidth*0.80);
      screenY=round(displayHeight*0.80);
      size(screenX, screenY);
      restart();
      loadMediaResources();
      loadSplashScreen();
      currentScreen = SPLASH_SCREEN;
    }
    
    void restart()
    {
     timer = 10;
     timer2 = 8;
     timer3 = 8;
     squaretimer = 0;
     squaretimer2 = 0;
     squaretimer3 = 0;
     squarex = 0;
     squarey = 0;
     squaresize = 100;
       gamestate = 0;
     WIN_STATE = 1;
     LOSE_STATE = 2;
      WIN_STATE2 = 3;
     LOSE_STATE2 = 4;
     clickcount = 25;
     clickcount2 = 30;
     clickcount3 = 10;
    }
    
  • edited October 2014

    What is "not working"?

    Do you call restart() when you need it?

  • the game won't reset after playing on it PhiLho :/

  • More precisely? It continues where it was? It displays a blank screen?

    Again, do you call restart()?

    BTW, have you searched reset restart in the Processing site (don't use this forum' search)? There are dozens of similar topics...

Sign In or Register to comment.