Making a game in processing - My first project

Hello guys.
I’ve bee fighting a lot now to get the objects spawn right. The game have 3 elements spawning but the problem is to make them spawn after an object is pressed. I tried to use boolean but it won’t work and will just spawn all the elements at same time then. The timer works, but it already starts from the beginning while i try to make it start after i’ve clicked the “continue” button.
My script is a bit janky since i am new, and i am doing this whole project myself even though we are 6 people working on it.

My main page:

AlgaeGroup[] algae;
SinkingObject[] plasticPieces;
SunscreenDrop[] sunscreen = new SunscreenDrop [1];
SwimmingFishes fishes;
CrabMayor crab;
PlankPoints pPoints;
Timer timer;

float xDrop = random(-5);
float yDrop = 350 + random(5);
float dropRadius = 15;

//Pictures
PImage crs1Img;
PImage crs2Img;
PImage crs3Img;
PImage crs4Img;
PImage crs5Img;

PImage plasticImg;
PImage algaeImg;
PImage floaterImg;
PImage crabMayorImg;
PImage plankImg;
PImage fishEyeRightImg;
PImage fishEyeLeftImg;

PImage speechBubble0Img;
PImage continueButtonImg;
PFont pointsFont;

int points = 0;

boolean stage1;
boolean stage2;
boolean stage3;
boolean stage4;

boolean crabGone = false;

int timeIntervalS1Plastics;
int activePlastics;
int plasticsTotal = 50;
boolean plasticsS1;

int timeIntervalS1Algae;
int activeAlgae;
int algaeTotal = 15;
boolean algaeS1;


void setup() {
  size(1280, 720);
  noStroke();
  frameRate(60);
  crs1Img = loadImage("coralReefStage1.png");
  crs2Img = loadImage("coralReefStage2.png");
  crs3Img = loadImage("coralReefStage3.png");
  crs4Img = loadImage("coralReefStage4.png");
  crs5Img = loadImage("coralReefStage5.png");

  plasticImg = loadImage("flaske.png");
  algaeImg = loadImage("algae.png");
  floaterImg = loadImage("floater.png");
  crabMayorImg = loadImage("crabMayor.png");
  plankImg = loadImage("plank.png");
  fishEyeRightImg = loadImage("eyeRight.png");
  fishEyeLeftImg = loadImage("eyeLeft.png");


  speechBubble0Img = loadImage("speechBubble0.png");
  continueButtonImg = loadImage("continueButton.png");

  pointsFont = createFont("almondNougat.ttf", 40);


  fishes = new SwimmingFishes();
  crab = new CrabMayor();
  pPoints = new PlankPoints();

  plasticPieces = new SinkingObject[plasticsTotal];
  for (int i = 0; i < plasticsTotal; i++) {
    plasticPieces[i] = new SinkingObject();
  }

  algae = new AlgaeGroup[algaeTotal];
  for (int i = 0; i < algaeTotal; i++) {
    algae[i] = new AlgaeGroup();
  }

  for (int i = 0; i < sunscreen.length; i++) {
    sunscreen[i] = new SunscreenDrop(xDrop, yDrop, dropRadius);
  }

  //Stages and timer
  stage1 = points <= 9;
  stage2 = points >= 10 && points <= 19;
  stage3 = points >= 20 && points <= 29;
  stage4 = points >= 30 && points <= 39;

  activePlastics = 0;
  activeAlgae = 0;
  
  //crab.crabIsGone();

  if (stage1) {
    timeIntervalS1Plastics = 2000;
    timeIntervalS1Algae = 3000;

    timer = new Timer(timeIntervalS1Plastics);
    timer = new Timer(timeIntervalS1Algae);
    timer.start();
  } else if (stage2) {
  }
}

