problem solved.thanks
in
Programming Questions
•
2 years ago
Hi, firstly thank you all for helping me out . I changed my code and posted below.
But new problem came...T^T
1.
My palyerFish eats smaller enemyfish and grows size each time it eats now, but to some kind of enemyfish, Playerfish doest eat, even it is smaller than my playerfish..Maybe something wrong with my collision? Also, I set my palyer fish same as Enemyfish with random sizes..but sometime it is too random that my player fish is soooo small and cant eat any other fishes..
2.I need my playerfish dead and spawn at the bottom of the screen..how can I do that?
3. When the enemyFish is eaten I need to show eating traces for a period of time before the enemyFish is removed.
Teacher gave some hint on this:
"This means that the playerFish should stop colliding with the enemyFish, you can do this by setting a boolean in the enemyFish to false, and once false the playerFish should NOT be checking it's collision with the enemyFish.
When the enemyFish is eaten, you can set an int to equal some number of frames which the traces will remain on screen. If your enemyFish boolean is false, you can call a dead method in your enemyFish class. The method should look like this:
"This means that the playerFish should stop colliding with the enemyFish, you can do this by setting a boolean in the enemyFish to false, and once false the playerFish should NOT be checking it's collision with the enemyFish.
When the enemyFish is eaten, you can set an int to equal some number of frames which the traces will remain on screen. If your enemyFish boolean is false, you can call a dead method in your enemyFish class. The method should look like this:
void dead() {
if (traceTimer > 0) {
traceTimer--; //decrement the trace timer so that it will eventually == 0
y += 2; //traces will sink to the bottom
} else {
fishList.remove(this); //remove the current enemyFish with a traceTimer < 0
spawnNewEnemyFish(); //your enemyFish respawn method
}
}
if (traceTimer > 0) {
traceTimer--; //decrement the trace timer so that it will eventually == 0
y += 2; //traces will sink to the bottom
} else {
fishList.remove(this); //remove the current enemyFish with a traceTimer < 0
spawnNewEnemyFish(); //your enemyFish respawn method
}
}
"
He says my eat method should change to this:
void eat(Fish other) {
if (fWidth > other.fWidth) {
fishScale *= 1.1;
fWidth = bSize * fishScale;
//change this next line so that the other fish alive is false
//and do the traces before removing from the arrayList
fishList.remove(other);
}
else {
//change this next line so that the alive is false
//and do the traces, call dead method from main draw loop
player.dead();
}
}
if (fWidth > other.fWidth) {
fishScale *= 1.1;
fWidth = bSize * fishScale;
//change this next line so that the other fish alive is false
//and do the traces before removing from the arrayList
fishList.remove(other);
}
else {
//change this next line so that the alive is false
//and do the traces, call dead method from main draw loop
player.dead();
}
}
But I am still confusing

