/*
*/ /* OBJECT-BASED EXERCISE Inside a fish tank, a number of fish swim from left to right, with constant dx (horizontal velocity), and small random variations in dy (vertical velocity). When any fish reaches a boundary (left, top, right, bottom), it "bounces" off (reversing either dx or dy). If mouse is clicked on any fish, it starts again at the left, with a random height. If a fish bumps into another fish, what should it do? They both turn around (reversing dx), and The smaller one swims downward (positive dy), while the bigger one swims upward. Each fish should be a different size and color. Draw each fish as an ellipse, with a triangular "tail" at the "back", and a circular "eye" near the "front". (Be sure to change "front" and "back" depending upon direction, dx.) Start by defining the class Fish . What should it "have" and what should it "do". A fish has the following properties: -- a color (r,g,b) -- a width and height (w,h) -- a position (x,y) -- a speed (dx,dy) A fish does the following (methods): -- display itself (ellipse, triangular tail at the "back", eye at "front"). -- moves normally (dx,dy), after bouncing off boundaries. -- also needs a method to report whether or not a coordinate (x,y) is touching it. */ class Fish { // Fish gotta swim, birds gotta fly ... float x=100,y=100, dx=5,dy=0, w=80,h=30; int r,g,b; // Color String name=""; // METHODS // void show() { // Draw the fish. fill(r,g,b); ellipse( x,y, w,h ); float front=x+w/2, back=x-w/2; triangle( back,y, back-20,y-10, back-20,y+10 ); // Tail fill(255); // eye strokeWeight(1); ellipse( front-15, y-10, 10, 10 ); } void move() { //... dy= dy + random( -1, +1 ); x += dx; y += dy; } boolean isHit( float x, float y ) { // Return true if (x,y) is within this fish! // +++++ STUB - we will code this later. +++++ return false; // ++++ fix this later ++++ } }// END class Fish // //We'll also need a "Button" class, but leave it empty for now. class Button { // Button } //Just make one fish, for now. //See how it swims and bounces, see how it looks, make sure the eye and tail are OK. // GLOBALS: just the objects and the scene (boundaries, etc.) String title="Practice project // Object-based fish."; String news="r to reset; q to quit"; float left=50, top=50, right=550, bottom=350; Button b1; // Fish charlie; // Declare the object // // void setup() { // Setup screen; instantiate object(s). size( 600, 400 ); reset(); } void reset() { // Reset everything. // charlie= new Fish(); charlie.name= "Charlie"; // Instatiate it. // // b1= new Button(); } // void draw() { // Next frame scene(); // background, tank, etc. showAll(); // Draw the fish, buttons, etc. action(); // Move fish, check collisions, etc. } void showAll() { // Show fish, buttons, etc. // charlie.show(); // // b1.show(); } void action() { // Move fish. // charlie.move(); // } void scene() { // Tank, etc. background( 255 ); } ////// EVENT HANDLERS ////// void keyPressed() { //// Handle keys. if (key == 'q') exit(); if (key == 'r') reset(); }