本文主要是介绍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 入门教程(十) 弹弹球的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!