// Bouncing ball simulation // Variables int ballX = 200; int ballY = 150; int ballWidth = 30; int moveAmount = 2; int fallAmount = 1; int windowWidth = 600; int windowHeight = 400; void setup() { size(windowWidth, windowHeight); smooth(); // get rid of jagged edges } void draw () { // Draw ball at current position background(150); ellipse(ballX, ballY, ballWidth, ballWidth); // Move ball for next call to draw() ballX = ballX + moveAmount; ballY = ballY + fallAmount; // Make sure ball doesn't move offscreen int distanceToRightEdge = windowWidth - ballX; int distanceToLeftEdge = ballX; int distanceToSide = abs(distanceToRightEdge - distanceToLeftEdge); if (distanceToSide > (windowWidth - ballWidth) ) { // Reverse direction moveAmount = -1 * moveAmount; } int distanceToBottom = windowHeight - ballY; int distanceToTop = ballY; int distanceToEdge = abs(distanceToBottom - distanceToTop); if (distanceToEdge > (windowHeight - ballWidth)) { fallAmount = -1 * fallAmount; // Change ball color int redComponent = (int)random(256); int greenComponent = (int)random(256); int blueComponent = (int)random(256); fill(redComponent, greenComponent, blueComponent); } }