Here are my codes:
int numFishes = 8;
ArrayList<EnemyFish> fishList = new ArrayList<EnemyFish>();
PlayerFish player;
boolean keyUp, keyDown, keyLeft, keyRight;
void setup() {
size(800, 800);
player = new PlayerFish();
noStroke();
for (int i = 0; i < numFishes; i++) {
fishList.add(new EnemyFish());
}
keyUp = keyDown = keyLeft = keyRight = false;
}
void keyPressed() {
if (key == 'w') keyUp = true;
if (key == 's') keyDown = true;
if (key == 'a') keyLeft = true;
if (key == 'd') keyRight = true;
}
ArrayList<EnemyFish> fishList = new ArrayList<EnemyFish>();
PlayerFish player;
boolean keyUp, keyDown, keyLeft, keyRight;
void setup() {
size(800, 800);
player = new PlayerFish();
noStroke();
for (int i = 0; i < numFishes; i++) {
fishList.add(new EnemyFish());
}
keyUp = keyDown = keyLeft = keyRight = false;
}
void keyPressed() {
if (key == 'w') keyUp = true;
if (key == 's') keyDown = true;
if (key == 'a') keyLeft = true;
if (key == 'd') keyRight = true;
}
void keyReleased() {
if (key == 'w') keyUp = false;
if (key == 's') keyDown = false;
if (key == 'a') keyLeft = false;
if (key == 'd') keyRight = false;
}
void draw() {
background(62, 188, 202);
fill(62, 188, 202, 60);
if (keyUp) player.update(0, -1);
if (keyDown) player.update(0, 1);
if (keyLeft) player.update(-1, 0);
if (keyRight) player.update(1, 0);
player.update();
player.walls();
player.drawMe();
for (int i = 0; i < fishList.size(); i++) {
EnemyFish fishi = fishList.get(i);
if (fishi.alive) {
if (player.collision(fishi) && player.headOn(fishi)) {
player.eat(fishi);
}
for (int j = i+1; j < fishList.size(); j++) {
EnemyFish fishj = fishList.get(j);
if ( fishi.collision( fishj )) { //GeneralFish collision method
fishi.bounce( fishj ); //bounce the fish
}
}//for j
fishi.update();
fishi.walls(fishList);
} else {
fishi.dead();
}
fishi.drawMe();
}//for i
}//for draw
class Fish {
float x, y, velX, velY, fishScale, fWidth;
int bSize;
color c;
Fish() {
x = random(width);
y = random(height);
velX = random(-2, 2);
velY = random(-2, 2);
bSize = 150;
fishScale = random(0.3, 0.9);
fWidth = bSize * fishScale;
}
//a method for collision
boolean collision(Fish other) {
return dist(x, y, other.x, other.y) < fWidth/2 + other.fWidth/2;
}
void update() {
x += velX;
y += velY;
}
void dead(){
} // To be defined
}//class
if (key == 'w') keyUp = false;
if (key == 's') keyDown = false;
if (key == 'a') keyLeft = false;
if (key == 'd') keyRight = false;
}
void draw() {
background(62, 188, 202);
fill(62, 188, 202, 60);
if (keyUp) player.update(0, -1);
if (keyDown) player.update(0, 1);
if (keyLeft) player.update(-1, 0);
if (keyRight) player.update(1, 0);
player.update();
player.walls();
player.drawMe();
for (int i = 0; i < fishList.size(); i++) {
EnemyFish fishi = fishList.get(i);
if (fishi.alive) {
if (player.collision(fishi) && player.headOn(fishi)) {
player.eat(fishi);
}
for (int j = i+1; j < fishList.size(); j++) {
EnemyFish fishj = fishList.get(j);
if ( fishi.collision( fishj )) { //GeneralFish collision method
fishi.bounce( fishj ); //bounce the fish
}
}//for j
fishi.update();
fishi.walls(fishList);
} else {
fishi.dead();
}
fishi.drawMe();
}//for i
}//for draw
class Fish {
float x, y, velX, velY, fishScale, fWidth;
int bSize;
color c;
Fish() {
x = random(width);
y = random(height);
velX = random(-2, 2);
velY = random(-2, 2);
bSize = 150;
fishScale = random(0.3, 0.9);
fWidth = bSize * fishScale;
}
//a method for collision
boolean collision(Fish other) {
return dist(x, y, other.x, other.y) < fWidth/2 + other.fWidth/2;
}
void update() {
x += velX;
y += velY;
}
void dead(){
} // To be defined
}//class
class PlayerFish extends Fish {
PlayerFish() {
super();
fishScale = random(0.3, 0.9);
}
void update() {
velX *= 0.92;
velY *= 0.92;
super.update();
}
void update(float xDir, float yDir) {
velX += xDir;
velY += yDir;
}
boolean headOn(Fish other) {
return velX > 0 && other.velX < 0 || velX < 0 && other.velX > 0;
}
void eat(Fish other) {
if (fWidth > other.fWidth) {
fishScale *= 1.15;
other.dead();
} else {
player.dead();
}
}
void dead(){
fill(0,0,0);
rect(0,0,800,800);
}
void walls() {
//right wall
if (x > width - bSize/4) {
x = width - bSize/4;
velX *= -1;
}
//left wall
if (x < bSize/4) {
x = bSize/4;
velX *= -1;
}
//bottom wall
if (y > height - bSize/4) {
y = height - bSize/4;
velY *= -1;
}
//top wall
if (y < bSize/4) {
y = bSize/4;
velY *= -1;
}
}
void drawMe() {
pushMatrix();
translate(x, y);
scale(fishScale);
if (velX > 0) {
scale(-1, 1);
}
noStroke();
strokeWeight(1);
fill(255, 115, 85);
PlayerFish() {
super();
fishScale = random(0.3, 0.9);
}
void update() {
velX *= 0.92;
velY *= 0.92;
super.update();
}
void update(float xDir, float yDir) {
velX += xDir;
velY += yDir;
}
boolean headOn(Fish other) {
return velX > 0 && other.velX < 0 || velX < 0 && other.velX > 0;
}
void eat(Fish other) {
if (fWidth > other.fWidth) {
fishScale *= 1.15;
other.dead();
} else {
player.dead();
}
}
void dead(){
fill(0,0,0);
rect(0,0,800,800);
}
void walls() {
//right wall
if (x > width - bSize/4) {
x = width - bSize/4;
velX *= -1;
}
//left wall
if (x < bSize/4) {
x = bSize/4;
velX *= -1;
}
//bottom wall
if (y > height - bSize/4) {
y = height - bSize/4;
velY *= -1;
}
//top wall
if (y < bSize/4) {
y = bSize/4;
velY *= -1;
}
}
void drawMe() {
pushMatrix();
translate(x, y);
scale(fishScale);
if (velX > 0) {
scale(-1, 1);
}
noStroke();
strokeWeight(1);
fill(255, 115, 85);
quad(-15, -20, 45, 0, -15, 30, -15, 30);
stroke(2);
fill(255, 115, 85);
curve(-100, 26, -15, -20, 45, 0, 73, 61);
curve(73, -50, 45, 0, -15, 30, 15, -25);
curve(600, -100, -15, -20, -15, 30, 73, -50);
triangle(45, 0, 55, 10, 65, -10);
stroke(2);
fill(255, 115, 85);
curve(-100, 26, -15, -20, 45, 0, 73, 61);
curve(73, -50, 45, 0, -15, 30, 15, -25);
curve(600, -100, -15, -20, -15, 30, 73, -50);
triangle(45, 0, 55, 10, 65, -10);
noStroke();
fill(62, 188, 202);
// triangle(-20,15,-36,25,-56,0);
//eye
strokeWeight(1);
stroke(2);
fill(255, 255, 255);
ellipse(-25, 0, 15, 15);
fill(0, 0, 0);
ellipse(-25, 0, 8, 8);
popMatrix();
}
}//end class
}
}//end class
class EnemyFish extends Fish {
float wave, waveSpeed, amp;
boolean alive;
int traceTimer;
int enemyBound;
color c;
EnemyFish() {
super();
alive= true;
wave = random(TWO_PI);
waveSpeed = random(0.1, 0.5);
amp = random(1, 4);
enemyBound=50;
fWidth = bSize * fishScale;
fishScale = random(0.3, 0.9);
c = color( random(255), random(10), 150);
}
EnemyFish(float xpos, float ypos, float velXa, float velYa) {
super();
x=xpos;
y=ypos;
velX=velXa;
velX=velYa;
alive= true;
wave = random(TWO_PI);
waveSpeed = random(0.1, 0.5);
amp = random(1, 4);
enemyBound=50;
fWidth = bSize * fishScale;
c = color( random(255), random(10), 150);
}
void update() {
wave += waveSpeed;
y += amp*sin(wave);
super.update();
}
void bounce(Fish other) {
float angle = atan2(y - other.y, x - other.x);
velX = 2 * cos(angle);
velY = 2 * sin(angle);
other.velX = 2 * cos(angle - PI);
other.velY = 2 * sin(angle - PI);
}
void dead() {
if (traceTimer > 0) {
traceTimer--;
y +=2;
} else {
fishList.remove(this);
spawnNewEnemyFish(fishList);
}
}
void spawnNewEnemyFish(ArrayList fishList) {
if (random(0, 2) < 1) {
fishList.add(new EnemyFish(-25, random(100, height-100), random(1, 4), random(-2, 2)));
} else {
fishList.add(new EnemyFish(width+25, random(100, height-100), random(-1, -4), random(-2, 2)));
}
}
//change this method for enemyFish wall collision
void walls(ArrayList fishList) {
//right wall
if (x > width + enemyBound) {
fishList.remove(this);
spawnNewEnemyFish(fishList);
}
float wave, waveSpeed, amp;
boolean alive;
int traceTimer;
int enemyBound;
color c;
EnemyFish() {
super();
alive= true;
wave = random(TWO_PI);
waveSpeed = random(0.1, 0.5);
amp = random(1, 4);
enemyBound=50;
fWidth = bSize * fishScale;
fishScale = random(0.3, 0.9);
c = color( random(255), random(10), 150);
}
EnemyFish(float xpos, float ypos, float velXa, float velYa) {
super();
x=xpos;
y=ypos;
velX=velXa;
velX=velYa;
alive= true;
wave = random(TWO_PI);
waveSpeed = random(0.1, 0.5);
amp = random(1, 4);
enemyBound=50;
fWidth = bSize * fishScale;
c = color( random(255), random(10), 150);
}
void update() {
wave += waveSpeed;
y += amp*sin(wave);
super.update();
}
void bounce(Fish other) {
float angle = atan2(y - other.y, x - other.x);
velX = 2 * cos(angle);
velY = 2 * sin(angle);
other.velX = 2 * cos(angle - PI);
other.velY = 2 * sin(angle - PI);
}
void dead() {
if (traceTimer > 0) {
traceTimer--;
y +=2;
} else {
fishList.remove(this);
spawnNewEnemyFish(fishList);
}
}
void spawnNewEnemyFish(ArrayList fishList) {
if (random(0, 2) < 1) {
fishList.add(new EnemyFish(-25, random(100, height-100), random(1, 4), random(-2, 2)));
} else {
fishList.add(new EnemyFish(width+25, random(100, height-100), random(-1, -4), random(-2, 2)));
}
}
//change this method for enemyFish wall collision
void walls(ArrayList fishList) {
//right wall
if (x > width + enemyBound) {
fishList.remove(this);
spawnNewEnemyFish(fishList);
}
if ( x <- enemyBound) {
fishList.remove(this);
spawnNewEnemyFish(fishList);
}
//bottom wall
if (y > height - bSize/4) {
y = height - bSize/4;
velY *= -1;
}
//top wall
if (y < bSize/4) {
y = bSize/4;
velY *= -1;
}
}
void drawMe() {
pushMatrix();
translate(x, y);
scale(fishScale);
if (velX<0) {
scale(-1, 1);
}
//fish
fill(c);
arc(30, 0, 60, 30, -PI/2, PI/2);
quad(30, -15, 30, 15, -50, 5, -50, -19);
quad(-50, -10, -50, 5, -70, 10, -69, 8);
stroke(3);
noFill();
curve(300, 100, 35, -15, -70, 35, 100, 270);
fill(0, 0, 0);
ellipse(40, -5, 6, 6);//eye
fill(229, 190, 157);
triangle(-66, 35, -40, 50, -40, 60);//tail
triangle(-70, 33, -40, 70, -40, 90);//tail
triangle(30-10, 30-16, 30-40, 30-20, 30-35, 30-10);//down fin
curve(30-20, 50, 30-80, 30-40, -30, 30-48, 30+20, 30+20);
curve(30*0, 0, 30-53, 30-48, 30-40, 30-48, 30, 30+10);
popMatrix();
}
}
fishList.remove(this);
spawnNewEnemyFish(fishList);
}
//bottom wall
if (y > height - bSize/4) {
y = height - bSize/4;
velY *= -1;
}
//top wall
if (y < bSize/4) {
y = bSize/4;
velY *= -1;
}
}
void drawMe() {
pushMatrix();
translate(x, y);
scale(fishScale);
if (velX<0) {
scale(-1, 1);
}
//fish
fill(c);
arc(30, 0, 60, 30, -PI/2, PI/2);
quad(30, -15, 30, 15, -50, 5, -50, -19);
quad(-50, -10, -50, 5, -70, 10, -69, 8);
stroke(3);
noFill();
curve(300, 100, 35, -15, -70, 35, 100, 270);
fill(0, 0, 0);
ellipse(40, -5, 6, 6);//eye
fill(229, 190, 157);
triangle(-66, 35, -40, 50, -40, 60);//tail
triangle(-70, 33, -40, 70, -40, 90);//tail
triangle(30-10, 30-16, 30-40, 30-20, 30-35, 30-10);//down fin
curve(30-20, 50, 30-80, 30-40, -30, 30-48, 30+20, 30+20);
curve(30*0, 0, 30-53, 30-48, 30-40, 30-48, 30, 30+10);
popMatrix();
}
}
1