How to start a timer only on a certain scene??

edited February 7 in Library Questions

Hi I'm making a game where I need a timer but it only starts on a certain scene. I need the timer (timer.start()) to start only on level 1. Thanks for the help!

import lord_of_galaxy.timing_utils.*;
PImage pic;
PImage pic2;
PImage pic3;
PImage pic4;
PImage pic5;
PImage pic6;
PImage pic7;
PImage pic8;
PImage pic9;
PImage pic10;
PImage pic11;
PImage pic12;
PImage pic13;
PImage loseImage;
PFont menuFont;
PFont animFont;
PFont loseFont;
PFont menuFont2;
PFont menuFont3;
float imgX = 100;
float imgY = 750;
float eggX = 500;
float eggY = 700;
float opacity = 0;
float opacity2 = 255;
float opacity3 = 0;
float cloud1 = 100;
float cloud2 = 300;
float cloud3 = 500;
float cloud4 = 700;
float cloud5 = 900;
float anim = 5;
float animTimer = 0;
float animTimer2 = 0;
float loseTimer = 0;
float menuTimer = 0;
int food = 0;
float menuX = 500;
float cloudContinue = 1200;
String[] lost = new String [2];
String lose; 
boolean backgroundIsWhite = false;
Stopwatch timer;
Bass[] b = new Bass[2];
AquaticBug[] a = new AquaticBug[3];
Mayfly[] m = new Mayfly[3];
Plankton[] p = new Plankton[4];
Heron[] h = new Heron[1];
Pike[] pi = new Pike[2];

String scene = "animation"; 


void setup() {
  noStroke();
  size(1000, 1000);
  rectMode(CENTER);
  imageMode(CENTER);
  textAlign(CENTER);
  pic = loadImage("chinook-salmon-alevin.png");
  pic2 = loadImage("491375864 (1).png");
  pic4 = loadImage("Moomans hellgrammite.gif");
  pic5 = loadImage("LargemouthBass.gif");
  pic6 = loadImage("2017-hafele-spinner-3-1110.gif");
  pic7 = loadImage("2921797_orig.gif");
  pic8 = loadImage("plankton-md.gif");
  pic9 = loadImage("532606-ocean-water-wallpaper-1920x1200-xiaomi.gif");
  pic11 = loadImage("heron-clipart-96.gif");
  pic12 = loadImage("northern-pike-800px-Custom.gif");
  pic13 = loadImage("273px-SalmonoidsBergeau.gif");
  loseImage = loadImage("2921797_orig.gif");
  menuFont = loadFont("TwCenMT-CondensedBold-100.vlw");
  animFont = loadFont("TwCenMT-CondensedBold-48.vlw");
  loseFont = loadFont("TwCenMT-CondensedBold-70.vlw");
  menuFont2 = loadFont("TwCenMT-CondensedBold-65.vlw");
  menuFont3 = loadFont("TwCenMT-CondensedBold-40.vlw");
  timer = new Stopwatch(this);   
  for (int i = 0; i < b.length; i++) {
    b[i] = new Bass();
  }
  for (int i = 0; i < a.length; i++) {
    a[i] = new AquaticBug();
  }
  for (int i = 0; i < m.length; i++) {
    m[i] = new Mayfly();
  }
  for (int i = 0; i < p.length; i++) {
    p[i] = new Plankton();
  }
  for (int i = 0; i < h.length; i++) {
    h[i] = new Heron();
  }
  for (int i = 0; i < pi.length; i++) {
    pi[i] = new Pike();
  }
}