void draw () {

  for (SunscreenDrop sunscreen : sunscreen)
    sunscreen.moveDrop();


  stage1 = points <= 9;
  stage2 = points >= 10 && points <= 19;
  stage3 = points >= 20 && points <= 29;
  stage4 = points >= 30 && points <= 39;

  if (stage1) {
    background(crs1Img);
  } else if (stage2) {
    background(crs2Img);
  } else if (stage3) {
    background(crs3Img);
  } else if (stage4) {
    background(crs4Img);
  } else {
    background(crs5Img);
  } 

  fishes.display();


  for (SunscreenDrop sunscreen : sunscreen)
    sunscreen.dropDisplay();
  for (SunscreenDrop sunscreen : sunscreen)
    sunscreen.manDisplayAndSpeed();
  for (SunscreenDrop sunscreen : sunscreen)
    sunscreen.disappearBottom();
  for (SunscreenDrop sunscreen : sunscreen)
    sunscreen.disappearOnClick();


  if (stage1) {
    crab.display();
    crab.speaking0();

  } else if (stage2) {
  }  

  algaeS1 = activeAlgae < algaeTotal - 12;
  if (timer.completeS1Algae() == true) {
    if (algaeS1) {
      activeAlgae++;
      timer.start();
    }
  }

  plasticsS1 = activePlastics < plasticsTotal - 43;
  if (timer.completeS1Plastics() == true) {
    if (plasticsS1) {
      activePlastics++;
      timer.start();
    }
  }
  /*plasticsS1 = activePlastics < plasticsTotal - 43;
   if (timer.completeS1Algae() == true || timer.completeS1Plastics() == true){
   if (algaeS1 || plasticsS1) {
   activeAlgae++;
   activePlastics++;
   
   timer.start();
   }*/

  for (int i = 0; i < activeAlgae; i++) {
    algae[i].display();
    algae[i].grow();
    algae[i].overGrowth();
    algae[i].disappearOnClick();
  }


  for (int i = 0; i < activePlastics; i++) {
    plasticPieces[i].display();
    plasticPieces[i].move();
    plasticPieces[i].disappearBottom();
    plasticPieces[i].disappearOnClick();
  }

  pPoints.display();
  /* fill(#000000);
   stroke(200);
   line(0, 550, width, 550);*/
}


void mouseReleased() {
}

the 2 other important classes are:

class CrabMayor {

  float xCrab = width-300, yCrab = height-300;
  float crabSize = 300;

  float xBubble = xCrab - 650, yBubble = yCrab - 200;
  float lBubble = 700, hBubble = 400;

  float xContinue = xBubble * 2, yContinue = yBubble * 2.2;
  float lContinue = 200, hContinue = 50;

  int outOfScreen = 2000;

  boolean overButton = false;
  boolean condition1;
  boolean condition2;
  boolean condition3;
  boolean condition4;



  void display() {
    if (stage1) {
      crabGone = false;
      image(crabMayorImg, xCrab, yCrab, crabSize, crabSize);
    }
  }



  void speaking0() {    
    image(speechBubble0Img, xBubble, yBubble, lBubble, hBubble);
    image(continueButtonImg, xContinue, yContinue, lContinue, hContinue);

    condition1 = mouseX > xContinue;
    condition2 = mouseX < (xContinue + lContinue);
    condition3 =(mouseY < yContinue + hContinue);
    condition4 = (mouseY > yContinue);

    if (condition1 && condition2 && condition3 && condition4) {
      cursor(HAND);
      overButton = true;
    } else {
      cursor(ARROW);
      overButton = false;
    }
    if (mousePressed) {
      if (overButton == true) {
        xCrab = outOfScreen;
        xBubble = outOfScreen;
        xContinue = outOfScreen;
      }
    }
    if (xCrab == outOfScreen) {
      crabGone = true;
    }
  }

  /*void crabIsGone() {
   if (xCrab == outOfScreen) {
   crabGone = true;
   }
   }*/
}

and

class Timer { 
  int startTimeS1Plastics;
  int intervalS1Plastics;

  int startTimeS1Algae;
  int intervalS1Algae;


  Timer(int timeInterval) {
    intervalS1Plastics = timeInterval;
    intervalS1Algae = timeInterval;
  }

  boolean completeS1Plastics() {
    int elapsedTime = millis() - startTimeS1Plastics;
    if (elapsedTime > intervalS1Plastics) {
      return true;
    } else {
      return false;
    }
  } 
  boolean completeS1Algae() {
    int elapsedTime = millis() - startTimeS1Algae;
    if (elapsedTime > intervalS1Algae) {
      return true;
    } else {
      return false;
    }
  }
  
   void start() {
    if (crabGone == true && stage1){
    startTimeS1Plastics = millis();
    startTimeS1Algae = millis();
    }
  }

}

I have tried using this:

void start() {
    if (crabGone == true && stage1){
    startTimeS1Plastics = millis();
    startTimeS1Algae = millis();
    }
  }

but then it spawn everything at same time. if i remove the if statement and just write the startTime variables it works fine, but it begins from the start and not when the statements are true. I am not 100% sure of what the void start() does. I have 5 stages but i just need to get the first done, and then i think i can get the others implemented as well… The other classes are just other objects, which should spawn, but write if they are needed in order to make you understand and try it yourself, i don’t include them since it would make my post much longer.

