I don’t know if it’s ok to ask another question under the same topic, but my program just run into a new error.
So I am trying to let users catch the fish, and every time they catch one, I created a scoreboard to count. But somehow the number on my score doesn’t add when they catch a new fish, but I feel like there is no flaw in my syntax.
PImage fishpic;
PImage fishpsa;
PImage fishpic2;
PImage user;
PImage calendar;
Fish [] groupers = new Fish[20];
void setup() {
size(1000, 1000);
stroke(10);
strokeWeight(6);
line(0, 600, 800, 600);
fishpic = loadImage("grouper.png");
imageMode(CENTER);
fishpic.resize(0, 70);
fishpic2 = loadImage("grouper2.png");
fishpic2.resize(0, 70);
fishpsa = loadImage("public service announcement.jpg");
fishpsa.resize(0, 240);
user = loadImage("user.png");
user.resize(0, 240);
calendar = loadImage("calendar.png");
calendar.resize(1000, 70);
for (int i = 0; i<groupers.length; i++) {
groupers[i] = new Fish(50);
}
}
void draw() {
background(255);
strokeWeight(0);
rect(0, 300, 1000, 700);
fill(67, 42, 0);
rect(0, 60, 800, 240);
fill(232, 176, 146);
image(fishpsa, 840, 180);
image(user, 90, 180);
fill(55, 122, 216);
image(calendar, 500, 25);
line(350, 0, 350, 59);
line(835, 0, 835, 59);
for (int i = 0; i<groupers.length; i++) {
groupers[i].display();
groupers[i].fishmoving();
groupers[i].turn();
groupers[i].timing();
groupers[i].remonth();
groupers[i].numberOFfish();
}
}
void mousePressed() {
for (int i = 0; i<groupers.length; i++) {
groupers[i].catching();
}
}
class Fish {
float x;
float y;
float xSpeed;
float diameter;
boolean fishMovingLeft = false;
float t = 0;
float tSpeed = 2;
int fishnumber = 0;
Fish(float Medium) {
x=random(500);
y=random(350, 920);
xSpeed =random(1, 10);
diameter = Medium;
}
void display() {
if (fishMovingLeft)
image(fishpic, x, y);
else
image(fishpic2, x, y);
}
void fishmoving() {
x = x + xSpeed;
}
void turn() {
if (x+70 > width ) {
xSpeed = abs(xSpeed) * -1;
fishMovingLeft = true;
}
if ( x-65 < 0 ) {
xSpeed = abs(xSpeed);
fishMovingLeft = false;
}
}
void timing() {
strokeWeight(1);
rect(t, 0, 1, 60);
t = t + tSpeed;
}
void remonth (){
if (t > width) {
t = 0;
}
}
void numberOFfish() {
fill(55, 122, 216);
text("Can you catch 20 fish?", 250, 150);
textSize(30);
text("Fish: " + fishnumber, 250, 200);
}
void catching() {
float distance = sqrt((x-mouseX)*(x-mouseX)+(y-mouseY)*(y-mouseY));
if (distance<=20 && ((t > 350)&&(t<835))) {
x = -50;
y = -50;
fishnumber = fishnumber +1;
}
}
}