xxxxxxxxxx
int b1x = random(3) + 3; //initializing and declaring global variables
int b1y = random(3) + 3;
int b2x = random(3) + 2;
int b2y = random(3) + 2;
int signb1x = +1;
int signb1y = +1;
int signb2x = +1;
int signb2y = +1;
int px = 150;
int py = 33;
int px2 = 200;
int py2 = 133;
int score = 0;
int time1 = 0;
int hasStarted = 2;
void setup() { //setting up canvas
size(270,270);
frameRate(60);
}
void draw() {
time1++;
noStroke();
background(100,100,150);
fill(200,200,255,150);
rect(10,10,width-20,height-20);
//////////// controls orange ball
if (signb1x == +1) { px += b1x;} //moves the ball
if (signb1y == +1) { py += b1y;}
if (signb1x == -1) { px -= b1x;}
if (signb1y == -1) { py -= b1y;}
strokeWeight(20); //draws the ball
stroke(#FF9900);
point(px , py);
if (py >= 250) {signb1y = -1;} //bounces the ball
if (px >= 250) {signb1x = -1;}
if (py <= 20) {signb1y = +1;}
if (px <= 20) {signb1x = +1;}
/////////// controls yellow ball
if (signb2x == +1) { px2 += b2x;} //moves the ball
if (signb2y == +1) { py2 += b2y;}
if (signb2x == -1) { px2 -= b2x;}
if (signb2y == -1) { py2 -= b2y;}
strokeWeight(20); //draws the ball
stroke(#FFFF00);
point(px2 , py2);
if (py2 >= 250) {signb2y = -1;} //bounces the ball
if (px2 >= 250) {signb2x = -1;}
if (py2 <= 20) {signb2y = +1;}
if (px2 <= 20) {signb2x = +1;}
textSize(24); //prints the score
text("Score: " + score, 20, 40);
if (hasStarted==1) { //increases the score
if ((time1 % 60) == 0) {score++;}
} else {
textSize(18);
fill(255,255,255,200);
text("Click on canvas to start.", 40, 225);
text("Try not to touch the balls.", 37, 250);
}
checkIfTouch(px,py,px2,py2,hasStarted); //checks if you have touched the balls
strokeWeight(10); //draws green ball at mouse
stroke(0,255,0);
point(mouseX,mouseY);
}
void checkIfTouch(int px, int py, int px2, int py2) { //checks if your mouse has touched the balls
int d1 = dist(mouseX,mouseY,px,py);
int d2 = dist(mouseX,mouseY,px2,py2);
if (((d1 <= 10) || (d2 <= 10)) && hasStarted == 1) {
noLoop();
}
}
void mouseClicked() { //starts the game when clicked
hasStarted = 1 ;
loop();
}