FAQ
 
 
Cover  \ Exhibition  \ Learning  \ Reference  \ Download  \ Discourse   
    Examples \ Tutorials \ Courses & Workshops
 
   
 
 
Examples for Processing (BETA) version 91+. If you have a previous version, use the examples included with your software. If you see any errors or have comments, please let us know.



Bounce
by REAS <http://reas.com>

When the shape hits the edge of the window, it reverses its direction

Updated 1 September 2002

   
// Bounce 
// by REAS <http://reas.com> 

int size = 60;       // Width of the shape 
float xpos, ypos;    // Starting position of shape    
 
float xspeed = 2.8;  // Speed of the shape 
float yspeed = 2.2;  // Speed of the shape 
 
int xdirection = 1;  // Left or Right 
int ydirection = 1;  // Top to Bottom 
 
 
void setup() 
{ 
  size(200, 200); 
  noStroke(); 
  framerate(30); 
  smooth(); 
  // Set the starting position of the shape 
  xpos = width/2; 
  ypos = height/2; 
} 
 
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-size || xpos < 0) { 
    xdirection *= -1; 
  } 
  if (ypos > height-size || ypos < 0) { 
    ydirection *= -1; 
  } 
 
  // Draw the shape 
  ellipse(xpos+size/2, ypos+size/2, size, size); 
} 
 







   
  Processing is an open project initiated by Ben Fry and Casey Reas  
  © Info