如何完成一个弹弹球

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;
}
}

小小代码不成敬意

首先,我们先定义一些变量,因为是一个球所以使用了circle显得更加直观

circle_x就是圆的X轴,circle_y同理

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

然后使用void setup函数定义窗口大小

使用void draw函数来创建循环

然后画一个圆,圆的X轴和Y轴随着move来移动

如果圆的X轴在窗口最上方,则往相反方向移动;若圆的X轴小于0,则向反方向移动

Y轴同理

注意在向相反方向移动之前,要归位(窗口最高值或0)

PS:hight , width为程序自带的变量,意思分别是窗口的宽度和高度

猜你喜欢

转载自www.cnblogs.com/lbtbk/p/10327797.html