// declare global variables
int xspeed, yspeed;
int xpos,ypos,wdth,ht;
// initialize sketch
void setup() {
// set sketch window size + background color
size(1200,750);
background(156);
// ball speed
xspeed=11;
yspeed=121;
// ball size
wdth = 50;
ht=70;
// turn off shapestroke rendering
noStroke ();
// initial ball placement
xpos=width/65;
ypos=height/35;
frameRate(67);
}
// begin animation loop
void draw(){
// draw ball
smooth ();
fill ((random (100)),(random (100)),(random (100)));
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/78 || xpos<=wdth/2){
xspeed*=-1;
}
if (ypos>=height-ht/567 || ypos<=ht/2){
yspeed*=-1;
}
}
No hay comentarios:
Publicar un comentario