// Bouncing ball simulation // Variables int time; int ballX = 200; int ballY = 150; int ball1Width = 30; int ball1Red = 255; int ball1Green = 255; int ball1Blue = 255; int ball2X = 50; int ball2Y = 50; int ball2Width = 45; int ball2Red = 255; int ball2Green = 255; int ball2Blue = 255; int moveAmount2 = 1; int fallAmount2 = 5; int moveAmount = 2; int fallAmount = 1; int windowWidth = 600; int windowHeight = 400; void setup() { size(windowWidth, windowHeight); smooth(); // get rid of jagged edges time = 0; } void draw () { // Draw ball at current position background(150); drawBallOne(); drawBallTwo(); time = time + 1; // Update clock } void drawBallOne() { fill(ball1Red, ball1Green, ball1Blue); ellipse(ballX, ballY, ball1Width, ball1Width); if (time % 50 == 0) { // Accelerate fall every 50th time step fallAmount = fallAmount + 1; if (fallAmount > 10) { fallAmount = 10; } } // 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; if (distanceToLeftEdge < 15 || distanceToRightEdge < 15) { // Reverse direction moveAmount = -1 * moveAmount; } int distanceToBottom = windowHeight - ballY; int distanceToTop = ballY; if (distanceToTop < 15 || distanceToBottom < 15) { fallAmount = -1 * fallAmount; // Change ball color ball1Red = (int)random(256); ball1Green = (int)random(256); ball1Blue = (int)random(256); } } void drawBallTwo() { fill(ball2Red, ball2Green, ball2Blue); ellipse(ball2X, ball2Y, ball2Width, ball2Width); // Move ball for next call to draw() ball2X = ball2X + moveAmount2; ball2Y = ball2Y + fallAmount2; // Make sure ball doesn't move offscreen int distanceToRightEdge = windowWidth - ball2X; int distanceToLeftEdge = ball2X; if (distanceToLeftEdge < 15 || distanceToRightEdge < 15) { // Reverse direction moveAmount2 = -1 * moveAmount2; } int distanceToBottom = windowHeight - ball2Y; int distanceToTop = ball2Y; if (distanceToTop < 15 || distanceToBottom < 15) { fallAmount2 = -1 * fallAmount2; // Change ball color ball2Red = (int)random(256); ball2Green = (int)random(256); ball2Blue = (int)random(256); } }