xxxxxxxxxx
float x, y;
float V;
float Vx, Vy;
float Real;
float R;
float ballR;
float ballx;
float bally;
float Dxball;
float Dyball;
String First = "Slower? :P";//50
String Second = "I want to live :(";//100
String Third = "Seriously Stop!!";//300
String Last = "Game Over";//600
String AfterLast = "because winning is from connection, not size";//600
void setup(){
size(800, 600);
noStroke();
x = width/2;
y = height/2;
Real = 10;
ballx = random(0, width); // (ballx, bally) is the random position of the green ball
bally = random(0, height);
ballR = random(7, R); // ballR is the radius of the green ball being between 7 pixels and the size of the red ball
}
void draw(){
/* -------------------------| Defining Variables |------------------------- */
V = 50/Real + 1; // V is the speed of the red ball in respect to its "Real" size
float Dx = mouseX - x; // Dx and Dy are the distance from the mouse to the position of the red ball
float Dy = mouseY - y;
float D = sqrt(sq(Dx)+sq(Dy)); // Using pythagoras, D is the exact distance between the mouse pointer and the red ball
Vx = (V/D)*Dx; // because our canvas works in the rectangular coordinates (x, y), we are defining the speed on each x and y separately
Vy = (V/D)*Dy; // Vx, Vy are the x and y speed of the red ball, defined by the total speed "V" and D/Dx and D/Dy (which combined are the normalized vector)
float Dxball = ballx - x; // (Dxball, Dyball) is the (x, y) distance between the two balls
float Dyball = bally - y;
float ballD = sqrt(sq(Dxball)+sq(Dyball)); // using pythagoras, ballD is the total distance
/* -----------------| Displaying the objects on the screen |----------------- */
background(255);
fill(200, 0, 0);
ellipse(x, y, 2*R, 2*R); // the red ball
fill(0, 150, 100);
ellipse(ballx, bally, 2*ballR, 2*ballR); // the green ball
/* --------------------------| Creating the logic |-------------------------- */
x+=Vx;
y+=Vy;
if(ballD < abs(R) - abs(ballR)){ // if red eats green
Real = sqrt(sq(R)+sq(ballR)); // pythagoras is also used on finding the combined area using the radius
ballx = random(0, width); // spawning the green ball in a random position
bally = random(0, height);
ballR = random(9, R);
}
if(R < Real){ // this allows the effect of seeing the red ball being incremented
R+= 2.5;
}
/* -----------------| Displaying the text messages on the screen |----------------- */
fill(0);
textAlign(CORNER);
textSize(16);
text("Your Size: " + R, 10, 20);
fill(255);
textAlign(CENTER);
if(R < 50){
textSize(ballR);
text("._.", ballx, bally);
}
else if(R < 100){
textSize(ballR/3);
text(First, ballx, bally);
}
else if(R < 200){
textSize(ballR/4);
text(Second, ballx, bally);
}else if(R < 400){
textSize(ballR/4);
text(Third, ballx, bally);
}else if(R < 700){
textSize(ballR/5);
text("Bruh!!!", ballx, bally);
}else {
fill(0);
background(200);
textSize(70);
text(Last, width/2, height/2);
textSize(30);
text(AfterLast, width/2, height - 10);
}
}
void keyPressed(){
if(key == 'r'){
restart();
}
}
void restart(){
x = width/2;
y = height/2;
Real = 10;
R = Real;
ballx = random(0, width);
bally = random(0, height);
ballR = random(3, R);
}