//// CST-112 FINAL EXAM -- Jennifer Blomberg //// String student = " Jennifer Blomberg "; String title = "CST-112 FINAL EXAM: " + student; String news = "Click to move objects"; //////// GLOBAL DECLARATIONS //////// int score = 0; float left = 10, top = 50, right = 750, bottom = 550; Animal Lady, Tramp, bird, bird1; Animal submarine; Animal ant; // +++++ ADD YOUR DECLARATIONS HERE, // +++++ to create array of 5 bugs. //////// SKETCH FUNCTIONS //////// void setup( ) { //// Setup. size( 800, 600 ); right = width - 10; bottom = height - 10; reset( ); } void reset( ) { //// initialize the menagarie. Tramp = new Animal( 50, 30, 20, 4, 0 ); Tramp.setRGB( 255, 150, 255 ); Tramp.x = 100; Tramp.y = 100; stroke( 0 ); Tramp.name = "Tramp"; // Lady = new Animal( 40, 80, 0, 2, 1 ); Lady.setRGB( 0, 0, 0 ); Lady.x = 200; Lady.y = 200; Lady.name = "Lady"; // bird = new Animal( 3, 0, 0, 50, 15, 12, 0, 0 ); bird.setRGB( 50, 50, 250 ); bird.x = 300; bird.y = 300; // second bird // bird1 = new Animal( 3, 0, 0, 50, 15, 12, 50, 50 ); bird1.setRGB( 50, 150, 100 ); bird1.x = 300; bird1.y = 300; // submarine = new Animal( 4, 0, 150, 150, 25, 25, 0, 0 ); submarine.setRGB( 255, 255, 0 ); submarine.x = 400; submarine.y = 400; submarine.name = "Yellow Submarine" ; // ant = new Animal( 6, 0, 3, 15, 45, 255, 50, 50 ); ant.y = bottom ; //// Creat five bugs //// // ADD YOUR CODE HERE // } void draw( ) { //// Next frame. scene( ); action( ); // Move all objects, check collisions, etc. show( ); // Draw objects on screen. messages( ); } void action( ) { //// Move, check collisions, etc. //// Tramp.move( ); bird.move( ); bird1.move( ); Lady.move( ); crash( ); // ant.move( ); if ( ant.y < top + 20 ) { ant.dy = 0; // Dead ant -- Stop rising. ant.setRGB( 150, 50, 50 ); // Change color if (ant.x > right - 20 ) { // Float off end; new ant. ant = new Animal( 6, 0, 3, 15, 45, 255, 0, 0 ); ant.y = bottom; } } //// Move the bugs -- same as the ant. // ++++++++++ YOUR CODE GOES HERE. +++++++++++++ // } void crash( ) { //// Check for collisions //// // Lady kicks the Tramp (+50) -- move Tramp to right side. if (Lady.hit( Tramp ) ) { Tramp.x=right-20; score += 50; } // Tramp Trampches bird (-100) -- move bird to left side. // ++++++++++ YOUR CODE GOES HERE. +++++++++++++ // // bird hits Lady (-50) -- move Lady to random position. // ++++++++++ YOUR CODE GOES HERE. +++++++++++++ // // bird Trampches any bug (+10) -- start new bug at bottom // ++++++++++ YOUR CODE GOES HERE. +++++++++++++ // // Tramp Trampches any bug (+25) -- start new bug at bottom // ++++++++++ YOUR CODE GOES HERE. +++++++++++++ // } void show( ) { //// Draw objects on screen. //// Tramp.show( ); bird.show( ); bird1.show( ); Lady.show( ); submarine.show(); ant.show( ); for ( int i = 0; i < 5; i++) { //// Display the bugs //// // ++++++++++ YOUR CODE GOES HERE. +++++++++++++ // } } void scene( ) { //// Background, scene, etc. //// // Grass along bottom of screen. // stroke( 0, 150, 0 ); for ( int x = 10; x < width; x = x + 25) { line( x, height, x + 2, height - 25 ); //// ONE blade of grass from bottom of screen. } background( 255, 255, 200 ); fill( 150, 200, 150 ); rectMode( CORNERS ); rect( left, top, right, bottom ); //// Blades of grass across the bottom //// stroke( 0 ); // Grass color. strokeWeight( 1 ); // Grass along bottom of screen. // stroke( 0 ); for ( int x = 10; x < width; x = x + 5) { line( x, height, x + 2, height - 25 ); //// ONE blade of grass from bottom of screen. } // ++++++++++ ADD YOUR CODE HERE. +++++++++++++ // } void messages() { //// Display messages fill( 0 ); textSize( 20 ); text( title, width/3, 25 ); textSize( 14 ); text( news, left, 40 ); } //////// EVENT HANDLERS //////// void keyPressed( ) { //// Handle keys //// if ( key == 'q' ) { exit(); } if ( key == 'r' ) { reset(); } if ( key == 'c' ) { Tramp.randomXY(); } if ( key == 'z' ) { Lady.randomXY(); } if ( key == 'b' ) { bird.randomXY( ); bird.dx = 12; bird.dy = .5; bird1.dx = 8; bird1.dy = 5; } } //////// CLASS DEFINITION //////// class Animal { //// Animal has a body, an optional head, legs, pairs of arms, etc. //// Animal is wide and tall, and has other properties such as color. //// Also: position (x,y) and velocity (dx,dy). //// Desription and drawing info. float wide = 10, tall = 50, head = 15, tail = 10; int legs = 2, armpairs = 2, reach = 10; String name = ""; int r, g , b; // Color. int shape = 0; //// Position, velocity, boundaries float x = 400,y = 300, dx = 2, dy = 3; //// CONSTRUCTORS (and set functions) //// Animal( int shape, int legs ) { //// 2-arg Constructor: shape, wide, tall //// this.shape= shape; randomXY( ); //Random x,y, dx,dy } Animal( int shape, int legs, int armpairs, float wide, float tall, int r, int g, int b) { //// 8-arg Constructor: shape, wide, tall //// this.shape = shape; this.legs = legs; this.armpairs = armpairs; this.wide = wide; this.tall = tall; randomXY( ); //Random x,y, dx,dy setRGB( r, g, b ); } Animal( float wide, float tall, float head, int legs, int armpairs ) { //// 5-arg Constructor //// this.wide = wide; this.tall = tall; this.head = head; this.legs = legs; this.armpairs = armpairs; } void setRGB( int r, int g, int b) { this.r = r; this.g = g; this.b = b; } void randomXY( ) { x = random( 100, 700); y = random( 100, 500); dx = random( 1, 8); dy = random( 1, 5); } //// METHODS to move and show //// ////////////////////////////////// void move( ) { //// Check for boundaries, and move to new coordinates (x+dx, y+dy). if ( x < left || x > right ) dx = -dx; if ( y < top || y > bottom) 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; } //// Draw different shapes. //// 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) { // ++++++++++ ADD YOUR CODE HERE. +++++++++++++ // } else if ( shape == 6 ) { ellipse( x, y - tall * 2/3, wide, tall/3 ); ellipse( x, y - tall/3, wide, tall/3 ); ellipse( x, y, wide, tall/3 ); reach = 8; } else { // ++++++++++ ADD YOUR CODE HERE. +++++++++++++ // text( "Found you", x, y ); } //// Head 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, 0, 0 ); // Tail at rear } limbs( ); //// Name fill( 256 - r, 256 - g, 256 - b ); text( name, x - wide/3, y - tall/6 ); } 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+reach ); // First leg. } if ( legs > 1 ) { float legSpacing = wide / ( legs - 1 ); for ( int i = 1; i < legs; i++ ) { legX = legX + legSpacing; line( legX, legY, legX, legY + reach ); // First leg. } } //// Pairs of arms. //// float armY = y - tall/2; // Arms start at top of rectangle. float armSpacing = tall / (armpairs); for ( int i = 0; i < armpairs; i++ ) { line( x - wide/2, armY, x - reach - wide/2, armY ); // Left arm line( x + wide/2, armY, x + reach + wide/2, armY ); // Right arm armY = armY + armSpacing; } } boolean hit( float x, float y ) { //// Return true iff x,y is inside this box. if ( x < this.x - this.wide/2 ) return false; if ( x > 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 ); } }