How to get this class code work
in
Programming Questions
•
1 year ago
I have the following class code, i want to make this 5 fish, bus its not working. the random values i put in this dont take it for all 5 fish in the class.
i wanted to make the fish turn around as well but it is not turning anyway// like after reflecting the fish is suppose to change the direction. but it is not.. kindly help me out in solving this matter.
Fish myFish , mineFish , hisFish , herFish , goodFish;
class Fish {
int x , y , w , h , r , b , g , s ;
float d , a ,f ;
Fish ( int p1, int p2, int p3, int p4 ,float p5, int p6, int p7 , int p8, int p9, float p10, float p11){
x = p1;
y = p2;
w = p3;
h = p4;
d = p5;
r = p6;
g = p7;
b = p8;
s = p9;
a = p10;
f = p11;
}
}
//useful constants for Assigment 2
final int TANK_LEFT = 100;
final int TANK_RIGHT = 900;
final int WATER_TOP = 300;
final int WATER_BOTTOM = 700;
/*The setup method is the backbone of this program, it
calls 3 drawing procedures in a back-to-front order*/
void setup() {
size(1000, 800);
//random size
int randw = (int)random(30, 50);
int randh = (int)random(randw-20, randw);
//random direction
int randD = (int)random(0, 1); //will be either 0 or 1
//random location
int randx;
int randy;
if (randD==0) { //facing right, adjust for tail
randx = (int)random(TANK_LEFT+randw, TANK_RIGHT-(randw/2));
randy = (int)random(WATER_TOP, WATER_BOTTOM -(randh/2));
}
else { //facing left, adjust for tail
randx = (int)random(TANK_LEFT+(randw/2),TANK_RIGHT-(randw));
randy = (int)random(WATER_TOP, WATER_BOTTOM-(randh/2));
}
float randxangle = random (2*PI);
float randyangle = random (2*PI);
//random colour
int randr = (int)random(0, 256);
int randg = (int)random(0, 256);
int randb = (int)random(0, 256);
drawBackground();
drawForeground();
myFish = new Fish(randx, randy, randw, randh, randD, randr, randg, randb, int (random (3,6)),randxangle, randyangle);
hisFish = new Fish(randx, randy, randw, randh, randD, randr, randg, randb, int (random (3,6)),randxangle, randyangle);
mineFish = new Fish(randx, randy, randw, randh, randD, randr, randg, randb, int (random (3,6)),randxangle, randyangle);
herFish = new Fish(randx, randy, randw, randh, randD, randr, randg, randb, int (random (3,6)),randxangle, randyangle);
goodFish = new Fish(randx, randy, randw, randh, randD, randr, randg, randb, int (random (3,6)),randxangle, randyangle);
}
/*drawBackground() organizes any background drawing that must
be done*/
void drawBackground() {
//wall
background(128, 50, 50);
//floor
fill(100, 56, 0);
rect(0, 500, 1000, 300);
drawTable();
drawAquariumBack();
}
/*The drawTable method draws a 3d table beneath where the
fish tank will go*/
void drawTable() {
//table legs
stroke(0);
fill(160, 116, 60); //shade
//back left leg
rect(100, 750, 25, 15);
triangle(125, 750, 135, 750, 125, 765);
//back right leg
rect(940, 650, 25, 115);
fill(200, 156, 100); //unshaded
quad(965, 765, 975, 740, 975, 600, 965, 600);
//front left leg
rect(50, 750, 25, 50);
fill(160, 116, 60);
quad(75, 750, 85, 750, 85, 800, 75, 800);
//front right leg
fill(200, 156, 100);
rect(900, 750, 25, 50);
quad(925, 700, 935, 700, 935, 800, 925, 800);
//table top
quad(50, 725, 130, 600, 975, 600, 925, 725); //top
rect(50, 725, 875, 25); //front
quad(925, 725, 975, 600, 975, 625, 925, 750); //side
}
/*drawAquariumBack draws the bottom, back, and left panes of
the fish tank.*/
void drawAquariumBack() {
//aquarium
stroke(255);
fill(255, 255, 255, 20); //faint white tint
rect(140, 140, 785, 500); //back
quad(100, 700, 140, 640, 925, 640, 900, 700); //bottom
quad(100, 700, 140, 640, 140, 140, 100, 200);//back-left
}
/*drawSchool organizes the drawing of individual random fish. For
each fish random values for each parameter were generated and
then passed into the drawAFish() method.*/
void draw() {
drawBackground();
drawForeground();
drawFish (myFish);
drawFish (hisFish);
drawFish (mineFish);
drawFish (hisFish);
drawFish (herFish);
moveFish (myFish);
moveFish (mineFish);
moveFish (hisFish);
moveFish (herFish);
moveFish (goodFish);
}
/*drawFish draws a fish based on the provided parameters.
Parameters:
int x - x coordinate of fish (centre of body)
int y - y coordinate of fish
int w - width of fish's body (tail is w/2 more)
int h - height of fish's body and tail
int dir - 0 = right, 1 = left
int r,g,b - fill colour of fish
*/
void drawFish(Fish aFish) {
stroke(0);
fill(aFish.r, aFish.g, aFish.b);
ellipse(aFish.x, aFish.y, aFish.w, aFish.h); //body
int tail; //x-offset for tail's connection to the body
if ((aFish.a<=PI/2) || (aFish.a >=(PI))) { //facing right
tail = -(aFish.w/2); //tail is to the left of the body
//draw an eye on the right side of the head
fill(255);
ellipse(aFish.x+(aFish.w*.25), aFish.y-(aFish.h*.25), 10, 10);
fill(0);
ellipse(aFish.x+(aFish.w*.25), aFish.y-(aFish.h*.25), 5, 5);
}
else { //facing left
tail = (aFish.w/2); //tail is to the right of the body
//draw an eye on the left side of the head
fill(255);
ellipse(aFish.x-(aFish.w*.25), aFish.y-(aFish.h*.25), 10, 10);
fill(0);
ellipse(aFish.x-(aFish.w*.25), aFish.y-(aFish.h*.25), 5, 5);
}
//draw the tail using the offset calculated above
fill(aFish.r, aFish.g, aFish.b);
triangle(aFish.x+tail, aFish.y, (aFish.x+(2*tail)), (aFish.y+(aFish.h/2)), (aFish.x+(2*tail)), (aFish.y-(aFish.h/2)));
}
/*drawForeground organizes all the items that need to be drawn
after the fish*/
void drawForeground() {
drawWater();
drawAquarium();
}
/*drawWater draws 3 panes of water on top of the fish to imitate
a rectangular prism. Water is tinted translucent blue.*/
void drawWater() {
noStroke();
fill(50, 100, 255, 100); //translucent blue
rect(TANK_LEFT, WATER_TOP, TANK_RIGHT - TANK_LEFT, WATER_BOTTOM-WATER_TOP); //front
quad(900, 300, 925, 240, 925, 640, 900, 700);//right
stroke(0, 0, 56); //dark blue surface line
quad(100, 300, 140, 240, 925, 240, 900, 300);//top
}
/*drawAquarium() draws the front and right panes of the aquarium*/
void drawAquarium() {
//aquarium
stroke(255);
fill(255, 255, 255, 20); //faint white tint
rect(100, 200, 800, 500); //front
quad(900, 200, 925, 140, 925, 640, 900, 700);//front-right
noFill();
quad(100, 200, 140, 140, 925, 140, 900, 200); //top
}
void moveFish(Fish aFish){
// move the ball forward
aFish.x = aFish.x + int(aFish.s*cos(aFish.a));
aFish.y = aFish.y + int(aFish.s*sin(aFish.a));
if ((aFish.y + aFish.h >= 690) || (aFish.y - aFish.h <=310))
aFish.a = -aFish.a;
if ((aFish.x + aFish.w >= 890) || (aFish.x - aFish.w <=110))
aFish.a = abs (PI-aFish.a);
}
what should i change to get it work properly.
thanks
1