/* title: bouncing ball description: ball deflects off sketch window edges created: August 9, 2005 by: Ira Greenberg modified: September 6, 2006, ever so slightly, by Fred Green */ // declare global variables int xspeed, yspeed; int xpos, ypos, wdth, ht; //initialize sketch void setup(){ //set sketch window size and background color size(400, 400); background(0); //ball speed xspeed = 4; yspeed = 6; //ball size wdth = 20; ht = 20; // turn off shape stroke rendering noStroke(); //initial ball placement xpos = width/2; ypos = height/2; //set the animation loop speed frameRate(30); smooth(); } // begin animation loop void draw(){ //update background background(0); //draw ball fill(200, 200, 0); ellipse(xpos, ypos, wdth, ht); //upgrade position values xpos += xspeed; ypos += yspeed; /*conditionals detects ball collission with sketch window edges also accounts for thickness of ball */ if (xpos>=width-wdth/2 || xpos<=wdth/2){ xspeed *= -1; } if (ypos>=height-ht/2 || ypos<=ht/2){ yspeed *= -1; } }