#include using namespace std; #include "canvas.h" #include "randgen.h" #include "prompt.h" #include "mathutils.h" #include "strutils.h" class FishBouncer : public Bouncer { public: FishBouncer(const Point& p, double angle, double velocity); virtual void updateright(AnimatedCanvas& c, Point& p); virtual void updateleft(AnimatedCanvas& c, Point& p); virtual Shape* clone(); protected: Shape * myLeft; Shape * myRight; }; FishBouncer::FishBouncer(const Point& p, double angle, double velocity) : Bouncer(new EmptyShape(), angle, velocity) { EllipseShape body (Point(10,10), Point(50,30), CanvasColor::YELLOW); EllipseShape bodyb(Point(9,9), Point(51,31), CanvasColor::BLACK); CircleShape eye (Point(40,15), 5, CanvasColor::RED); TriangleShape fin (Point(30,5), Point(30,11), Point(35,11), CanvasColor::BLUE); TriangleShape tail (Point(0,10), Point(0,30), Point(15,20), CanvasColor::GREEN); CompositeShape fish; fish.add(fin); fish.add(tail); fish.add(bodyb); fish.add(body); fish.add(eye); myRight = fish.clone(); myRight->setLocation(p); CompositeShape leftfish; TriangleShape leftfin (Point(30,5), Point(30,11), Point(25,11), CanvasColor::BLUE); TriangleShape lefttail (Point(60,10), Point(60,30), Point(45,20), CanvasColor::GREEN); CircleShape lefteye (Point(20,15), 5, CanvasColor::RED); leftfish.add(leftfin); leftfish.add(lefttail); leftfish.add(bodyb); leftfish.add(body); leftfish.add(lefteye); myLeft = leftfish.clone(); myLeft->setLocation(p); delete myShape; // empty shape, remove it if (PI/2 <= angle && angle <= 3*PI/2) { myShape = myLeft; } else { myShape = myRight; } } void FishBouncer::updateright(AnimatedCanvas& c, Point& p) { myShape = myLeft; myShape->setLocation(p); Bouncer::updateright(c,p); } void FishBouncer::updateleft(AnimatedCanvas& c, Point& p) { myShape = myRight; myShape->setLocation(p); Bouncer::updateleft(c,p); } Shape* FishBouncer::clone() { FishBouncer * fb = new FishBouncer(getLocation(),myAngle,myVelocity); return fb; } int main() { const int WIDTH= 300; const int HEIGHT= 200; AnimatedCanvas display(WIDTH,HEIGHT,20,20); RandGen rgen; display.SetTitle("fish bouncer demo"); int numFish = PromptRange("how many fish: ",1,100); int k; for(k=0; k < numFish; k++) { Point p(WIDTH/2, HEIGHT/2); double angle = deg2rad(rgen.RandInt(-90,90)); FishBouncer fishb(p, angle, rgen.RandInt(2,4)); display.addShape(fishb); angle = deg2rad(rgen.RandInt(90,270)); FishBouncer fishlb(p, angle, rgen.RandInt(2,4)); display.addShape(fishlb); } display.runUntilEscape(10); return 0; }