//******************************************************************************************** //Define the setup function, which will be called once in the beginning // to set up some initial settings of the program //******************************************************************************************** void setup() { //Set the canvas size size(800, 800); //Set the background color of the canvas background(192, 64, 0); //Set the stroke color as white stroke(255, 255, 255); //Set up the text font PFont f; f = createFont("Arial",16,true); textFont(f,32); } //******************************************************************************************** //Declare two variables to store the X and Y coordinates of a circle and // initilize the contents to represent the point (400, 400) //******************************************************************************************** float circleX=400; float circleY=400; //Declare a variable to store the score int score = 0; //******************************************************************************************** //Define the draw function, which will be called repeatedly to draw pictures on the screen //******************************************************************************************** void draw() { //Clear the canvas background(192, 64, 0); //********************************************************************* //Set the fill color to the red color, and then draw a 20 x 20 rectangle, // with (mouseX-10, mouseY-10) as the upperleft corner of the rectangle //********************************************************************* fill(255, 0, 0); rect(mouseX-10, mouseY-10, 20, 20); //********************************************************************* //Set the fill color to the green color, and then draw a circle with a radius of 20 // centered at (circleX, circleY). //********************************************************************* fill(0, 255, 0); ellipse(circleX, circleY, 15, 15); //******************************************************************************************** //Update the X and Y coordinates of a circle to move it away from the current location // of the mouse at (mouseX, mouseY), faster when they are closer. //******************************************************************************************** float distance; //Calculate the Manhattann distance where // abs(mouseX-circleX) gives the absolute value of (mouseX-circleX) // abs(mouseY-circleY) gives the absolute value of (mouseY-circleY) distance = abs(mouseX-circleX) + abs(mouseY-circleY)+ 1; circleX= circleX - 5*(mouseX-circleX)/distance; circleY= circleY - 5*(mouseY-circleY)/distance; //******************************************************************************************** //If the circle goes out of the canvas, bring it back to the middle of the canvas. //******************************************************************************************** if (circleX > 800) circleX = 400; if (circleX < 0) circleX = 400; if (circleY > 800) circleY = 400; if (circleY < 0) circleY = 400; //********************************************************************* //Draw the 40 by 40 capture area // with leftcentered at (680, 680). //********************************************************************* noFill(); rect(680, 680, 40, 40); //******************************************************************************************** //Check whether (circleX, circleY) is really close to to capture area (700, 700). //If they are, then we print out an end-of-game message. //******************************************************************************************** if ( ( abs( circleX - 700) < 20) && ( abs( circleY - 700) < 20) ) { text("Fish is captured!",700, 700) ; score = score + 1; } // Draw the score at (400, 50) text(score, 400, 50); }