//Cat chases mouse bombs drop from pipe //GLOBALS// int score = 0; //Score increases by 10 w/ each rat eaten int eaten = 0; //Number of rats eaten int hits = 0; //Number of bombs hitting cat int countdown = 0; //Countdown for cat eating rat float xCat = 100, yCat = 200; //Position of the cat float xRat = 50, yRat = 50; //Position of the mouse ("rat") float xBomb = width / 2; float yBomb = 0; //Blimp & bomb are halfway across the screen //Velocity of bomb(down)--ups with accel of gravity float vBomb = 0; //float frameRate=30.0; //Accel of gravity is 9.81 m/s float gravity = 9.81 / frameRate; boolean hitCat = false; boolean nearCat = false; void setup ( ) { //Big screen initialize size ( 640, 888 ); rectMode ( CENTER ); ellipseMode ( CENTER ); //frameRate = 1; } void draw ( ) { //Move cat, mouse, bomb; check hits; draw everything background ( 127, 255, 127 ); //A grassy field fill ( 255, 0, 255 ); updateScore ( score ); //Blimp and bomb updateBomb ( ); //Cat updateCat ( ); //Rat updateRat ( mouseX, mouseY ); //Rat follows mouse } boolean isOver ( float x1, float y1, float x2, float y2, float margin ) { //Return true if 1 is over 2 return ( abs ( x1 - x2 ) < margin ) && ( abs ( y1 - y2 ) < margin ); /* boolean result; result = ( abs ( x1 - x2 ) < margin ) && ( abs ( y1 - y2 ) < margin ) ); return result; if ( abs ( x1 - x2 ) < margin && abs ( y1 - y2 ) < margin ) { return true; //True if within this margin } else { return false; } */ } void keyPressed ( ){ yBomb = 1; //Bomb starts dropping when y>0 vBomb = 0; } //MOVINDG & DRAWING METHODS void updateScore ( int n ) { text ( "Press any key to drop bomb", 400, 10); fill ( 0 ); if ( score < 0 ) fill ( 255, 0, 0 ); //Display the score if > 10 if ( score ! = 0 ) text ( "SCORE:" + score, 400, 30 ); } void updateBomb ( ) { //Handle the blimp and bomb xBomb = width / 2; //First draw blimp and accelerate the bomb drawBlimp ( xBomb, yBomb ); //Draw blimp (contains the bomb) moveBomb ( ); //Accelerate the bomb drawBomb ( xBomb, yBomb ); //Draw bomb unless it's offscreen //-- drawBomb ( 100, 100 );//debugging: dislay static bomb } void drawBlimp ( float x, float y ) { fill ( 0, 255, 255 ); rect ( xBomb, 20, 100, 40 ); } void moveBomb ( ) { //Make the bomb fall Let 1o pixels=10m if ( yBomb < height ) { //Accel bomb unless it's offscreen vBomb = vBomb + gravity; yBomb = yBomb + vBomb; } } void drawBomb ( float x, float y ) { if ( y < height ) { //Draw bomb unless it's offscreen fill ( 0, 127, 127 ); rect ( x, y, 30, 65 ); fill ( 255 ); text ( "B\nO\nM\nB", x, y - 20 ); fill ( 0 ); text ( "" + key, x, y + 45 ); fill ( 255 ); if ( y < height ) text ( vBomb, 400, 60 ); } } void updateCat ( ) { //Handle the cat if ( hitCat ) { //Cat was by bomb so move it offscreen xCat = -20; yCat = -20; //Move cat offscreen score -= 20; //Cat loses 20 points hits++; } moveCat ( ); //Chase the rat //Check if bomb near/hit cat nearCat = isOver ( xBomb, yBomb, xCat, yCat, 150 ); //Check if cat hit hitCat = isOver ( xBomb, yBomb, xCat, yCat, 75 ); drawCat ( xCat, yCat ); //Draw cat } void moveCat ( ) { //Cat chases mouse float xFar = xRat - xCat; float yFar = yRat - yCat; //Close the distance by half xCat = xCat + xFar / 60; yCat = yCat + yFar / 60; } void drawCat ( float x, float y ) { //Check if bomb is over the cat--if so,change cat color if ( hitCat ) { //BOMB HIT CAT! Make cat blue (this frame) fill ( 0, 0, 255 ); } else if( nearCat ) { //BOMB is near so cat is scared fill ( 255, 255, 0); //Make scared cat yellow+harmless } else { //Cat = darker w/ hit but lighter w/ eat int darker = 10 * hits; int lighter = 10 * eaten; fill ( 200 + lighter, 100 - darker, 100 - darker ); } //draw a cat at x,y noStroke ( ); rect ( x, y, 80 - hits, 40 - hits ); rect ( x + 40, y - 20, 30, 20 ); triangle ( x + 25, y - 30, x + 30, y - 40, x + 35, y - 30 ); triangle ( x + 30, y - 30, x + 35, y - 40, x + 40, y - 30 ); fill ( 0 ); text ( "CAT \n #" + hits, x - 20, y ); } void updateRat ( float x, float y ) { xRat = x; yRat = y; //Move rat to new x,y if ( mousePressed ) xRat += 50; //Rat jumps to right on click //Check if rat is eaten else draw it //Use a mouse radius of 50 if ( ! near Cat && isOver ( xCat, yCat, xRat, yRat, 50 ) ) { eatRat ( ); //Cat eats rat so start countdown if ( countdown <= 0 ) countdown = 20; countdown--; if ( countdown == 0 ) { //Countdown finished xCat = -20; yCat = -20; //Move cat off-screen score += 10; eaten++; } fill ( 0, 127, 0 ); drawRat ( xRat - 10 + countdown, yRat - 10 + countdown );//Draw rat } else { fill ( 0, 255, 0 ); drawRat ( xRat, yRat ); //Draw rat } } void drawRat ( float x, float y ) { //Mouse gets smaller + lighter aft eat int smaller = ( 2 * eaten ) %10; if ( countdown > 0 ) smaller = -2 * countdown; rect ( x - 20, y - 20, 60 - smaller, 30 - smaller / 2 ); rect ( x - 60, y - 30, 20, 5 ); fill ( 255, 0, 0 ); text ( "RAT \n #" + eaten, x - 40, y - 20 ); } void eatRat ( ) { //Rat was eat so update scores etc background ( 127, 127, 127, 200 ); //Flash background text ( "CAT CAUGHT RAT", mouseX + 10, mouseY - 40 ); }