xxxxxxxxxx
float x, y; //defining of the variables as floats since decimals are used in the software
float x_velocity, y_velocity;
float gravity;
float radius;
void setup()
{
size(600, 300);
x = 2; //sets the flies horizontal starting pixel
y = 10; //sets the flys vertical starting pixel
x_velocity = 10;//horizontal velocity preset
y_velocity = 20; //vertical velocity preset
gravity = .3; //defines the increase per cycle to the y value used later
radius = 10; //radius of the ellipse
}
void draw()
{
background(200);
y_velocity += gravity; //constantly ad to the y value, moving it downwarsd on the canvas acting liek gravity
x += x_velocity ;// make th efly constantly moving to the right
y += y_velocity ; //the fly is constantly accelerating
if ( y>(height-radius) ) //when the fly hits the bottom of the screen this statement makes it bounce back
{
y = height-radius;//stops the fly moving any more in its original direction
y_velocity *= -0.7;
}
if ( x<radius ) //makes the fly bounce off the left wall at the distance of the radius of the ellipse that makes the fly
{
x = radius;
x_velocity *= -0.5;
}
if ( x>(width-radius) ) //if the fly touched the right side fo the screen it will be cicked into the opposite direction
{
x = width-radius; //sets the flys cordinates to the side fo the canvas, essentially stoping it form moving anymore in the original direction
x_velocity *= -0.5;
}
if(y<(0+radius)){ //when the fly touched the top of the canvas at its radius, it will bounce downwards
y=0+radius;//stops the fly at the contact position
y_velocity*=-.5;
}
if(mousePressed == true){ //when the mouse is clicked the fly will accelerate
y+=.5;
y_velocity*=1.1; //its vertical velocity will get a large kick
x+=random(-.4,.7); //the fly will jump to the left or right depending on hoe long you hold the mouse
x_velocity*=1.1; //moves the fly to the right
}
if( mouseButton == RIGHT){ //when the mouse is right clicked the fly will be drawn in the top left corner
x=0;
y=0;
x_velocity += 2.1; //pushes the fly to the right so it won't drop and get stuck in verticle motion
}
fly(x,y); //Calls the fly function with the parameters of x and y decided above
}
void ball(float x, float y) //defining a ball function originally used in the code but now subsituted with a fly
{
pushMatrix();//isolates the translatio neffects to just the objects enclosed
translate(x, y); //make the origin that the ball is drawn in the variables x and y
ellipseMode(RADIUS); //sets the cordinate system of the ball to the center of the circle
ellipse(0, 0, radius, radius); //draws the ellipse with respects to the translated cordinates
popMatrix();
}
void fly(float x, float y){//defiens the fly finction and gives it the arguments of x and y, the values it will be expecting when called
ellipseMode(CENTER); //sets the cordinates of the ellipses to the center of the ellipse so the two move correctly
ellipse(x,y,5,20);//wings
fill(0);//fills the fly in black
ellipse(x,y,10,13);//body
}