如何制作一个反弹球

float circle_x = 200;

float circle_y = 10;

float move_x = 2;

float move_y = 2;

void setup()

{

  size(500,300);

}

 void draw()

{

 background(225);

 ellipse(circle_x,circle_y,50,50);  

circle_x=circle_x+move_x;

 circle_y=circle_y+move_y;

 if(circle_x>width){

 circle_x=width;  

move_x=-move_x;

 }  

if(circle_x<0){

 circle_x=0;

 move_x=-move_x;  

}

 if(circle_y>height){  

circle_y=height;

 move_y=-move_y;

 }

 if(circle_y<0)

 {  

circle_y=0;

 move_y=-move_y;

 }

}

开始我们先定义变量,定义圆的X轴和Y轴和移动距离。

因为是一个球体所以使用circle。circle_x就是圆的x轴,circle_y就是圆的y轴。

move_x就是圆的X轴的移动的距离(及方向)move_y同样也是。

move_x=-move_x;就是改变圆的x轴的方向。move_y=-move_y;同样也是。

猜你喜欢

转载自www.cnblogs.com/wy051023/p/10328374.html