Source code: (Versión a partir del ejercicio de Ira Greenberg)
//
declare global variables
int xspeed, yspeed;
int xpos,ypos,wdth,ht;
//
initialize sketch
void setup() {
//
set sketch window size + background color
size(600,400);
background(50);
// ball speed
xspeed=3;
yspeed=6;
// ball size
wdth = 60;
ht=60;
// turn off shapestroke
rendering
noStroke ();
// initial
ball placement
xpos=width/2;
ypos=height/2;
frameRate(30);
}
//
begin animation loop
void draw(){
// draw ball
smooth ();
fill
((random (256)),(random (256)),(random (256)));
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;
}
}