效果如下:[

](http://www.wjgbaby.com/wp-content/uploads/2017/08/17080501-300x285.gif)
](http://www.wjgbaby.com/wp-content/uploads/2017/08/17080501-300x285.gif)
代码在下面,我已经加了很多注释了:

//小球的位置和速度
float x=100;
float y=100;
float xspeed=1;
float yspeed=3;

//setup启动时调用一次
void setup()
{
size(200,200); //窗口尺寸大小
smooth(); //开启平滑模式
background(255); //背景色
}

//draw启动后一直调用
void draw()
{
background(255);

//根据速度移动小球
x=x+xspeed;
y=y+yspeed;

//检测边缘,用来改变速度方向
if((x>width)||(x<0))
{
xspeed=xspeed*-1;
}
if((y>width)||(y<0))
{
yspeed=yspeed*-1;
}

stroke(0); //边框颜色
fill(175); //填充颜色
ellipse(x,y,16,16); //在(x,y)位置绘制小球

}