void checkForTimer() {
  if (scene == "level1") {
    timer.start();
  }
}
void useCloud() {
  drawCloud(cloud1, 100);
  drawCloud(cloud2, 300);
  drawCloud(cloud3, 100);
  drawCloud(cloud4, 300);
  if (cloud1 < -200) {
    cloud1 = cloudContinue;
  }
  if (cloud2 < -200) {
    cloud2 = cloudContinue;
  }
  if (cloud3 < -200) {
    cloud3 = cloudContinue;
  }
  if (cloud4 < -200) {
    cloud4 = cloudContinue;
  }
  if (cloud5 < -200) {
    cloud5 = cloudContinue;
  }
}  
void drawCloud(float a, float b) {
  float one = a; // 100
  float two = a + 50;
  float four = a + 100;
  float six = 100;
  float seven = b; // 100
  float nine = b - 25;
  float eleven = b + 10;

  fill(255, 255, 255);
  ellipse(one, seven, six, six);
  ellipse(two, nine, six, six);
  ellipse(four, seven, six, six);
  ellipse(two, eleven, six, six);
}
void drawSea() {
  fill(0, 100, 255);
  rect(500, 800, 1300, 550);
  fill(78, 79, 75);
  rect(500, 950, 1500, 100);
}
void draw() {
  switch(scene) {
  case "menu" : 
    {
      image(pic9, 500, 500);
      textFont(menuFont);
      fill(0, 0, 0);
      text("A Salmon's Life", 500, 250);
      image(pic2, menuX, 400, 306, 190);
      fill(0, 102, 204);
      rect(500, 500, 200, 100, 200);
      rect(500, 650, 200, 100, 200);
      rect(500, 800, 200, 100, 200);
      fill(255, 255, 255);
      textFont(menuFont2);
      text("PLAY", 500, 517);
      text("INFO", 500, 817);
      text("HOW TO", 500, 670);
      menuTimer += 1;
      if (menuTimer >= 50) {
        menuX -= 4;
      }
      if (menuX <= -350) {
        menuX = 1350;
      }
      if (mouseX > 400 && mouseX < 600 && mouseY > 450 && mouseY < 550) {
        fill(255, 255, 255);
        rect(500, 500, 200, 100, 200);
        textFont(menuFont2);
        fill(0, 102, 204);
        text("PLAY", 500, 517);
      }
      if (mouseX > 400 && mouseX < 600 && mouseY > 450 && mouseY < 550 && mousePressed == true) {
        scene = "level1";
      }

      if (mouseX > 400 && mouseX < 600 && mouseY > 750 && mouseY < 850) {
        fill(255, 255, 255);
        rect(500, 800, 200, 100, 200);
        textFont(menuFont2);
        fill(0, 102, 204);
        text("INFO", 500, 817);
      }
      if (mouseX > 400 && mouseX < 600 && mouseY > 750 && mouseY < 850 && mousePressed == true) {
        scene = "info";
      }

      if (mouseX > 400 && mouseX < 600 && mouseY > 600 && mouseY < 700) {
        fill(255, 255, 255);
        rect(500, 650, 200, 100, 200);
        textFont(menuFont2);
        fill(0, 102, 204);
        text("HOW TO", 500, 670);
      }
      if (mouseX > 400 && mouseX < 600 && mouseY > 600 && mouseY < 700 && mousePressed == true) {
        scene = "how to";
      }

      break;
    }
  case "how to" : 
    {
    }
  case "info" : 
    {
    }
  case "animation" : 
    {
      background(255, 255, 255, opacity2);
      opacity2 -= 5;
      animTimer2 += 1;
      fill(0, 100, 255);
      rect(500, 500, 10000, 10000); // sea background
      fill(78, 79, 75);
      rect(500, 900, 1000, 500); // gravel
      fill(255, 153, 153);
      ellipse(eggX, eggY, 100, 100); // egg
      textFont(menuFont);
      fill(0, 0, 0);
      text("A salmon is hatching!", 500, 200);
      textFont(menuFont3);
      text("Click anywhere to start!", 500, 950); 
      eggX += 0.5;
      eggY += 0.5;
      animTimer += 5;
      if (eggX >= 501) {
        eggX -= 4;
      }
      if (eggY >= 701) {
        eggY -= 3;
      }
      if (animTimer >= 500) {
        backgroundIsWhite = true;
      }
      if (backgroundIsWhite == true) {
        fill(255, 255, 255, opacity);
        rect(0, 0, 10000, 10000);
        opacity += 5;
      }
      if (animTimer >= 1000) {
        backgroundIsWhite = false;
        fill(0, 100, 255);
        rect(500, 0, 10000, 10000);
        fill(78, 79, 75);
        rect(500, 900, 1000, 300);
        fill(255, 153, 153);
        textSize(20);
        textFont(animFont);
        fill(0, 0, 0);
        text("A salmon has hatched! A baby salmon is called an alevin!", 500, 200);
        textFont(menuFont3);
        text("Use the arrow keys to move and avoid the bass and water bugs!", 500, 800);
        text("Click anywhere to start!", 500, 950); 
        image(pic, 500, 550, 250, 125);
      }
      if (mousePressed == true) {
        scene = "menu";
      } 
      break;
    }

  case "level1" : 
    {
      background(135, 206, 235);
      drawSea();
      if (keyCode == DOWN && keyPressed == true) {
        imgY += anim;
      }
      if (keyCode == UP && keyPressed == true) {
        imgY -= anim;
      }
      if (keyCode == RIGHT && keyPressed == true) {
        imgX += anim;
      }
      if (keyCode == LEFT && keyPressed == true) {
        imgX -= anim;
      }
      useCloud();
      image(pic, constrain(imgX, 0, width - 100), constrain(imgY, 550, height - 100));
      cloud1 -= 2;
      cloud2 -= 2;
      cloud3 -= 2;
      cloud4 -= 2;
      cloud5 -= 2;
      fill(0, 0, 0);
      textFont(menuFont3);
      text("Survive for 30 seconds!", 830, 50); 
      text(str(timer.time()/1000), 900, 100);
      if (timer.time()/1000 == 30) {
        scene = "transition1";
      }
      for (int i = 0; i < b.length; i++) {
        b[i].bass();
        b[i].move();
      }
      for (int i = 0; i < a.length; i++) {
        a[i].aquaticBug();
        a[i].move();
      }
      for (int i = 0; i < b.length; i++) {
        if (dist(imgX, imgY, b[i].x - 125, b[i].y) < 35) {
          scene = "lose";
          lose = "bass!";
          loseImage = pic5;
        }
      }
      for (int i = 0; i < a.length; i++) {
        if (dist(imgX, imgY, a[i].x - 70, a[i].y) < 40) {
          scene = "lose";
          lose = "water bug!";
          loseImage = pic4;
        }
      }
      break;
    }
  case "transition1" : 
    {
      fill(0, 102, 204, opacity3);
      rect(500, 500, 1500, 1500);
      fill(0, 0, 0);
      textFont(menuFont2);
      text("Nice Job! You beat the first stage!", 500, 300);
      textFont(menuFont3);
      text("You are now a Fry! Click anywhere to continue on!", 500, 700);
      text("Eat:", 100, 800);
      text("Avoid:", 100, 900);
      image(pic6, 170, 790);
      image(pic7, 500, 500);
      image(pic8, 220, 790);
      image(pic5, 350, 890);
      image(pic11, 700, 890);
      if (mousePressed == true) {
        scene = "level2";
      }
      opacity3 += .2;
      break;
    }
  case "level2" : 
    {
      background(135, 206, 235);
      drawSea();
      if (keyCode == DOWN && keyPressed == true) {
        imgY += anim;
      }
      if (keyCode == UP && keyPressed == true) {
        imgY -= anim;
      }
      if (keyCode == RIGHT && keyPressed == true) {
        imgX += anim;
      }
      if (keyCode == LEFT && keyPressed == true) {
        imgX -= anim;
      }
      useCloud();
      image(pic7, constrain(imgX, 100, width - 100), constrain(imgY, 550, height - 100), 150, 50);
      cloud1 -= 2;
      cloud2 -= 2;
      cloud3 -= 2;
      cloud4 -= 2;
      cloud5 -= 2;
      fill(0, 0, 0);
      textFont(menuFont3);
      text("Score: " + food, 900, 90);
      text("Get a food score of 50!", 800, 50);
      if (food == 50) {
        scene = "transition2";
      }
      for (int i = 0; i < m.length; i++) {
        m[i].mayfly();
        m[i].move();
      }
      for (int i = 0; i < p.length; i++) {
        p[i].plankton();
        p[i].move();
      }
      for (int i = 0; i < b.length; i++) {
        b[i].bass();
        b[i].move();
      }
      for (int i = 0; i < h.length; i++) {
        h[i].heron();
        h[i].move();
      }
      for (int i = 0; i < m.length; i++) {
        if (dist(imgX, imgY, m[i].x - 37.5, m[i].y) < 35) {
          food += 5;
          m[i].x = random(1000, 3000);
          m[i].y = random(550, 950);
        }
      }
      for (int i = 0; i < p.length; i++) {
        if (dist(imgX, imgY, p[i].x - 17.25, p[i].y) < 35) {
          food += 5;
          p[i].x = random(1000, 3000);
          p[i].y = random(550, 950);
        }
      }
      for (int i = 0; i < b.length; i++) {
        if (dist(imgX, imgY, b[i].x - 125, b[i].y) < 30) {
          scene = "lose";
          lose = "bass!";
          loseImage = pic5;
        }
      }
      for (int i = 0; i < h.length; i++) {
        if (dist(imgX, imgY, h[i].x + 129, h[i].y + 101) < 30) {
          scene = "lose";
          lose = "heron!";
          loseImage = pic11;
        }
      }
      break;
    }
  case "transition2" : 
    {    
      fill(0, 102, 204, opacity3);
      rect(500, 500, 1500, 1500);
      fill(0, 0, 0);
      textFont(menuFont2);
      text("AMAZING!! You beat the second stage!", 500, 300);
      textFont(menuFont3);
      text("You are now a Parr! Click anywhere to continue on!", 500, 700);
      text("Eat:", 100, 800);
      text("Avoid:", 100, 900);
      image(pic6, 170, 790);
      image(pic13, 500, 500, 400, 143);
      image(pic8, 220, 790);
      image(pic12, 350, 890);
      image(pic11, 700, 890);
      if (mousePressed == true) {
        scene = "level3";
      }
      opacity3 += .2;
      break;
    }
  case "level3" : 
    {
      background(135, 206, 235);
      drawSea();
      if (keyCode == DOWN && keyPressed == true) {
        imgY += anim;
      }
      if (keyCode == UP && keyPressed == true) {
        imgY -= anim;
      }
      if (keyCode == RIGHT && keyPressed == true) {
        imgX += anim;
      }
      if (keyCode == LEFT && keyPressed == true) {
        imgX -= anim;
      }
      useCloud();
      image(pic13, constrain(imgX, 100, width - 100), constrain(imgY, 550, height - 100), 150, 50);
      cloud1 -= 2;
      cloud2 -= 2;
      cloud3 -= 2;
      cloud4 -= 2;
      cloud5 -= 2;
      fill(0, 0, 0);
      textFont(menuFont3);
      text("Survive for 45 seconds!", 830, 50); 
      text(str(timer.time()/1000) + " seconds", 920, 150);
      if (timer.time()/1000 == 45) {
        scene = "transition2";
      }
      text("Get a food score of 150!", 830, 100);
      text("Score: " + food, 930, 200);
      for (int i = 0; i < m.length; i++) {
        m[i].mayfly();
        m[i].move();
      }
      for (int i = 0; i < p.length; i++) {
        p[i].plankton();
        p[i].move();
      }
      for (int i = 0; i < h.length; i++) {
        h[i].heron();
        h[i].move();
      }
      for (int i = 0; i < pi.length; i++) {
        pi[i].pike();
        pi[i].move();
      }
      for (int i = 0; i < m.length; i++) {
        if (dist(imgX, imgY, m[i].x - 37.5, m[i].y) < 35) {
          food += 5;
          m[i].x = random(1000, 3000);
          m[i].y = random(550, 950);
        }
      }
      for (int i = 0; i < p.length; i++) {
        if (dist(imgX, imgY, p[i].x - 17.25, p[i].y) < 35) {
          food += 5;
          p[i].x = random(1000, 3000);
          p[i].y = random(550, 950);
        }
      }
      for (int i = 0; i < h.length; i++) {
        if (dist(imgX, imgY, h[i].x + 129, h[i].y + 101) < 30) {
          scene = "lose";
          lose = "heron!";
          loseImage = pic11;
        }
      }
      for (int i = 0; i < pi.length; i++) {
        if (dist(imgX, imgY, pi[i].x - 200, pi[i].y) < 30) {
          scene = "lose";
          lose = "pike!";
          loseImage = pic12;
        }
      }
      break;
    }
  case "lose" : 
    {
      fill(255, 0, 0, loseTimer);
      rect(500, 500, 1500, 1500);
      fill(0, 0, 0);
      textFont(loseFont);
      text("You became a snack for a " + lose, 500, 400);
      text("Better luck next time...", 500, 800);
      textFont(menuFont);
      text("YOU LOST!!", 500, 300);
      fill(0, 102, 204);
      rect(500, 900, 200, 100, 200);
      textFont(animFont);
      fill(255, 255, 255);
      text("TRY AGAIN", 500, 913);
      image(loseImage, 500, 600);
      if (mouseX > 400 && mouseY > 850 && mouseX < 600 && mouseY < 950) {
        fill(255, 255, 255);
        rect(500, 900, 200, 100, 200);
        textFont(animFont);
        fill(0, 102, 204);
        text("TRY AGAIN", 500, 913);
      } 
      if (mouseX > 400 && mouseY > 850 && mouseX < 600 && mouseY < 950 && mousePressed) {
        scene = "menu";
      }
      loseTimer += 0.2;
      break;
    }
  case "win" : 
    {
    }
  }
}

Tell me if you need the extra tabs. :-h

Tagged:

Answers

Sign In or Register to comment.