// 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 color 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. int getValue( ) { return value; } // Returns true if mouseX and mouseY are withing the boundaries of the figure. 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( #ff0000 ); // red = 1 point deltaX = 1; deltaY = 2; break; case 2: col = color( #00ff00 ); // green = 2 points deltaX = 3; deltaY = 2; break; case 3: col = color( #0000ff ); // blue = 3 points deltaX = 4; deltaY = 5; break; case 4: col = color( #ff00ff ); // yellow = 4 point deltaX = 6; deltaY = 7; break; default: col = color( #ff0000 ); // 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); } }