// Cat chases mouse; bombs drop from blimp. // bam: '1A21 // Cat chases mouse; bombs drop from blimp. // bam: '1A21 //// GLOABAL VARIABLES //// int score=0; // Score increases by 10 for 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, yBomb=0; // Blimp & bomb are halfway across the screen. float vBomb=0; // Velocity of bomb (downward) -- increases with acceleration of gravity. // float frameRate=30.0; float gravity= 9.81 / frameRate; // Acceleration of gravity is 9.81 meters per second. 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 ); //// 1. Blimp and bomb. updateBomb(); //// 2. Cat. updateCat(); // 3. 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); if (score != 0) text( "SCORE: " + score, 400, 30); // Display the score (if > 10); } void updateBomb() { // Handle the blimp and bomb, xBomb=width/2; // First, draw blimp and accelerate the bomb. drawBlimp( xBomb, yBomb ); // Draw blimp (containin the bomb). moveBomb(); // Accelerate the bomb. drawBomb( xBomb, yBomb ); // Draw bomb, unless it is off the screen. //-- 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. (Acceleration of gravity is 10 m/s. Let 1o pixels = 10 meters. if (yBomb < height) { // Accelerate bomb, unless it is off the screen. vBomb= vBomb + gravity; yBomb = yBomb + vBomb; } } void drawBomb(float x, float y) { if (y < height) { // Draw bomb, unless it is off the screen. 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 (y0) 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 eaten! update scores, etc. background(127,127,127, 200); // Flash the background. text( "CAT EATS THE RAT!!!!!", mouseX+10, mouseY-40); }