四处碰壁的小球

碰到墙壁就弹开的程序,定义两个速度,分别为x轴和y轴的速度。

float x = 100;


float y = 100;


float xspeed = 1;


float yspeed = 3.3;

int diameter = 20;


void setup(){
size(400,400);

smooth();

background(255);


  }

void draw(){
background(255);


 x = x+xspeed;


 y = y+yspeed;


 if((x >width - diameter/2)||(x <diameter/2)){ xspeed = xspeed * -1;


  } if((y >height - diameter / 2)||(y <diameter / 2)){
  yspeed = yspeed * -1;

 
 }
stroke(0);

 fill(175);

 ellipse(x,y,diameter,diameter);


  }

Capture

发表评论