//////////////////////////////Ball Tag////////////////////////////// //You are in control of a little blue ball, which you use to catch// //the red or green circles. Catching a red circle will make you // //grow, and catching a green circle will make you shrink. Keep // //going until you no longer can! Use the arrow keys to move. // //////////////////////////////////////////////////////////////////// //VARIABLES //Circle variables float circlePositionRedX, circlePositionRedY; float circlePositionGreenX, circlePositionGreenY; float circleRadius = 20; float circleSpeed = 4.5; //Ball Variables float ballPositionX, ballPositionY; float ballRadius = 10; float ballSpeed =2; float c1x, c1y; float c2x, c2y; float r1, r2; //////////////////////////////////////////////////////////////////////////// //SETUP void setup() { size (400, 400); updateBall(); updateRedCircle(); updateGreenCircle(); } //////////////////////////////////////////////////////////////////////////// //UPDATE //where the ball is at the start - center of the screen void updateBall() { ballPositionX = random (200, 200); ballPositionY = random (200, 200); } //where the red circle is at the start - offscreen void updateRedCircle() { circlePositionRedX = random (0, height+1); circlePositionRedY = random (0, height-1); } //where the green circle is at the start - offscreen void updateGreenCircle() { circlePositionGreenX = random (0, height+1); circlePositionGreenY = random (0, height-1); } //if the red circle overlaps with the blue ball, the blue ball grows larger void redOverlap() { if (ballGrow(ballPositionX, ballPositionY, ballRadius, circlePositionRedX, circlePositionRedY, circleRadius)) { ballRadius+=0.07; } } //if the green circle overlaps with the blue ball, the blue ball grows smaller void greenOverlap() { if (ballShrink(ballPositionX, ballPositionY, ballRadius, circlePositionGreenX, circlePositionGreenY, circleRadius)) { ballRadius-=0.07; } } //////////////////////////////////////////////////////////////////////////// //DRAW void draw() { background (230); displayBallMovement(); displayBall(); displayRedCircle(); displayGreenCircle(); displayRedCircleMovement(); displayGreenCircleMovement(); displayBallOnScreen(); displayRedCircleOnScreen(); displayGreenCircleOnScreen(); redOverlap(); greenOverlap(); } //////////////////////////////////////////////////////////////////////////// //FUNCTIONS void displayBallMovement() { // moves the ball when an arrow key is pressed; ex, goes left when left key is pressed if (keyPressed) { if (keyCode == UP) { ballPositionY-=ballSpeed; } else if (keyCode == DOWN) { ballPositionY+=ballSpeed; } else if (keyCode == RIGHT) { ballPositionX+=ballSpeed; } else if (keyCode == LEFT) { ballPositionX-=ballSpeed; } } } void displayBall() { //appearance of ball noStroke(); //body fill(0, 150, 200); ellipse(ballPositionX, ballPositionY, ballRadius, ballRadius); } void displayRedCircle() { //appearance of red circle - going from left to right noStroke(); fill(150, 0, 0); ellipse(circlePositionRedX, circlePositionRedY, circleRadius, circleRadius); } void displayGreenCircle() { //appearance of green circle - going from top to bottom noStroke(); fill(0, 150, 0); ellipse(circlePositionGreenX, circlePositionGreenY, circleRadius, circleRadius); } void displayRedCircleMovement() { //controls the movement of red circle //go from left to right circlePositionRedX+=circleSpeed; //make it reappear after going offscreen if (circlePositionRedX >+450) { circlePositionRedX = width - 350; circlePositionRedY = random (0, 420); } } void displayGreenCircleMovement() { //controls the movement of green circle //go from top to bottom circlePositionGreenY+=circleSpeed; //make it reappear after going offscreen if (circlePositionGreenY >+450) { circlePositionGreenY = height - 350; circlePositionGreenX = random (0, 420); } } void displayBallOnScreen() { //to keep the Ball from moving offscreen - stops it at the edge if (ballPositionY < 10) { ballPositionY = 10; } else { if (ballPositionY > 390) { ballPositionY = 390; } else { if (ballPositionX < 10) { ballPositionX = 10; } else { if (ballPositionX > 390) { ballPositionX = 390; } } } } } void displayRedCircleOnScreen() { //to keep the red circle from appearing half offscreen if (circlePositionRedY < 150) { circlePositionRedY = 150; } else { if (circlePositionRedY > 270) { circlePositionRedY = 270; } else { if (circlePositionRedX < 60) { circlePositionRedX = 60; } } } } void displayGreenCircleOnScreen() { //to keep the green circle from appearing half offscreen if (circlePositionGreenY < 50) { circlePositionGreenY = 50; } else { if (circlePositionGreenX > 270) { circlePositionGreenX = 270; } else { if (circlePositionGreenX < 20) { circlePositionGreenX = 20; } } } } //////////////////////////////////////////////////////////////////////////// //INTERSECTIONS //make the ball grow when it intersects with red boolean ballGrow(float c1x, float c1y, float r1, float c2x, float c2y, float r2) { if (dist(c1x, c1y, c2x, c2y) < r1 + r2) { return true; } else { return false; } } //make the ball shrink when it intersects with green boolean ballShrink(float c1x, float c1y, float r1, float c2x, float c2y, float r2) { if (dist(c1x, c1y, c2x, c2y) < r1 + r2) { return true; } else { return false; } } ////////////////////////////////////////////////////////////////////////////