Oh yeah, and btw. Please explain if you rewrite code or just write code, and sorry for not using capital I every time.

I put the rest of the classes here just in case:

class AlgaeGroup {
  float ypos = 720/6;
  float l = 100;
  float h = 100;
  float state;
  //Algae Random Spawn on x-axis
  float xpos = random(100, 1080);

  //Algae colors
  color lGreen = #00C800, dGreen = #006400;
  color algaeColor = #2f3f03;

  //Mouse over algae
  boolean overAlgae = false;
  boolean onAlgae;
  boolean condition1;
  boolean condition2;
  boolean condition3;
  boolean condition4;

  //Algae max and min scale
  float minLength = 40, minHeight = 40;
  float maxLength = 150, maxHeight = 150;

  //Algae scale size
  float scaleAlgae = 1.1;

  //Algae autogrow
  float hGrow = 0.15;
  float lGrow = 0.15;

  //Algae scale size on click
  float decreaseScale = 1.2;

  //Despawn variable when out of screen
  int outOfScreen = 2000;

  //Points
  float pointSupport0 = 0.05;
  float pointSupport1 = 0.15;
  boolean givePoint = false;
  boolean takePoint = false;


  void display() {
    //Algae(xRandom, y, h, l);
    image(algaeImg, xpos, ypos, h, l);
  }


  void grow() {
    if (h <= maxHeight) { 
      h = h + hGrow;
      xpos = xpos - hGrow;
    }
    if (l <= maxLength) { 
      l = l + lGrow;
    }
  }

  void disappearOnClick() {
    condition1 = mouseX > xpos;
    condition2 = mouseX < (xpos + h);
    condition3 =(mouseY < ypos + l);
    condition4 = (mouseY > ypos);

    if (condition1 && condition2 && condition3 && condition4) {
      cursor(HAND);      
      overAlgae = true;
      if (mousePressed) {
        if (overAlgae == true) {
          loadPixels();
          h = h-decreaseScale;
          l = l-decreaseScale;
          xpos = xpos + decreaseScale/2;
          ypos = ypos + decreaseScale/2;
          overAlgae = false;
        }
      }
    } else {
      cursor(ARROW);
      overAlgae = false;
    }


    //Removes algae when decreased to certain height or length
    if (h <= minHeight + pointSupport0 && l <= minLength + pointSupport0) {
      givePoint = true;
      if (h <= minHeight && l <= minLength){
            xpos = outOfScreen;
      }
      if (givePoint == true && xpos != outOfScreen) {
        points++;
        givePoint = false;
        
      }
    }
  }

  void overGrowth() {
    //Despawn below certain values
    if (h >= maxHeight - pointSupport1 && l >= maxLength - pointSupport1) {
      takePoint = true;
      if (h >= maxHeight && l >= maxLength) {
        xpos = outOfScreen;
      }
      if (takePoint == true && xpos > 100 && xpos < 1100) {
        points--;
        takePoint = false;
      }
    }
  }

}
class AlgaeGroup {
  float ypos = 720/6;
  float l = 100;
  float h = 100;
  float state;
  //Algae Random Spawn on x-axis
  float xpos = random(100, 1080);

  //Algae colors
  color lGreen = #00C800, dGreen = #006400;
  color algaeColor = #2f3f03;

  //Mouse over algae
  boolean overAlgae = false;
  boolean onAlgae;
  boolean condition1;
  boolean condition2;
  boolean condition3;
  boolean condition4;

  //Algae max and min scale
  float minLength = 40, minHeight = 40;
  float maxLength = 150, maxHeight = 150;

  //Algae scale size
  float scaleAlgae = 1.1;

  //Algae autogrow
  float hGrow = 0.15;
  float lGrow = 0.15;

  //Algae scale size on click
  float decreaseScale = 1.2;

  //Despawn variable when out of screen
  int outOfScreen = 2000;

  //Points
  float pointSupport0 = 0.05;
  float pointSupport1 = 0.15;
  boolean givePoint = false;
  boolean takePoint = false;


  void display() {
    //Algae(xRandom, y, h, l);
    image(algaeImg, xpos, ypos, h, l);
  }


  void grow() {
    if (h <= maxHeight) { 
      h = h + hGrow;
      xpos = xpos - hGrow;
    }
    if (l <= maxLength) { 
      l = l + lGrow;
    }
  }

