// Bounce // by REAS <http://reas.com> // When the shape hits the edge of the window, it reverses its direction // Updated 1 September 2002 int iSize = 60; // Width of the shape double xpos, ypos; // Starting position of shape double xspeed = 2.8; // Speed of the shape double yspeed = 2.2; // Speed of the shape int xdirection = 1; // Left or Right int ydirection = 1; // Top to Bottom public override void setup() { size(200, 200); noStroke(); framerate(30); smooth(); // Set the starting position of the shape xpos = width/2; ypos = height/2; } public override void draw() { background(102); // Update the position of the shape xpos = xpos + ( xspeed * xdirection ); ypos = ypos + ( yspeed * ydirection ); // Test to see if the shape exceeds the boundaries of the screen // If it does, reverse its direction by multiplying by -1 if (xpos > width-iSize || xpos < 0) { xdirection *= -1; } if (ypos > height-iSize || ypos < 0) { ydirection *= -1; } // Draw the shape ellipse(xpos+iSize/2, ypos+iSize/2, iSize, iSize); }