admin 管理员组

文章数量: 1087649

Processing 入门教程(十) 弹弹球

实现一个球在屏幕内不停的反弹力效果,跟大神写的就是不一样,感觉自己写的真的是渣啊,不过写完之后发现跟大神的思路基本是一样的,开心心

我自己的代码:

float circle_x = 0;void setup() {size(400, 400);noStroke();fill(#C1FF3E);
}
float moveX = 2;
float moveY = 2;
float radiu = 50;
float radiuPosX = 50;
float radiuPosY = 0;
boolean isXSub= true;
boolean isYSub= true;
void draw() {background(#1BB1F5);    ellipse(radiuPosX, radiuPosY, radiu, radiu); if (isXSub) {radiuPosX += moveX;} else {radiuPosX -= moveX;}if (radiuPosX > width) {radiuPosX = width;isXSub = false;}if (radiuPosX < 0) {radiuPosX = 0;isXSub= true;}if (isYSub) {radiuPosY += moveY;} else {radiuPosY -= moveY;}if (radiuPosY > height) {radiuPosY = height;isYSub = false;}if (radiuPosY < 0) {radiuPosY =0;isYSub= true;}
}

大神写的代码:

// initial position for our circle
float circle_x = 300;
float circle_y = 20;
// how much to move the circle on each frame
float move_x = 2;
float move_y = -2;void setup() {size(400, 200);stroke(#D60DFF);strokeWeight(7);
}
void draw() {background(#21EA73);ellipse(circle_x, circle_y, 40, 40);circle_x = circle_x + move_x;circle_y = circle_y + move_y;if(circle_x > width) {circle_x = width;move_x = -move_x;println("too far right");}if(circle_y > height) {circle_y = height;move_y = -move_y;println("too far bottom");}if(circle_x < 0) {circle_x = 0;move_x = -move_x;println("too far left");}if(circle_y < 0) {circle_y = 0;move_y = -move_y;println("too far top");}
}

效果图如下:

本文标签: Processing 入门教程(十) 弹弹球