//// class definition and basic functions for CST-112 FINAL EXAM //// String author="Jennifer Blomberg"; String title="A N I M A L K I N G D O M"; String subtitle=" Press the ? key, for the instructions"; String news="e"; int newsCountdown=0; int score=0; //////// CLASS DEFINITION //////// class Animal { //// Animal has a body, an optional head, legs, pairs of arms, etc. //// Animal is wide and tall //W,T;// and has other properties such as color. //// Also: position (x,y) and velocity (dx,dy). //// Desription and drawing info. float wide = 30, tall = 50, head = 0, tail = 0; int legs = 2 , armpairs = 6; String name=""; int r,g,b; // Color. int shape=4; //// Position, velocity, boundaries float x=400,y=300, dx=0,dy=0; float left=50, top=50, right=750, bottom=550; //// CONSTRUCTORS (and set functions) //// Animal() { //// Default Constructor //// } Animal( float wide, float tall) { //// 2-arg Constructor //// this.wide= wide; this.tall= tall; } Animal( float wide, float tall, int legs, int armpairs ) { //// 4-arg Constructor //// this.wide= wide; this.tall= tall; this.legs= legs; this.armpairs= armpairs; } void setRGB( int r, int g, int b) { this.r= r; this.g= g; this.b= b; } void randomRGB() { setRGB( int(random(255)), int(random(255)), int(random(255)) ); } void randomXY() { x= random(100,700); y= random(100,500); dx= random(1.5,2.5); dy= random(0.5,1.5); } //// METHODS to move and show //// void move() { //// Check for boundaries, and move to new coordinates (x+dx, y+dy). if (xright) dx= -dx; if (ybottom) dy= -dy; //// x += dx; y += dy; } void show() { //// Draw the Animal on the screen //// rectMode( CENTER ); ellipseMode( CENTER ); fill(r,g,b); //// Front & back depend on dx //// float front=x+wide/2, rear=x-wide/2; if (dx<0) { front= x-wide/2; rear= x+wide/2; } //// Shape //// if (shape == 0) { ellipse( x, y, wide, tall ); } else if (shape == 4) { rect( x, y, wide, tall ); } else if (shape == 3) { triangle( front,y, rear,y-tall/2, rear,y+tall/2 ); } else if (shape == 2) { } else { } //// Head /// CHANGE THE LOCATION OF THE HEAD TO THE CENTER OF ZOOG /// if (head>0) { // Head (if any) // ellipseMode( CENTER ); ellipse( front, y-tall/2 - head/2, head, head ); // Head at front rect( rear, y-tall/2, 20, 6 ); // Tail at rear } limbs(); //// Name fill( 256-r, 256-g, 256-b ); text( name, x-wide/4, y-tall/4 ); } void limbs() { //// Legs & Arms //// float legX= x - wide/2; // First leg is at the left float legY= y + tall/2; // Legs start at bottom of rectangle. if (legs > 0) { line( legX,legY, legX,legY+20 ); // First leg. } if (legs > 1) { float legSpacing= wide / (legs-1); for (int i=1; i this.x + this.wide/2) return false; if (y < this.y - this.tall/2) return false; if (y > this.y + this.tall/2) return false; return true; } boolean hit( Animal other ) { //// Return true iff other hits this one. return hit( other.x, other.y ); } } //////// GLOBAL DECLARATIONS //////// Animal octopus, zoog, cat, bird; int maxblobs=50, numblobs=3; Animal[] blobs= new Animal[maxblobs]; // Create array for blobs; //////// SKETCH FUNCTIONS //////// void setup() { //// Setup. size( 800, 600 ); reset(); } void reset() { //// initialize the menagarie. cat= new Animal( 50, 30, 4, 0 );// cat properties // cat.setRGB( 200,150,50 ); cat.head=20; cat.x=100; cat.name= "cat"; // zoog= new Animal( 30, 80, 0, 8 ); zoog.setRGB( 200,175,175 ); zoog.head=30; zoog.x= 200; zoog.y= 200; zoog.name= "Z\nO\nO\nG"; // octopus= new Animal( 60, 80, 8, 0 ); octopus.setRGB( 150,50,150 ); octopus.y= 100; octopus.dx=0.25; // Fast up & down octopus.dy=5; octopus.name= "O\n c\n t\n o\n p\n u\n s"; // bird= new Animal( 50, 15, 0, 0 ); bird.shape= 3; bird.setRGB( 50,50,250 ); bird.head=12; // (eye) bird.x= 300; bird.y= 300; bird.dx= 12; bird.dy= .5; // Blobs have random sizes, locations, and colors for (int i=0; i 0) { text( news, width/2, height-30 ); fill( 255,0,0); } else { news=""; } } textSize( 12 ); text( author, 20, height-20 ); // Author's name at lower left. } void makeNews( String s ) { //// Make news; set timeout. news= s; newsCountdown=60; } void help() { //// Display instructions //// fill(0); textSize( 15 ); int j=0, k=20; text( "? for help", 100, 100+k*j++ ); text( "q to quit", 100, 100+k*j++ ); text( "r to reset all animals", 100, 100+k*j++ ); text( "c to start cat", 100, 100+k*j++ ); text( "z to start zoog", 100, 100+k*j++ ); text( "", 100, 100+k*j++ ); text( "space-key for more blobs", 100, 100+k*j++ ); text( "0 for none / # for "+ maxblobs, 100, 100+k*j++ ); text( "/ = to place blobs on diagonal", 100, 100+k*j++ ); } //////// EVENT HANDLERS //////// void keyPressed() { //// Handle keys //// if (key == 'q') { exit(); } if (key == 'r') { reset(); } if (key == 'c') { cat.randomXY(); } if (key == 'z') { zoog.randomXY(); } //// b for more blobs; B for fewer blobs, etc. if (key == ' ') { if (numblobs0) numblobs--; } if (key == '0') { numblobs=1; } if (key == '#') { numblobs=maxblobs; } if (key == '/') { for (int i=0; i