  void disappearOnClick() {
    condition1 = mouseX > xpos;
    condition2 = mouseX < (xpos + h);
    condition3 =(mouseY < ypos + l);
    condition4 = (mouseY > ypos);

    if (condition1 && condition2 && condition3 && condition4) {
      cursor(HAND);      
      overAlgae = true;
      if (mousePressed) {
        if (overAlgae == true) {
          loadPixels();
          h = h-decreaseScale;
          l = l-decreaseScale;
          xpos = xpos + decreaseScale/2;
          ypos = ypos + decreaseScale/2;
          overAlgae = false;
        }
      }
    } else {
      cursor(ARROW);
      overAlgae = false;
    }


    //Removes algae when decreased to certain height or length
    if (h <= minHeight + pointSupport0 && l <= minLength + pointSupport0) {
      givePoint = true;
      if (h <= minHeight && l <= minLength){
            xpos = outOfScreen;
      }
      if (givePoint == true && xpos != outOfScreen) {
        points++;
        givePoint = false;
        
      }
    }
  }

  void overGrowth() {
    //Despawn below certain values
    if (h >= maxHeight - pointSupport1 && l >= maxLength - pointSupport1) {
      takePoint = true;
      if (h >= maxHeight && l >= maxLength) {
        xpos = outOfScreen;
      }
      if (takePoint == true && xpos > 100 && xpos < 1100) {
        points--;
        takePoint = false;
      }
    }
  }

}
class SinkingObject {
  float plasticX = random(150, 1130);
  float plasticY = 720/6;

  float plasticXSpeed = 0;
  float plasticYSpeed = random(0.75,1);

  float plasticHeight = 50;
  float plasticLength = 25;

  //Despawn variable when out of screen
  int outOfScreen = 2000;

  //Mouse over plastic
  boolean overPlastic = false;
  boolean condition1;
  boolean condition2;
  boolean condition3;
  boolean condition4;

  //Despawn line
  int despawnLine = 550;

  //Points
  float pointSupport = 0.8;
  boolean givePoint = false;
  boolean takePoint = false;

  void move() {
    plasticX += plasticXSpeed;
    plasticY += plasticYSpeed;
  }

  void display() {
    image(plasticImg, plasticX, plasticY, plasticLength, plasticHeight);
  }

  void disappearBottom() {
    if (plasticY > despawnLine - plasticHeight - pointSupport) {
      takePoint = true;
      if (plasticY > despawnLine - plasticHeight) {
        plasticX = outOfScreen;
      }
      if (takePoint == true && plasticX != outOfScreen) {
        points--;
        takePoint = false;
      }
    }
  }
  
  
  void disappearOnClick() {
    condition1 = mouseX > plasticX;
    condition2 = mouseX < (plasticX + plasticLength);
    condition3 =(mouseY < plasticY + plasticHeight);
    condition4 = (mouseY > plasticY);

    if (condition1 && condition2 && condition3 && condition4) {
      cursor(HAND);      
      overPlastic = true;
      if (mousePressed) {
        if (overPlastic == true) {
          loadPixels();
          if (plasticX != outOfScreen){
          points++;
        }
          float dist = dist(plasticX, plasticY, mouseX, mouseY);
          if (dist > plasticHeight) {
            overPlastic = false;
            return;
          } else {
            plasticX = outOfScreen;
          }
        }      
      } else {
        cursor(ARROW);
        overPlastic = false;
      }

    }
  }
}
class SunscreenDrop {

  float xMan = 1300, yMan = -50;
  float xManMin = 100, xManMax = 800;
  float manDrop = 400;
  int floaterSize = 300;

  //Variables for the drop
  float xDrop = 200, yDrop = 720/6;
  float dropRadius = 15;
  color sunscreenColor = #FDF8DE;

  float speedDrop = 1.25;

  //Despawn variable when out of screen
  int outOfScreen = 2000;

  //Despawn line
  int despawnLine = 550;

  float xpos, ypos, radius, speed, reset;
  float offset;

  float extra0 = 150;
  float extra1 = 150 + random(5);

  //Points
  float pointSupport = 0.05;
  boolean givePoint = false;
  boolean takePoint = false;

  SunscreenDrop(float xpos, float ypos, float radius) {
    this.xpos = xpos;
    this.ypos = ypos;
    this.radius = radius;
  }

  void manDisplayAndSpeed() {
    
  }
  
  void dropDisplay() {
    noStroke();
    fill(sunscreenColor);

    image(floaterImg, xMan, yMan, floaterSize, floaterSize);
    xMan--;
    
    if (xMan >= xManMin - manDrop && xMan <= xManMax) {
      triangle(xpos-(radius/2), ypos, xpos, ypos-(2*radius), xpos+(radius/2), ypos);
      ellipse(xpos, ypos, radius, radius);
    }
  }



