import processing.core.*; import java.applet.*; import java.awt.*; import java.awt.image.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.text.*; import java.util.*; import java.util.zip.*; import javax.sound.midi.*; import javax.sound.midi.spi.*; import javax.sound.sampled.*; import javax.sound.sampled.spi.*; import java.util.regex.*; import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.dom.*; import javax.xml.transform.sax.*; import javax.xml.transform.stream.*; import org.xml.sax.*; import org.xml.sax.ext.*; import org.xml.sax.helpers.*; public class Homework9 extends PApplet {// name: Carlos Sosa // section: O // e-mail id: carlosso@andrew.cmu.edu // class Homework9 // A video game based on Homework 8 // User must click the moving figures. // Each figure is worth 1 to 4 points based on the color of the figure. // Player has 1 minute to score as many points as possible. // Global variables or fields go here final int MAX = 15; Figure [ ] figArr;// calls the Figure array int hitCount; int missCount; boolean index; // Methods go here. public void setup( ) { size ( 400, 400); smooth ( ); hitCount=0; missCount=0; //News the array figArr= new Figure [MAX]; frameRate(10); for (int i = 0; i < figArr.length; i++) { figArr[i]= new Figure(random(width),random(height),random(25,.15f*width),(int)random(1,5)); } } public void draw( ) { background( 140, 234, 156 ); for (int i = 0; i < MAX; i++) { figArr[i].drawFigure( );// draws the figure figArr[i].move( );// moves the figure } displayScores( ); if (frameCount%100 == 0) { frameRate(frameRate+5); } if (frameCount >= 500) { noLoop(); background( 140, 234, 156 ); text("Final Score: " + (hitCount-missCount), .38f*width, height/2); } } public void displayScores( ) { text("Time", .45f*width, height/15); text(500-frameCount,.46f*width, height/8); text("Hits", width/15, height/15); text(hitCount,.085f*width, height/8); text("Misses", width/30, .8f*height); text(missCount, .085f*width, .86f*height); } public void mousePressed() { index=false; int score=0; for(int i = 0; i < MAX; i++) { if (figArr[i].mouseOnFigure() == true) { index = true; score = figArr[i].getValue(); break; } } if (index == true) { hitCount += score; } else missCount++; } // class Figure modified for Homework 9 // original author: da' moose // date: 10.12.8 // modified by: class Figure { // fields private float x, y, figureSize, deltaX, deltaY; private int col; private int value; private boolean notHitYet; private PFont f1; // constructor // The syntax "this." requires Java to use the fields listed above // instead of the parameter with the same name. If the parameter has // a name that is different from the field, "this." is not used public Figure(float x, float y, float figureSize, int value) { this.x = x; this.y = y; this.figureSize = figureSize; this.value = value; setColorAndDelta( ); f1 = loadFont( "f1.vlw"); } // Returns value of the figure if it is clicked. public int getValue( ) { return value; } // Returns true if mouseX and mouseY are withing the boundaries of the figure. public boolean mouseOnFigure( ) { if (mouseX >= x && mouseX <= (x + figureSize) && mouseY >= y && mouseY <= (y + figureSize) ) { return true; } else { return false; } } // Uses value to set new color and delta values. private void setColorAndDelta( ) { switch( value ) { case 1: col = color( 0xffff0000 ); // red = 1 point deltaX = 1; deltaY = 2; break; case 2: col = color( 0xff00ff00 ); // green = 2 points deltaX = 3; deltaY = 2; break; case 3: col = color( 0xff0000ff ); // blue = 3 points deltaX = 4; deltaY = 5; break; case 4: col = color( 0xffff00ff ); // yellow = 4 point deltaX = 6; deltaY = 7; break; default: col = color( 0xffff0000 ); // red = 1 point deltaX = 1; deltaY = 2; } } // Checks figure's position horizontally to the right and vertically to the bottom. // If figure is off screen, it is wrapped back to the opposite edge and // its value reset to a random value between 1 and 4 and the color and // the method setColorAndDelta is called. public void move( ) { if ( x > width) { x = -figureSize; value = (int)random( 1, 5); setColorAndDelta( ); } if ( y > height) { y = -figureSize; value = (int)random( 1, 5); setColorAndDelta( ); } x += deltaX; y += deltaY; } // Draws a colored rectangle with the score value in the lower left corner public void drawFigure( ) { stroke( 0 ); strokeWeight( 1 ); fill( col ); rect( x, y, figureSize, figureSize ); fill( 255 ); textFont(f1, 24); text( value, x + 4, y + figureSize - 4); } } static public void main(String args[]) { PApplet.main(new String[] { "Homework9" }); }}