// declare global variables
int xspeed, yspeed;
int xpos,ypos,wdth,ht;
// initialize sketch
void setup() {
// set sketch window size + background color
size(700,700);
background(150);
// ball speed
xspeed=3;
yspeed=6;
// ball size
wdth = 60;
ht=60;
// turn off shapestroke rendering
noStroke ();
// initial ball placement
xpos=width/8;
ypos=height/8;
frameRate(60);
}
// begin animation loop
void draw(){
// draw ball
smooth ();
fill ((random (126)),(random (156)),(random (156)));
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/9 || xpos<=wdth/9){
xspeed*=-1;
}
if (ypos>=height-ht/9 || ypos<=ht/9){
yspeed*=-8;
}
}
No comments:
Post a Comment