  void moveDrop() {
    speed = speedDrop;
    ypos += speed;

    if (xMan <= xManMin - manDrop && xMan >= xManMax) {
      speed = 50;
    } else {
      speed = speedDrop;
    }
  }

  void disappearBottom() {
    if (ypos >= despawnLine - pointSupport) {
      takePoint = true;
      if (takePoint == true && xpos != outOfScreen && xMan >= 0 - manDrop  && xMan <= xManMax ) {
        points--;
        takePoint = false;
      }
    }   
    if (ypos >= despawnLine) {
        xpos = xMan + extra0;
        ypos = extra1;
      }
   
  }


  boolean overSunscreen = false;

  void disappearOnClick() {
    if (get(mouseX, mouseY) == sunscreenColor) {
      cursor(HAND);      
      overSunscreen = true;
      if (mousePressed) {
        if (overSunscreen == true) {
          loadPixels();
          float dist = dist(xpos, ypos, mouseX, mouseY);
          if (dist > radius) {
            return;
          } else {
            points++;
            xpos = outOfScreen;
          }

      }
    }
  } else {    
  cursor(ARROW);
  overSunscreen = false;
}
}
}
class SwimmingFishes {
  float angle = 1;
  float offset = 350; 
  float scalar = 100; 
  float speed = 0.01; 

  float R = random(255);
  float G = random(255);
  float B = random(255);


  //variables of where my 
  //fish are located 
  float xLoc = -700;
  float yLoc; 

  boolean gLeft, gRight; 
  void display() {
    float y1 = offset + sin(angle) * scalar;
    float y2 = offset + sin(angle + .4) * scalar;
    float y3 = offset + sin(angle + .8) * scalar;
    float y4 = offset + sin(angle + 1.2) * scalar;

    //calling my fish into the draw function 
    fish(0, y1);
    fish(100, y2);
    fish(200, y3);
    fish(300, y4);
    fish(50, y1+50);
    fish(150, y2+50);
    fish(250, y3+50);
    fish(350, y4+50);

    //this sets the angle to go with 
    //the speed in wich the fish look 
    //like they are moving up and down
    angle += speed;
  }

  void fish(float x, float y) {

    //if it equals true it draws the corrisponding code
    if (gLeft==true) {

      pushMatrix();
      xLoc++;
      translate(x, y);
      strokeWeight(1);
      //fin
      fill(R, G, B);
      triangle(xLoc, yLoc, xLoc-40, yLoc+15, xLoc-40, yLoc-10);
      //fishbody
      ellipse (xLoc, yLoc, 50, 20);
      //fisheye
      image(fishEyeRightImg, xLoc+5, yLoc-10, 15, 15);

      popMatrix();
    }
    // if this equals true then it also draws the code
    if (gRight==true) {
      pushMatrix();
      xLoc--;
      translate(x, y); 
      strokeWeight(1);
      //fin
      fill(R, G, B);
      triangle(xLoc, yLoc, xLoc+40, yLoc-15, xLoc+40, yLoc+10);
      //fishbody
      ellipse (xLoc, yLoc, 50, 20);
      //fisheye
      fill(0); 
      image(fishEyeLeftImg, xLoc-15, yLoc-10, 15, 15);


      popMatrix();
    }
    //below are the ifs for when the fish get to a 
    //certain distance and telling wich boolean to be true
    if (xLoc <= -500) {
      gRight = false;
      gLeft = true;
    }

    if (xLoc >= 1400) {
      gLeft = false;
      gRight = true;
    }
  }
}

It would help if you wrote how to reset is as well, since I am trying to make 5 different stages/levels that should spawn different amount of objects at different times, but only if you have got time for that as well ^^

Hi,

Your problem is too specific : you are putting the whole code of your project (lot of classes and methods) therefore it’s difficult for people on the forum to help you. When describing a problem you need to reduce it to simple steps.

In fact your code is using external images that we can’t access therefore running the code will be difficult.

Try the following :

Make a simple Processing sketch where you have a “continue” button and three different objects to spawn (circles with different colors). Then when you click on the button, start a timer and try to display the shapes with the right timing and with the logic that you want.

I’m sure it will be easier to understand after that. :wink:

1 Like

I will be aware of that in the future, thank you.
I kind of fixed so it is all good. Thought i could delete this post but can’t so.

But the game is working now