keyPressed reset after Game Over

edited December 2015 in Questions about Code

So I've been working with a reset function, which is called in setup and is also inside my Keypressed function when the user presses Backspace. Could someone tell me why it won't work after "Game Over?" And what I can do to fix it?

// ---------------------------------------
//type Spike initializing mySpike instance   
// ---------------------------------------
Spike mySpike;

//declare a variable to hold the molecule instance
Molecule [] movingMolecule = new Molecule [4] ;  


// ---------------------------------------
//Initialize enemy array 
// ---------------------------------------
Bubble[] bubbles = new Bubble[15];

// ---------------------------------------
//global variable for showing instruction 
// ---------------------------------------

boolean displayInstructions = true; 
int endInstructions;


// ---------------------------------------
// Collision booleans
// ---------------------------------------

//create 4 booleans for collision of molecules with 4 different spikes. 
//initialized to false until they touch, making them true. 
boolean isColliding1 =false;
boolean isColliding2 =false;
boolean isColliding3 =false;
boolean isColliding4 =false;


// ---------------------------------------
//timer 
// ---------------------------------------
//Count down and game over 
//set the font 
PFont font;
// initialize the current time variable 
int time;
//time string to display on the screen 
String timeString = "00";
// set initial time variable 
int initialTime;
int interval = 1000;
// total time is 40 seconds 
int totalTime = 50000;
// current time initialized at 0 
int currentTime=0;
// initialize start time 
int startTime;
// initialize end time 
int timePassed;

// ---------------------------------------
// Setup 
// ---------------------------------------
void setup() {
  //set the canvas size to 500 by 500
  size (500, 500);
  textSize(18);
  font= createFont("Arial", 90);
  int initialTime = millis();
  // reset function 
  reset();

  // ----------------------------------------------------------------------
  // to put in reset function V
  // end instruction state after 7 seconds 
  endInstructions = millis() + 7000;

  // fill with black 
  fill(0);
  //set initial time in millis for count dowm 
  initialTime = millis();
  //instanciate the Spike object
  mySpike = new Spike (100, 100, 25);

  //instanciate the Molecule object, displaying all of the array  
  for (int i =0; i < movingMolecule.length; i ++)
    movingMolecule[i]= new Molecule(200+i*50, 200+i*50);

  //instanciate the Bubble enemy object, displaying all of the array  
  for (int i =0; i < bubbles.length; i ++)
    bubbles[i]= new Bubble(200+i*random(50), 200+i*random(50), 8);
}

