////// CST112 BAM "catRat" -- Cat chases mouse; bombs drop from blimp. // bam '1A21 Cat chases mouse; bombs drop from blimp. // bam '2222 Watch the cat: add eyes that follow // bam '2229 Pictures for cat. Random shaking of rat. PImage p1, pcat, qcat; int frameNumber=0; //// 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= 0.5 * 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; p1= loadImage( "Blue hills.jpg" ); pcat= loadImage("232american bobtail.ashx.jpeg"); qcat= loadImage("232american bobtail kitten.ashx.jpeg"); } void draw() { // Move cat, mouse, bomb; check hits; draw everything. frameNumber++; background( 127, 255, 127 ); image(p1, 0,0.1*height, width, 0.75*height ); // 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; 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) image(pcat, x-40, y-20, 80-hits+20, 40-hits+10); else image(qcat, x-50, y-20, 80-hits+20, 40-hits+10); rect( x+40, y-20, 40, 30); // Head triangle( x+20, y-30, x+25, y-45, x+35, y-30); triangle( x+25, y-30, x+30, y-45, x+40, y-35); //// Eyes and mouth eyeFollow( x+35, y-25, xRat, yRat); fill(255, 0, 0); triangle( x+35, y-12, x+60, y-17, x+60, y-3 ); //// Show teeth, based on x coordinate / 10 int teeth= (int) x /10; if (teeth % 5 > 0) { //-- text( x%10, x+50,y ); fill(255); triangle( x+40, y-10, x+60, y-15, x+60, y-5 ); } //// 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). } eyeFollow( xRat-10, yRat-40, 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, 100); ellipse(x1+20*right, y1+20*down, 5, 5); } void drawRat(float x, float y) { int smaller= (2 * eaten) % 10; // Mouse gets smaller (and lighter) after being eating. x = x -10 + random(20); y = y -6 + random(12); 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); }