// Cat chases mouse; bombs drop from blimp. // bam: '1A21 // bam: '2222 Watch the cat: add eyes that follow String title= "CST112 bam: '2222 bam/catRatEyes - add head, random arms, eyes that follow"; //// 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=300, 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(800, 600); 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); text( title, 10, height-20 ); 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; xBomb= random( 0.1*width, 0.9*width ); // Random bomb position. } /////////////// 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 (y 0) { //-- text( x%10, x+50,y ); fill(255); triangle( x+40,y-10, x+65,y-15, x+65,y-5 ); stroke(0); line( x+50,y-10, x+65,y-10 ); noStroke(); } //// Hits text. 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 jumops to right, on click. // Check if rat is eaten, else draw it. if ( ! nearCat && isOver( xCat,yCat, xRat,yRat, 50)) { // Use a mouse radius of 50. eatRat(); if (countdown<=0) countdown=20; // Cat eats rat -- start countdown. 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 "mouse (rat). } else { fill( 0, 255, 0 ); drawRat( xRat, yRat ); // Draw "mouse (rat). } drawEye( xRat-10,yRat-40, 16, xCat, yCat); eyeFollow( xRat+10,yRat-40, xCat, yCat); } void eyeFollow( float x1, float y1, float x2, float y2 ) { //// Add an eye at (x1,y1) that follows (x2,y2) stroke(0); fill(255); ellipseMode(CENTER); ellipse(x1,y1, 20,25); noStroke(); //// Make the pupil follow X2,y2 float right,down; //// How far right/left? down/up? right= (x2-x1) / width; // Fraction amount right/left down= (y2-y1) / height; fill(0,0,200); // Blue ellipse(x1+20*right, y1+20*down, 6,6); } void drawEye( float x, float y, float r, float lookX, float lookY ) { //// Draw one eye at (x,y) with radius r. Also make it look. //// Eyes. (r is horizontal radius; make it 2 pixels taller.) //// Add an eye at (x1,y1) that follows (x2,y2) stroke(0); fill(255); ellipseMode(CENTER); ellipse(x,y, r,r+5); noStroke(); //// Make the pupil follow X2,y2 float right,down; //// How far right/left? down/up? right= (lookX-x) / width; // Fraction amount right/left down= (lookY-y) / height; fill(0,0,100); ellipse(x+20*right, y+20*down, r/3,r/3); } void drawRat(float x, float y) { int smaller= (2 * eaten) % 10; // Mouse gets smaller (and lighter) after being eating. 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 eaten! update scores, etc. background(127,127,127, 200); // Flash the background. text( "CAT EATS THE RAT!!!!!", mouseX+10, mouseY-40); }