// ----------------------------------------------------------------------
// Draw 
// ----------------------------------------------------------------------
void draw() {
  //set background to blue 
  background(57, 69, 170);


  // ---------------------------------------------- //
  //          Display instructions
  //-----------------------------------------------//
  if (displayInstructions) {
    background(57, 68, 180);
    fill(130, 160, 123);
    smooth();
    textAlign(CENTER, CENTER);
    textSize(38);
    font = createFont("EurostileRegular-38.vlw", 38);
    text(" Can't Touch This ", 230, 100);
    textSize(14);
    font= createFont("BankGothic-Light-14.vlw", 14);
    text("Use the Arrow keys to move", 230, 170);
    text(" Connect 4 molecules to 4 points before time runs out!", 230, 200);
    text(" Avoid the evil floating bubbles", 230, 230);
    //text("press SPACE to start",width/2,210);
    //if the total time has run out for the instructions.. 
    if (millis() > endInstructions) {
      //display instructions returns false, start game  
      displayInstructions =false;
      startTime =millis();
      timePassed =0;
    }
    //return;
  } 
  //--------------------------------------------------------------//
  //display game
  else
  {
    timePassed = millis() - startTime;
    timeString = nf(timePassed/1000, 2);
    println(timePassed);
    //-----------------------//
    // Draw Bubbles 
    //-----------------------//
    // draw the ascend, display and change of color/size of bubble enemies >:) 
    for (int i = 0; i < bubbles.length; i++) {
      bubbles[i].ascend();
      bubbles[i].display();
      bubbles[i].change();
    }



    // ----------------------------------------------------------------------
    // Timer 
    // ----------------------------------------------------------------------

    // display the final timestring in milliseconds at the top of the screen 
    // fill(255);
    createFont("AgencyFB-Bold-30.vlw", 30);
    text(timeString + " sec", 420, 27);
    // if the milliseconds are greater than or equal to the total time, 


    if (timePassed >= totalTime)
    {
      // display the Game over text in the middle of the screen 
      createFont("AgencyFB-Bold-50.vlw", 50);
      //textSize
      textSize(50);
      text("Game Over", 225, 220 );
      noLoop();
      // display "press r to restart text.. and impliment it 
      //  text("Press R to restart", 225, 220 );
    }



    //display Spike object
    mySpike.displaySpike(250, 250, 25);
    //display Molecule object
    if (movingMolecule[0].attached!=0 && movingMolecule[1].attached!=0 && movingMolecule[2].attached!=0 && movingMolecule[3].attached!=0) {
      //..text you win
      println("you win2");
    }
    /*println("coll1:: "+movingMolecule[0].attached);
     println("coll2:: "+movingMolecule[1].attached);
     println("coll3:: "+movingMolecule[2].attached);
     println("coll4:: "+movingMolecule[3].attached);*/
    for (int i =0; i < movingMolecule.length; i ++) {
      movingMolecule[i].displayMolecule();
      //text("score ="+score,10,10);
      // println("MOVING"+movingMolecule[i].attached);


      //-----------------------------------------------------------------------
      // Check for Molecule collision with 1, 2, 3 and 4 spike points  
      //-----------------------------------------------------------------------

      //set booleans of colliding to correspond with the moving molecule 
      if (movingMolecule[i].attached==0)
      {
        isColliding1 = movingMolecule[i].collidesWith( mySpike.xPosSpike - 18, mySpike.yPosSpike - 45);
        isColliding2 = (movingMolecule[i].collidesWith( mySpike.xPosSpike + 18, mySpike.yPosSpike -45));
        isColliding3= (movingMolecule[i].collidesWith ( mySpike.xPosSpike + 20, mySpike.yPosSpike +26));
        isColliding4= (movingMolecule[i].collidesWith ( mySpike.xPosSpike - 20, mySpike.yPosSpike +23));


        // if molecule collides with top left spike.. 
        if (isColliding1 ==true)
        {
          //..attach molecule to position and connect it permanantly to moving molecule 
          // x & y of molecule will now be equal to the triangle point x and y  of mySpike
          movingMolecule[i].attached = 1;
          movingMolecule[i].x = mySpike.xPosSpike - 18;
          movingMolecule[i].y = mySpike.yPosSpike -45;
        }


        //if molecule collides with bottom right spike.. 
        else if (isColliding2 ==true)
        {
          //attach the moving molecule to 2nd position, and permenantly connect the x & y of molecule. 
          movingMolecule[i].attached = 2;
          movingMolecule[i].x = mySpike.xPosSpike + 18;
          movingMolecule[i].y = mySpike.xPosSpike - 45;
        }

        //if molecule collides with bottom right spike.. 
        else if (isColliding3 ==true)
        {
          //attach the moving molecule to 3rd position, and permenantly connect the x & y of molecule. (bottom right triangle) 
          movingMolecule[i].attached = 3;
          movingMolecule[i].x = mySpike.xPosSpike + 20;
          movingMolecule[i].y = mySpike.xPosSpike + 26;
        } 
        //if molecule collides with bottom left spike.. 
        else if (isColliding4 ==true)
        {
          //attach the moving molecule to 4th position, and permenantly connect the x & y of molecule. (bottom right triangle) 
          movingMolecule[i].attached = 4;
          movingMolecule[i].x = mySpike.xPosSpike - 20;
          movingMolecule[i].y = mySpike.xPosSpike + 23;
        }

        //       if (movingMolecule[i].attached = 1, movingMolecule[i].attached = 2, movingMolecule[i].attached = 3, movingMolecule[i].attached = 4) {
        //    println("you win2");
        //       }


        // -------------------------------------
        // no collision
        //-------------------------------------
        //if molecules don't collide with any of the 4 spikes...

        if ( movingMolecule[i].attached==0)
        {
          //..continue updating the molecule positions 
          movingMolecule[i].updtateMolPosition();
        }
      } // if attached is true for point one, two, three and four, attach molecules to these points 
      else
      {
        //if 1st point is attached to a moving molecule.. 
        if ( movingMolecule[i].attached==1) {
          // attach x and y of molecule to top right spike x and y position 
          movingMolecule[i].x = mySpike.xPosSpike - 18;
          movingMolecule[i].y = mySpike.yPosSpike -45;
        }
        //if 2nd point is attached to a moving molecule.. 
        if ( movingMolecule[i].attached==2) {  

          //.. attach x and y of molecule to 2nd Spike 
          movingMolecule[i].x = mySpike.xPosSpike + 18;
          movingMolecule[i].y = mySpike.yPosSpike -45;
        }
        if (movingMolecule[i].attached==3) {
          //.. attach x and y of molecule to 3rd Spike 
          movingMolecule[i].x = mySpike.xPosSpike + 20;
          movingMolecule[i].y = mySpike.yPosSpike +26;
        }
        if (movingMolecule[i].attached==4) {
          //.. attach x and y of molecule to 4th Spike 
          movingMolecule[i].x = mySpike.xPosSpike - 20;
          movingMolecule[i].y = mySpike.yPosSpike +23;
        }
      }//else
      //--------------------------------------------------------------
      // set collision with bubbles to offset collision with spike 
      //-------------------------------------------------------------
      for (int j=0; j<bubbles.length; j++)
      {
        if (movingMolecule[i].collidesWithBubble(bubbles[j].x, bubbles[j].y, bubbles[j].diameter))
        {

          if (movingMolecule[i].attached!=0)
          {
            println("bubble colliding test ");
            if (movingMolecule[i].attached ==1)
            {
              //make 1st point collision (top right point) false 
              isColliding1 =false;
            }
            if (movingMolecule[i].attached ==2)
            {
              // make 2nd point collision (top left point) false 
              isColliding2 =false;
            }
            if (movingMolecule[i].attached ==3)
            {
              // movingMolecule[i].updtateMolPosition();
              //make 3rd point collision (bottom right point) false 
              isColliding3 =false;
            }
            if (movingMolecule[i].attached ==4)
            {
              //make 4th point collision (bottom left point) false 
              isColliding4 =false;
            }
            movingMolecule[i].attached=0;
          }
          //println("bubbles colliding");
        }
      }
    }//for
    //} // if statement for start screen
  }
}  //draw

