Glitch in the animation (brief ghost appearance)
in
Programming Questions
•
2 years ago
This animation contains a glitch which is like an itch that I can't scratch (or find).
When the fish changes position, there is a brief flash of its ghost that appears at 180 degrees.
I can't find why. Slowing down the framerate does not help.
/* Undersea race*/
// declare the object variables for this program
Raceline drawStartLine; // Declare a reference variable for the start line object inherited from the Raceline class
Raceline drawFinishLine; // Declare a reference variable for the finish line object inherited from the Raceline class
Starfish steadyStarfish; // Declare a reference variable for the steady starfish object inherited from the Starfish class
Turtle slowTurtle; // Declare a reference variable for the slow turtle object inherited from the Turtle class
Fish indecisiveFish; // Declare reference variable for the indecisive fish object inherited from the Fish class
FishReverse indecisiveFishReverse; // Declare reference variable for the reverse indecisive fish object inherited from the FishReverse class
// declare the object variables related to the text in this program (to write "Start Line" and "Finish Line")
PFont startFinishLine; // Declare a variable that is of the Font class type
String startLine; // Declare a variable that is of the String class type to store a sequence of characters that will textually describe the "Start Line"
String finishLine; // Declare a variable that is of the String class type to store a sequence of characters that will textually describe the "Finish Line"
float fishAngle; // declare the variable that will control the position of the cos and sin wave for the fish
// define the initial environment properties for this program in the setup function that will be called only once
void setup () { // define initial environment properties such as screen size and background color by calling the setup method
size (screen.width*3/4, screen.height*5/6); // set the canvas size so that it fills up a large part of the screen
background (0, 170, 250); // set the color of the background to a watery shade of blue
// initialize the font object variables for the text in this program (to write "Start Line" and "Finish Line")
startFinishLine =loadFont ("CourierNewPS-BoldItalicMT-16.vlw"); // construct a new font for the text in this program by loading this font used in a data file attached to this file
textFont (startFinishLine); // make the loaded font active in the startFinishLine variable that is of the Font class type
startLine = "Start Line"; // initialize the startLine string type variable with a sequence of characters that spells "Start Line"
finishLine = "Finish Line"; // initialize the finishLine string type variable with a sequence of characters that spells "Finish Line"
fishAngle=245.0; // initialize the angle of the fish so that it emerges from the top left of the canvas
// declare and initialize the race lines and the three different elements of this animation as objects
drawStartLine=new Raceline (color (162, 20, 148), color (162, 20, 148), 2, 40); // declare and initialize the startline object and pass in the parameters that define it
drawFinishLine=new Raceline (color (162, 20, 148), color (162, 20, 148), 2, width-40); // declare and initialize the finishline object and pass in the parameters that define it
steadyStarfish=new Starfish (color (216, 20, 125), 0, 0); // declare and initialize the steadyStarfish object and pass in the parameters that define it
slowTurtle=new Turtle (color (114, 188, 19), 0, 0); // declare and initialize the slowTurtle object and pass in the parameters that define it
indecisiveFish=new Fish (color (255, 40, 10), 0, 0); // declare and initialize the indecisiveFish object and pass in the parameters that define it
indecisiveFishReverse=new FishReverse (color (255, 40, 10), 0, 0); // declare and initialize the reverse indecisiveFish object and pass in the parameters that define it
} // end the setup method
void draw() { // start the draw method
background (0, 170, 250); // set the color of the background to a watery shade of blue
// write the phrases "start line" and "finish line" on the canvas
fill (111, 4, 100); // determine the color of the text as purple
text (startLine, 56, 26); // display the "Start Line" on the top left of the canvas
text (finishLine, width-170, height-20); // display the "Finish Line" on the bottom right of the canvas
drawStartLine.display(); // call the method to display to the screen the startline object inherited from the Raceline class
drawFinishLine.display(); // call the method to display to the screen the finishline object inherited from the Raceline class
// execute the race by displaying and moving each element across the screen
steadyStarfish.move(); // call the method to move the steadyStarfish to the screen
steadyStarfish.display(); // call the method to display the steadyStarfish to the screen
slowTurtle.move(); // call the method to move the slowTurtle to the screen
slowTurtle.display(); // call the method to display the slowTurtle to the screen
float angleCondition=fishAngle/180; // declare and create a variable that calculates the division of the angle of the cos and sin movement by 180 to determine the condition when the fish changes direction
int angleConditionConvertedtoInteger = int (angleCondition); // convert this variable from a float type to an integer type
if (angleConditionConvertedtoInteger%2==0) { // create a condition that uses the remainder of the division of this variable to determine the direction of the fish so that if the remainder is an even number, the fish swims back
indecisiveFishReverse.move (); // move the reverse indecisive fish so that it swims back towards the start line
indecisiveFishReverse.display (); // draw the reverse indecisive fish so that it swims back towards the start line
}
else { // create a condition that uses the remainder of the division of this variable to determine the direction of the fish so that if the remainder is an odd number, the fish swims forward
indecisiveFish.move(); // move the indecisive fish in its forward direction so that it swims towards the finish line
indecisiveFish.display(); // draw the indecisive fish in its forward direction so that it swims towards the finish line
}
} // end the draw method
// Race line class
class Raceline {
color clrLine;
color strokeClr;
int dotSize;
int raceLineX;
Raceline (color clrLinetemp, color strokeClrtemp, int dotSizetemp, int raceLineXtemp) {
clrLine=clrLinetemp;
strokeClr=strokeClrtemp;
dotSize=dotSizetemp;
raceLineX=raceLineXtemp;
}
void display() {
for (int i=0; i<height; i+=8) { // loop that will use a counter that will draw a shape every 8 pixels for the whole height of the canvas
stroke (strokeClr); // determine the color of the stroke of the ellipses that form the dotted line as purple
fill (clrLine); // fill with purple the ellipses that form the dotted lines
ellipse (raceLineX, i, dotSize, dotSize); // draw little ellipses every 8 pixels from top to bottom on the left of the canvas to make up the dotted line
} // end the for loop
} // end display
}// end class
// StarFish class
class Starfish { // create a class called Starfish
// all of the fields for the class called Starfish
float starFishX;
float starFishY;
color clrStarFish; // declare the variable that controls the color parameter of the pentagon class
float starFishAngle; // declare the variable that will control the position of the cos and sin wave for the starfish
float starFishAngleSpeed; // declare the variable that will allow for the incrementation of the angle for the starfish undulatory movements
float starFishCenterX; // declare the variable that will determine the X position from which the cos motion occurs for the starfish
float starFishCenterY; // declare the variable that will determine the Y position from which the sin motion occurs for the starfish
float sizeArcStarFishMoveX; // declare the variable that will determine the radius of the cos motion for the starfish so the breadth of the circular movement
float sizeArcStarFishMoveY; // declare the variable that will determine the radius of the sin motion for the starfish so the breadth of the circular movement
float starFishLocX; // declare the variable that determines the X location of the starfish in the animation
float starFishLocY; // declare the variable that determines the Y location of the starfish in the animation
float speedXstarFish; // declare the variable that determines the speed applied to the location of the starfish in the animation (in this case only the X location)
// constructor for the class called Starfish
Starfish (color clrStarFishtemp, float starFishXtemp, float starFishYtemp) { // create the constructor for the Starfish class and declare its temporary variables
starFishX=starFishXtemp;
starFishY=starFishYtemp;
starFishAngle=255.0; // initialize the angle of the starFish so that it emerges from the upper left of the canvas
clrStarFish=clrStarFishtemp; // initialize the variable that controls the color parameter of the starfish class
starFishCenterX=0.0; // assign a value of 0 to the variable that will determine the X position from which the cos motion occurs for the starfish
starFishCenterY=height/3; // assign a value of a third of the height of the canvas to the variable that will determine the Y position from which the sin motion occurs for the starfish
sizeArcStarFishMoveX=width/6; // assign a value to the variable that will determine the radius of the cos motion for the starfish so the breadth of the circular movement
sizeArcStarFishMoveY=height/4; // assign a value to the variable that will determine the radius of the sin motion for the starfish so the breadth of the circular movement
} // end the block of code of the constructor for the class called Starfish
// method to move the Starfish
void move () {
starFishX=starFishLocX+speedXstarFish;
starFishY=starFishLocY;
starFishAngleSpeed=(random(-0.4, 0.1)); // assign a random value between -0.4 and 0.1 to the change of angle used in the undulatory movement of the starfish to make it a bit jittery
starFishLocX=starFishCenterX+sizeArcStarFishMoveX*cos(radians(starFishAngle)); // assign a cos wave movement to the variable that determines the X location of the starfish in the animation
starFishLocY=starFishCenterY+sizeArcStarFishMoveY*sin(radians(starFishAngle)); // assign a sin wave movement to the variable that determines the Y location of the starfish in the animation
starFishAngle+=starFishAngleSpeed; // increment the angle of the starfish cos and sin wave movements the random speed assigned to the starfish speed variable
speedXstarFish+=0.5+(random(-0.8, 0.8)); // assign a value that is a random value added to a constant to move the starfish horizontally across the screen
}
// method to display the Starfish
void display () { /// create a variable that will refer to the function used to display the starfish
strokeJoin(ROUND);
strokeWeight (4);
stroke(1);
fill(clrStarFish);
beginShape();
vertex(starFishX, starFishY+24);
vertex(starFishX+20, starFishY+32);
vertex(starFishX+27, starFishY);
vertex(starFishX+34, starFishY+32);
vertex(starFishX+54, starFishY+24);
vertex(starFishX+42, starFishY+54);
vertex(starFishX+50, starFishY+96);
vertex(starFishX+27, starFishY+72);
vertex(starFishX+4, starFishY+96);
vertex(starFishX+12, starFishY+54);
endShape(CLOSE); // end the recording of the vertices for the Starfish class and closes the shape
} // end the block of code of the display method for the class called Starfish
} // end the block for the class called Starfish
// Turtle class
class Turtle { // create a class called Turtle
// all of the fields for the class called Turtle
float turtleX;
float turtleY;
color clrTurtle;
float angle; // declare the variable that will control the position of the sin wave for the turtle
float turtleMiddleYPos; // declare the variable that will determine the Y position from which the sin motion occurs for the turtle
float turtleYRangeOfMotion; // declare the variable that will determine the radius of the sin motion for the turtle
float speedOfSinWaveMovement; // declare the variable that will impart a change in the angle that controls the sin wave for the turtle
float turtleLocX; // declare the variable that determines the X location of the turtle in the animation
float turtleLocY; // declare the variable that determines the Y location of the turtle in the animation
float speedXTurtle; // declare the variable that determines the speed of the turtle's horizontal movement
// constructor for the class called Turtle
Turtle (color clrTurtletemp, float turtleXtemp, float turtleYtemp) { // create the constructor for the Turtle class and declare its temporary variables
turtleX=turtleXtemp;
turtleY=turtleYtemp;
clrTurtle=clrTurtletemp; // initialize the variable that controls the color parameter of the turtle class
turtleLocX=0.0-140; // initialize the content of global variable for the X location of the turtle
angle=0.0; // initialize the value of the angle of the sin wave that has a zero value
turtleMiddleYPos=height/2-58; // initialize that determines the Y position from which the sin motion occurs so that it is in the center taking into account the height of the turtle divided by 2 (116%2=58)
turtleYRangeOfMotion=height/2.6; // initialize a value for the range of Y movement of the turtle on the sin wave that makes it cover most of the height but stays inside the window
speedOfSinWaveMovement=0.005; // initialize a value for the speed of the change of angle of the sin wave that is relatively slow
} // end the block of code of the constructor for the class called Turtle
// method to move the Turtle
void move () {
turtleX=turtleLocX+speedXTurtle;
turtleY=turtleLocY;
turtleLocY=turtleMiddleYPos+sin(angle)*turtleYRangeOfMotion; // calculates the Y location of the turtle on the sin wave movement
angle+=speedOfSinWaveMovement; // increment the angle of the sin wave by the speed factor every time the draw () method is called
speedXTurtle+=0.5; //increment the speed of the turtle's horizontal movement
}
// method to display the Turtle
void display () { /// create a variable that will refer to the function used to display the starfish
// assign parameters for the color and stroke of the solid turtle figure
fill(clrTurtle);
; // set color of solid figure to a turtle-like green
stroke(0); // set color of stroke to black for the solid figure outlines
smooth(); // set anti-aliased edges for the solid turtle figure
strokeWeight (3); // set the thickness of the stroke that delineated the solid turtle figure
//draw the rear legs of the turtle with triangle
triangle (turtleX+28, turtleY+18, turtleX+40, turtleY+10, turtleX, turtleY+4);
triangle (turtleX+28, turtleY+18, turtleX+22, turtleY+52, turtleX+52, turtleY+62);
// draw the top flapper underneath the body and head
beginShape();
curveVertex (turtleX+122, turtleY+25);
curveVertex (turtleX+122, turtleY+25);
curveVertex (turtleX+128, turtleY+21);
curveVertex (turtleX+138, turtleY-8);
curveVertex (turtleX+115, turtleY-30);
curveVertex (turtleX+125, turtleY-15);
curveVertex (turtleX+113, turtleY+7);
curveVertex (turtleX+113, turtleY+7);
endShape(CLOSE); // end the recording of the vertices for the top flapper of the Turtle class and closes the shape
// draw the bottom flapper underneath the body and head
beginShape();
curveVertex (turtleX+131, turtleY+23);
curveVertex (turtleX+131, turtleY+23);
curveVertex (turtleX+136, turtleY+27);
curveVertex (turtleX+138, turtleY+42);
curveVertex (turtleX+130, turtleY+60);
curveVertex (turtleX+118, turtleY+85);
curveVertex (turtleX+119, turtleY+86);
curveVertex (turtleX+115, turtleY+78);
curveVertex (turtleX+124, turtleY+25);
curveVertex (turtleX+124, turtleY+25);
endShape(CLOSE); // end the recording of the vertices for the bottom flapper of the Turtle class and closes the shape
// draw the body of the turtle with 11 curveVertices points
beginShape ();
//end of the turtle's tail
curveVertex (turtleX, turtleY+30);
curveVertex (turtleX, turtleY+30);
//top of turtle
curveVertex (turtleX+12, turtleY+20);
curveVertex (turtleX+50, turtleY+8);
curveVertex (turtleX+118, turtleY);
// head of turtle and twist in the turtle's neck
curveVertex (turtleX+152, turtleY+30);
curveVertex (turtleX+166, turtleY+8);
curveVertex (turtleX+146, turtleY+10);
//bottom of turtle
curveVertex (turtleX+126, turtleY+40);
curveVertex (turtleX+80, turtleY+52);
curveVertex (turtleX+20, turtleY+35);
// part of turtle's tail that connects to its end
curveVertex (turtleX+12, turtleY+25);
curveVertex (turtleX+12, turtleY+25);
endShape(CLOSE); // end the recording of the vertices for the body of the Turtle class and closes the shape
// draw the eye and the smile of the turtle
fill(232, 221, 7); // make eye color brown
ellipse (turtleX+158, turtleY+14, 2, 2); // draw small eye
noStroke(); // eliminate stroke for the mouth
fill(93, 9, 0); // make mouth pinky red
arc(turtleX+156, turtleY+24, 6, 6, QUARTER_PI, PI+QUARTER_PI); //draw arc of mouth
} // end the block of code of the display method for the class called Turtle
} // end the block for the class called Turtle
// Fish class
class Fish { // create a class called Fish
// all of the fields for the class called Turtle
float fishX;
float fishY;
color clrFish;
float fishAngleSpeed; // declare the variable that will allow for the incrementation of the angle for the fish circular movements
float fishCenterX; // declare the variable that will determine the X position from which the cos motion occurs for the fish
float fishCenterY; // declare the variable that will determine the Y position from which the sin motion occurs for the fish
float sizeArcFishMoveX; // declare the variable that will determine the radius of the cos motion for the fish swimming back and forth in circles
float sizeArcFishMoveY; // declare the variable that will determine the radius of the sin motion for the fish swimming back and forth in circles
float fishLocX; // declare the variable that determines the X location of the fish in the animation
float fishLocY; // declare the variable that determines the Y location of the fish in the animation
float speedXFish; // declare the variable that determines the speed applied to the location of the fish in the animation (in this case only the X location)
// constructor for the class called Fish
Fish (color clrFishtemp, float fishXtemp, float fishYtemp) { // create the constructor for the Turtle class and declare its temporary variables
fishX=fishXtemp;
fishY=fishYtemp;
clrFish=clrFishtemp; // initialize the variable that controls the color parameter of the turtle class
fishAngleSpeed=0.4; // assign a value of 0.4 to the change of angle used in the cos and sign wave movement of the fish
fishCenterX=0.0; // assign a value of 0 to the variable that will determine the X position from which the cos motion occurs for the fish
fishCenterY=height/2; // assign a value of half the height of the canvas to the variable that will determine the Y position from which the sin motion occurs for the fish
sizeArcFishMoveX=width/3; // assign a value of a third of the width of the canvas to the variable that will determine the radius of the cos wave motion of the fish
sizeArcFishMoveY=height/3; // assign a value of a third of the height of the canvas to the variable that will determine the radius of the sin wave motion of the fish
} // end the block of code of the constructor for the class called Fish
// method to move the Fish
void move () {
fishX=fishLocX+speedXFish;
fishY=fishLocY;
fishLocX=fishCenterX+sizeArcFishMoveX*cos(radians(fishAngle)); // assign a cos wave movement to the variable that determines the X location of the fish in the animation
fishLocY=fishCenterY+sizeArcFishMoveY*sin(radians(fishAngle)); // assign a sin wave movement to the variable that determines the Y location of the fish in the animation
fishAngle+=fishAngleSpeed; // increment the angle of the fish cos and sin wave movements by 0.4
speedXFish+=(random(0.1, 0.8)); // assign a value that is a random value to move the starfish horizontally across the screen
}
// method to display the Fish
void display () { /// create a variable that will refer to the function used to display the Fish
fill(clrFish);
stroke(1);
strokeWeight (3);
beginShape();
vertex(fishX, fishY);
vertex(fishX+50, fishY+40);
vertex(fishX+105, fishY+20);
vertex(fishX+150, fishY+50);
vertex(fishX+90, fishY+80);
vertex(fishX+55, fishY+70);
vertex(fishX+30, fishY+90);
endShape(CLOSE);
arc (fishX+116, fishY+42, 10, 15, 0, PI);
} // end the block of code of the display method for the class called Fish
} // end the block for the class called Fish
// Reverse Fish class
class FishReverse { // create a class called ReverseFish to show the fish in reverse
// all of the fields for the class called FishReverse
float fishXR;
float fishYR;
color clrFishR;
float fishAngleSpeedR; // declare the variable that will allow for the incrementation of the angle for the fish circular movements
float fishCenterXR; // declare the variable that will determine the X position from which the cos motion occurs for the fish
float fishCenterYR; // declare the variable that will determine the Y position from which the sin motion occurs for the fish
float sizeArcFishMoveXR; // declare the variable that will determine the radius of the cos motion for the fish swimming back and forth in circles
float sizeArcFishMoveYR; // declare the variable that will determine the radius of the sin motion for the fish swimming back and forth in circles
float fishLocXR; // declare the variable that determines the X location of the fish in the animation
float fishLocYR; // declare the variable that determines the Y location of the fish in the animation
float speedXFishR; // declare the variable that determines the speed applied to the location of the fish in the animation (in this case only the X location)
// constructor for the class called Reverse Fish
FishReverse (color clrFishtempR, float fishXtempR, float fishYtempR) { // create the constructor for the Turtle class and declare its temporary variables
fishXR=fishXtempR;
fishYR=fishYtempR;
clrFishR=clrFishtempR; // initialize the variable that controls the color parameter of the turtle class
fishAngleSpeedR=0.4; // assign a value of 0.4 to the change of angle used in the cos and sign wave movement of the fish
fishCenterXR=0.0; // assign a value of 0 to the variable that will determine the X position from which the cos motion occurs for the fish
fishCenterYR=height/2; // assign a value of half the height of the canvas to the variable that will determine the Y position from which the sin motion occurs for the fish
sizeArcFishMoveXR=width/3; // assign a value of a third of the width of the canvas to the variable that will determine the radius of the cos wave motion of the fish
sizeArcFishMoveYR=height/3; // assign a value of a third of the height of the canvas to the variable that will determine the radius of the sin wave motion of the fish
} // end the block of code of the constructor for the class called Fish
// method to move the Fish
void move () {
fishXR=fishLocXR+speedXFishR;
fishYR=fishLocYR;
fishLocXR=fishCenterXR+sizeArcFishMoveXR*cos(radians(fishAngle)); // assign a cos wave movement to the variable that determines the X location of the fish in the animation
fishLocYR=fishCenterYR+sizeArcFishMoveYR*sin(radians(fishAngle)); // assign a sin wave movement to the variable that determines the Y location of the fish in the animation
fishAngle+=fishAngleSpeedR; // increment the angle of the fish cos and sin wave movements by 0.4
speedXFishR+=(random(0.1, 0.8)); // assign a value that is a random value to move the starfish horizontally across the screen
}
// method to display the Reverse Fish
void display () {
fill(clrFishR);
stroke(1);
strokeWeight (3);
beginShape();
vertex(fishXR, fishYR+50);
vertex(fishXR+45, fishYR+20);
vertex(fishXR+100, fishYR+40);
vertex(fishXR+150, fishYR);
vertex(fishXR+120, fishYR+90);
vertex(fishXR+95, fishYR+70);
vertex(fishXR+60, fishYR+80);
endShape(CLOSE);
arc (fishXR+34, fishYR+42, 10, 15, 0, PI);
}
}
When the fish changes position, there is a brief flash of its ghost that appears at 180 degrees.
I can't find why. Slowing down the framerate does not help.
/* Undersea race*/
// declare the object variables for this program
Raceline drawStartLine; // Declare a reference variable for the start line object inherited from the Raceline class
Raceline drawFinishLine; // Declare a reference variable for the finish line object inherited from the Raceline class
Starfish steadyStarfish; // Declare a reference variable for the steady starfish object inherited from the Starfish class
Turtle slowTurtle; // Declare a reference variable for the slow turtle object inherited from the Turtle class
Fish indecisiveFish; // Declare reference variable for the indecisive fish object inherited from the Fish class
FishReverse indecisiveFishReverse; // Declare reference variable for the reverse indecisive fish object inherited from the FishReverse class
// declare the object variables related to the text in this program (to write "Start Line" and "Finish Line")
PFont startFinishLine; // Declare a variable that is of the Font class type
String startLine; // Declare a variable that is of the String class type to store a sequence of characters that will textually describe the "Start Line"
String finishLine; // Declare a variable that is of the String class type to store a sequence of characters that will textually describe the "Finish Line"
float fishAngle; // declare the variable that will control the position of the cos and sin wave for the fish
// define the initial environment properties for this program in the setup function that will be called only once
void setup () { // define initial environment properties such as screen size and background color by calling the setup method
size (screen.width*3/4, screen.height*5/6); // set the canvas size so that it fills up a large part of the screen
background (0, 170, 250); // set the color of the background to a watery shade of blue
// initialize the font object variables for the text in this program (to write "Start Line" and "Finish Line")
startFinishLine =loadFont ("CourierNewPS-BoldItalicMT-16.vlw"); // construct a new font for the text in this program by loading this font used in a data file attached to this file
textFont (startFinishLine); // make the loaded font active in the startFinishLine variable that is of the Font class type
startLine = "Start Line"; // initialize the startLine string type variable with a sequence of characters that spells "Start Line"
finishLine = "Finish Line"; // initialize the finishLine string type variable with a sequence of characters that spells "Finish Line"
fishAngle=245.0; // initialize the angle of the fish so that it emerges from the top left of the canvas
// declare and initialize the race lines and the three different elements of this animation as objects
drawStartLine=new Raceline (color (162, 20, 148), color (162, 20, 148), 2, 40); // declare and initialize the startline object and pass in the parameters that define it
drawFinishLine=new Raceline (color (162, 20, 148), color (162, 20, 148), 2, width-40); // declare and initialize the finishline object and pass in the parameters that define it
steadyStarfish=new Starfish (color (216, 20, 125), 0, 0); // declare and initialize the steadyStarfish object and pass in the parameters that define it
slowTurtle=new Turtle (color (114, 188, 19), 0, 0); // declare and initialize the slowTurtle object and pass in the parameters that define it
indecisiveFish=new Fish (color (255, 40, 10), 0, 0); // declare and initialize the indecisiveFish object and pass in the parameters that define it
indecisiveFishReverse=new FishReverse (color (255, 40, 10), 0, 0); // declare and initialize the reverse indecisiveFish object and pass in the parameters that define it
} // end the setup method
void draw() { // start the draw method
background (0, 170, 250); // set the color of the background to a watery shade of blue
// write the phrases "start line" and "finish line" on the canvas
fill (111, 4, 100); // determine the color of the text as purple
text (startLine, 56, 26); // display the "Start Line" on the top left of the canvas
text (finishLine, width-170, height-20); // display the "Finish Line" on the bottom right of the canvas
drawStartLine.display(); // call the method to display to the screen the startline object inherited from the Raceline class
drawFinishLine.display(); // call the method to display to the screen the finishline object inherited from the Raceline class
// execute the race by displaying and moving each element across the screen
steadyStarfish.move(); // call the method to move the steadyStarfish to the screen
steadyStarfish.display(); // call the method to display the steadyStarfish to the screen
slowTurtle.move(); // call the method to move the slowTurtle to the screen
slowTurtle.display(); // call the method to display the slowTurtle to the screen
float angleCondition=fishAngle/180; // declare and create a variable that calculates the division of the angle of the cos and sin movement by 180 to determine the condition when the fish changes direction
int angleConditionConvertedtoInteger = int (angleCondition); // convert this variable from a float type to an integer type
if (angleConditionConvertedtoInteger%2==0) { // create a condition that uses the remainder of the division of this variable to determine the direction of the fish so that if the remainder is an even number, the fish swims back
indecisiveFishReverse.move (); // move the reverse indecisive fish so that it swims back towards the start line
indecisiveFishReverse.display (); // draw the reverse indecisive fish so that it swims back towards the start line
}
else { // create a condition that uses the remainder of the division of this variable to determine the direction of the fish so that if the remainder is an odd number, the fish swims forward
indecisiveFish.move(); // move the indecisive fish in its forward direction so that it swims towards the finish line
indecisiveFish.display(); // draw the indecisive fish in its forward direction so that it swims towards the finish line
}
} // end the draw method
// Race line class
class Raceline {
color clrLine;
color strokeClr;
int dotSize;
int raceLineX;
Raceline (color clrLinetemp, color strokeClrtemp, int dotSizetemp, int raceLineXtemp) {
clrLine=clrLinetemp;
strokeClr=strokeClrtemp;
dotSize=dotSizetemp;
raceLineX=raceLineXtemp;
}
void display() {
for (int i=0; i<height; i+=8) { // loop that will use a counter that will draw a shape every 8 pixels for the whole height of the canvas
stroke (strokeClr); // determine the color of the stroke of the ellipses that form the dotted line as purple
fill (clrLine); // fill with purple the ellipses that form the dotted lines
ellipse (raceLineX, i, dotSize, dotSize); // draw little ellipses every 8 pixels from top to bottom on the left of the canvas to make up the dotted line
} // end the for loop
} // end display
}// end class
// StarFish class
class Starfish { // create a class called Starfish
// all of the fields for the class called Starfish
float starFishX;
float starFishY;
color clrStarFish; // declare the variable that controls the color parameter of the pentagon class
float starFishAngle; // declare the variable that will control the position of the cos and sin wave for the starfish
float starFishAngleSpeed; // declare the variable that will allow for the incrementation of the angle for the starfish undulatory movements
float starFishCenterX; // declare the variable that will determine the X position from which the cos motion occurs for the starfish
float starFishCenterY; // declare the variable that will determine the Y position from which the sin motion occurs for the starfish
float sizeArcStarFishMoveX; // declare the variable that will determine the radius of the cos motion for the starfish so the breadth of the circular movement
float sizeArcStarFishMoveY; // declare the variable that will determine the radius of the sin motion for the starfish so the breadth of the circular movement
float starFishLocX; // declare the variable that determines the X location of the starfish in the animation
float starFishLocY; // declare the variable that determines the Y location of the starfish in the animation
float speedXstarFish; // declare the variable that determines the speed applied to the location of the starfish in the animation (in this case only the X location)
// constructor for the class called Starfish
Starfish (color clrStarFishtemp, float starFishXtemp, float starFishYtemp) { // create the constructor for the Starfish class and declare its temporary variables
starFishX=starFishXtemp;
starFishY=starFishYtemp;
starFishAngle=255.0; // initialize the angle of the starFish so that it emerges from the upper left of the canvas
clrStarFish=clrStarFishtemp; // initialize the variable that controls the color parameter of the starfish class
starFishCenterX=0.0; // assign a value of 0 to the variable that will determine the X position from which the cos motion occurs for the starfish
starFishCenterY=height/3; // assign a value of a third of the height of the canvas to the variable that will determine the Y position from which the sin motion occurs for the starfish
sizeArcStarFishMoveX=width/6; // assign a value to the variable that will determine the radius of the cos motion for the starfish so the breadth of the circular movement
sizeArcStarFishMoveY=height/4; // assign a value to the variable that will determine the radius of the sin motion for the starfish so the breadth of the circular movement
} // end the block of code of the constructor for the class called Starfish
// method to move the Starfish
void move () {
starFishX=starFishLocX+speedXstarFish;
starFishY=starFishLocY;
starFishAngleSpeed=(random(-0.4, 0.1)); // assign a random value between -0.4 and 0.1 to the change of angle used in the undulatory movement of the starfish to make it a bit jittery
starFishLocX=starFishCenterX+sizeArcStarFishMoveX*cos(radians(starFishAngle)); // assign a cos wave movement to the variable that determines the X location of the starfish in the animation
starFishLocY=starFishCenterY+sizeArcStarFishMoveY*sin(radians(starFishAngle)); // assign a sin wave movement to the variable that determines the Y location of the starfish in the animation
starFishAngle+=starFishAngleSpeed; // increment the angle of the starfish cos and sin wave movements the random speed assigned to the starfish speed variable
speedXstarFish+=0.5+(random(-0.8, 0.8)); // assign a value that is a random value added to a constant to move the starfish horizontally across the screen
}
// method to display the Starfish
void display () { /// create a variable that will refer to the function used to display the starfish
strokeJoin(ROUND);
strokeWeight (4);
stroke(1);
fill(clrStarFish);
beginShape();
vertex(starFishX, starFishY+24);
vertex(starFishX+20, starFishY+32);
vertex(starFishX+27, starFishY);
vertex(starFishX+34, starFishY+32);
vertex(starFishX+54, starFishY+24);
vertex(starFishX+42, starFishY+54);
vertex(starFishX+50, starFishY+96);
vertex(starFishX+27, starFishY+72);
vertex(starFishX+4, starFishY+96);
vertex(starFishX+12, starFishY+54);
endShape(CLOSE); // end the recording of the vertices for the Starfish class and closes the shape
} // end the block of code of the display method for the class called Starfish
} // end the block for the class called Starfish
// Turtle class
class Turtle { // create a class called Turtle
// all of the fields for the class called Turtle
float turtleX;
float turtleY;
color clrTurtle;
float angle; // declare the variable that will control the position of the sin wave for the turtle
float turtleMiddleYPos; // declare the variable that will determine the Y position from which the sin motion occurs for the turtle
float turtleYRangeOfMotion; // declare the variable that will determine the radius of the sin motion for the turtle
float speedOfSinWaveMovement; // declare the variable that will impart a change in the angle that controls the sin wave for the turtle
float turtleLocX; // declare the variable that determines the X location of the turtle in the animation
float turtleLocY; // declare the variable that determines the Y location of the turtle in the animation
float speedXTurtle; // declare the variable that determines the speed of the turtle's horizontal movement
// constructor for the class called Turtle
Turtle (color clrTurtletemp, float turtleXtemp, float turtleYtemp) { // create the constructor for the Turtle class and declare its temporary variables
turtleX=turtleXtemp;
turtleY=turtleYtemp;
clrTurtle=clrTurtletemp; // initialize the variable that controls the color parameter of the turtle class
turtleLocX=0.0-140; // initialize the content of global variable for the X location of the turtle
angle=0.0; // initialize the value of the angle of the sin wave that has a zero value
turtleMiddleYPos=height/2-58; // initialize that determines the Y position from which the sin motion occurs so that it is in the center taking into account the height of the turtle divided by 2 (116%2=58)
turtleYRangeOfMotion=height/2.6; // initialize a value for the range of Y movement of the turtle on the sin wave that makes it cover most of the height but stays inside the window
speedOfSinWaveMovement=0.005; // initialize a value for the speed of the change of angle of the sin wave that is relatively slow
} // end the block of code of the constructor for the class called Turtle
// method to move the Turtle
void move () {
turtleX=turtleLocX+speedXTurtle;
turtleY=turtleLocY;
turtleLocY=turtleMiddleYPos+sin(angle)*turtleYRangeOfMotion; // calculates the Y location of the turtle on the sin wave movement
angle+=speedOfSinWaveMovement; // increment the angle of the sin wave by the speed factor every time the draw () method is called
speedXTurtle+=0.5; //increment the speed of the turtle's horizontal movement
}
// method to display the Turtle
void display () { /// create a variable that will refer to the function used to display the starfish
// assign parameters for the color and stroke of the solid turtle figure
fill(clrTurtle);
; // set color of solid figure to a turtle-like green
stroke(0); // set color of stroke to black for the solid figure outlines
smooth(); // set anti-aliased edges for the solid turtle figure
strokeWeight (3); // set the thickness of the stroke that delineated the solid turtle figure
//draw the rear legs of the turtle with triangle
triangle (turtleX+28, turtleY+18, turtleX+40, turtleY+10, turtleX, turtleY+4);
triangle (turtleX+28, turtleY+18, turtleX+22, turtleY+52, turtleX+52, turtleY+62);
// draw the top flapper underneath the body and head
beginShape();
curveVertex (turtleX+122, turtleY+25);
curveVertex (turtleX+122, turtleY+25);
curveVertex (turtleX+128, turtleY+21);
curveVertex (turtleX+138, turtleY-8);
curveVertex (turtleX+115, turtleY-30);
curveVertex (turtleX+125, turtleY-15);
curveVertex (turtleX+113, turtleY+7);
curveVertex (turtleX+113, turtleY+7);
endShape(CLOSE); // end the recording of the vertices for the top flapper of the Turtle class and closes the shape
// draw the bottom flapper underneath the body and head
beginShape();
curveVertex (turtleX+131, turtleY+23);
curveVertex (turtleX+131, turtleY+23);
curveVertex (turtleX+136, turtleY+27);
curveVertex (turtleX+138, turtleY+42);
curveVertex (turtleX+130, turtleY+60);
curveVertex (turtleX+118, turtleY+85);
curveVertex (turtleX+119, turtleY+86);
curveVertex (turtleX+115, turtleY+78);
curveVertex (turtleX+124, turtleY+25);
curveVertex (turtleX+124, turtleY+25);
endShape(CLOSE); // end the recording of the vertices for the bottom flapper of the Turtle class and closes the shape
// draw the body of the turtle with 11 curveVertices points
beginShape ();
//end of the turtle's tail
curveVertex (turtleX, turtleY+30);
curveVertex (turtleX, turtleY+30);
//top of turtle
curveVertex (turtleX+12, turtleY+20);
curveVertex (turtleX+50, turtleY+8);
curveVertex (turtleX+118, turtleY);
// head of turtle and twist in the turtle's neck
curveVertex (turtleX+152, turtleY+30);
curveVertex (turtleX+166, turtleY+8);
curveVertex (turtleX+146, turtleY+10);
//bottom of turtle
curveVertex (turtleX+126, turtleY+40);
curveVertex (turtleX+80, turtleY+52);
curveVertex (turtleX+20, turtleY+35);
// part of turtle's tail that connects to its end
curveVertex (turtleX+12, turtleY+25);
curveVertex (turtleX+12, turtleY+25);
endShape(CLOSE); // end the recording of the vertices for the body of the Turtle class and closes the shape
// draw the eye and the smile of the turtle
fill(232, 221, 7); // make eye color brown
ellipse (turtleX+158, turtleY+14, 2, 2); // draw small eye
noStroke(); // eliminate stroke for the mouth
fill(93, 9, 0); // make mouth pinky red
arc(turtleX+156, turtleY+24, 6, 6, QUARTER_PI, PI+QUARTER_PI); //draw arc of mouth
} // end the block of code of the display method for the class called Turtle
} // end the block for the class called Turtle
// Fish class
class Fish { // create a class called Fish
// all of the fields for the class called Turtle
float fishX;
float fishY;
color clrFish;
float fishAngleSpeed; // declare the variable that will allow for the incrementation of the angle for the fish circular movements
float fishCenterX; // declare the variable that will determine the X position from which the cos motion occurs for the fish
float fishCenterY; // declare the variable that will determine the Y position from which the sin motion occurs for the fish
float sizeArcFishMoveX; // declare the variable that will determine the radius of the cos motion for the fish swimming back and forth in circles
float sizeArcFishMoveY; // declare the variable that will determine the radius of the sin motion for the fish swimming back and forth in circles
float fishLocX; // declare the variable that determines the X location of the fish in the animation
float fishLocY; // declare the variable that determines the Y location of the fish in the animation
float speedXFish; // declare the variable that determines the speed applied to the location of the fish in the animation (in this case only the X location)
// constructor for the class called Fish
Fish (color clrFishtemp, float fishXtemp, float fishYtemp) { // create the constructor for the Turtle class and declare its temporary variables
fishX=fishXtemp;
fishY=fishYtemp;
clrFish=clrFishtemp; // initialize the variable that controls the color parameter of the turtle class
fishAngleSpeed=0.4; // assign a value of 0.4 to the change of angle used in the cos and sign wave movement of the fish
fishCenterX=0.0; // assign a value of 0 to the variable that will determine the X position from which the cos motion occurs for the fish
fishCenterY=height/2; // assign a value of half the height of the canvas to the variable that will determine the Y position from which the sin motion occurs for the fish
sizeArcFishMoveX=width/3; // assign a value of a third of the width of the canvas to the variable that will determine the radius of the cos wave motion of the fish
sizeArcFishMoveY=height/3; // assign a value of a third of the height of the canvas to the variable that will determine the radius of the sin wave motion of the fish
} // end the block of code of the constructor for the class called Fish
// method to move the Fish
void move () {
fishX=fishLocX+speedXFish;
fishY=fishLocY;
fishLocX=fishCenterX+sizeArcFishMoveX*cos(radians(fishAngle)); // assign a cos wave movement to the variable that determines the X location of the fish in the animation
fishLocY=fishCenterY+sizeArcFishMoveY*sin(radians(fishAngle)); // assign a sin wave movement to the variable that determines the Y location of the fish in the animation
fishAngle+=fishAngleSpeed; // increment the angle of the fish cos and sin wave movements by 0.4
speedXFish+=(random(0.1, 0.8)); // assign a value that is a random value to move the starfish horizontally across the screen
}
// method to display the Fish
void display () { /// create a variable that will refer to the function used to display the Fish
fill(clrFish);
stroke(1);
strokeWeight (3);
beginShape();
vertex(fishX, fishY);
vertex(fishX+50, fishY+40);
vertex(fishX+105, fishY+20);
vertex(fishX+150, fishY+50);
vertex(fishX+90, fishY+80);
vertex(fishX+55, fishY+70);
vertex(fishX+30, fishY+90);
endShape(CLOSE);
arc (fishX+116, fishY+42, 10, 15, 0, PI);
} // end the block of code of the display method for the class called Fish
} // end the block for the class called Fish
// Reverse Fish class
class FishReverse { // create a class called ReverseFish to show the fish in reverse
// all of the fields for the class called FishReverse
float fishXR;
float fishYR;
color clrFishR;
float fishAngleSpeedR; // declare the variable that will allow for the incrementation of the angle for the fish circular movements
float fishCenterXR; // declare the variable that will determine the X position from which the cos motion occurs for the fish
float fishCenterYR; // declare the variable that will determine the Y position from which the sin motion occurs for the fish
float sizeArcFishMoveXR; // declare the variable that will determine the radius of the cos motion for the fish swimming back and forth in circles
float sizeArcFishMoveYR; // declare the variable that will determine the radius of the sin motion for the fish swimming back and forth in circles
float fishLocXR; // declare the variable that determines the X location of the fish in the animation
float fishLocYR; // declare the variable that determines the Y location of the fish in the animation
float speedXFishR; // declare the variable that determines the speed applied to the location of the fish in the animation (in this case only the X location)
// constructor for the class called Reverse Fish
FishReverse (color clrFishtempR, float fishXtempR, float fishYtempR) { // create the constructor for the Turtle class and declare its temporary variables
fishXR=fishXtempR;
fishYR=fishYtempR;
clrFishR=clrFishtempR; // initialize the variable that controls the color parameter of the turtle class
fishAngleSpeedR=0.4; // assign a value of 0.4 to the change of angle used in the cos and sign wave movement of the fish
fishCenterXR=0.0; // assign a value of 0 to the variable that will determine the X position from which the cos motion occurs for the fish
fishCenterYR=height/2; // assign a value of half the height of the canvas to the variable that will determine the Y position from which the sin motion occurs for the fish
sizeArcFishMoveXR=width/3; // assign a value of a third of the width of the canvas to the variable that will determine the radius of the cos wave motion of the fish
sizeArcFishMoveYR=height/3; // assign a value of a third of the height of the canvas to the variable that will determine the radius of the sin wave motion of the fish
} // end the block of code of the constructor for the class called Fish
// method to move the Fish
void move () {
fishXR=fishLocXR+speedXFishR;
fishYR=fishLocYR;
fishLocXR=fishCenterXR+sizeArcFishMoveXR*cos(radians(fishAngle)); // assign a cos wave movement to the variable that determines the X location of the fish in the animation
fishLocYR=fishCenterYR+sizeArcFishMoveYR*sin(radians(fishAngle)); // assign a sin wave movement to the variable that determines the Y location of the fish in the animation
fishAngle+=fishAngleSpeedR; // increment the angle of the fish cos and sin wave movements by 0.4
speedXFishR+=(random(0.1, 0.8)); // assign a value that is a random value to move the starfish horizontally across the screen
}
// method to display the Reverse Fish
void display () {
fill(clrFishR);
stroke(1);
strokeWeight (3);
beginShape();
vertex(fishXR, fishYR+50);
vertex(fishXR+45, fishYR+20);
vertex(fishXR+100, fishYR+40);
vertex(fishXR+150, fishYR);
vertex(fishXR+120, fishYR+90);
vertex(fishXR+95, fishYR+70);
vertex(fishXR+60, fishYR+80);
endShape(CLOSE);
arc (fishXR+34, fishYR+42, 10, 15, 0, PI);
}
}
1