// ----------------------------------------------------------------------
// keyPressed function for spike
// ----------------------------------------------------------------------
void keyPressed() {

  // use this to release the molecules if more than one is attached? 
  //if (displayInstruction
  if (keyCode == BACKSPACE) {
    reset();
  }


  mySpike.keyPressed();
}

// ----------------------------------------------------------------------
//keyReleased function for spike
// ----------------------------------------------------------------------
void keyReleased() {
  mySpike.keyReleased();
}

// ----------------------------------------------------------------------
//reset game function when game over 
// ----------------------------------------------------------------------
//initialize the stopgame to false above with boolean  boolean stopgame=false; before setup

void reset() {

  //endInstructions = millis();

  startTime =millis();
  timePassed =0;
  //time = 00;
  // formats the number into a time string. String has 2 units 
  timeString = nf(timePassed, 2);
  // initial time is set to milliseconds 
  // initialTime = millis();
  currentTime = millis();
  //currentTime = millis()- endInstructions;
  // reset new spike 
  mySpike = new Spike (100, 100, 25);

  //  //instanciate the Molecule object, displaying all of the array  
  for (int i =0; i < movingMolecule.length; i ++)
    movingMolecule[i]= new Molecule(200+i*50, 200+i*50);

  //instanciate the Bubble enemy object, displaying all of the array  
  for (int i =0; i < bubbles.length; i ++)
    bubbles[i]= new Bubble(200+i*random(50), 200+i*random(50), 8);
}
Tagged:

Answers

  • because of line 161?

  • edited December 2015 Answer ✓

    When you use noLoop(), program stops listening for key events, so backspace won't work.

  • Answer ✓

    let me give you an advice...

    don't use noLoop()

    instead set a var boolean gameOver to true when time has passed

    your draw is far too long - most stuff like collision etc. you don't want when you are on instruction screen or when it's game over

    Hence make a new function playGame and call it from draw. The func draw will be much leaner then

  • later google state here in the forum - the idea is that your program can be in state game, instructions or game over.

  • Thank you, I will clean up my draw and create separate functions. Just to be clear, the "start game" should encompass the timer as well? Or should I separate them - by using a class for the timer?

  • Answer ✓

    function playGame should encompass the timer

Sign In or Register to comment.