Hi problem with falling ball
in
Programming Questions
•
2 months ago
class BouncingBall {
float x, y; // (x, y) is the center of the ball
float dx, dy;
float diameter;
color c;
// boolean shoot = false;
//constructor shorter code to intialize the vatiables
BouncingBall(float init_x, float init_y, float init_dx, float init_dy, float init_diameter, color init_c) {
x = init_x;
y = init_y;
dx = init_dx;
dy = init_dy;
c = init_c;
if (init_diameter <= 0) {
println("Error: diameter of a ball must always be positive");
exit(); // immediately quit the program
}
diameter = init_diameter;
}
void render() {
fill(c);
noStroke();
ellipse(x, y, diameter, diameter);
}
void update() { //update the position of the ball
// move the ball to its next position
x += dx;
y += dy;
// hit the top edge?
if (y < 0) {
y = 0;
dy = -dy;
}
//
// // hit the bottom edge?
// if (y > 499) {
// y = 499;
// dy = -dy;
// }
//
//
// hit the left edge?
if (x < 0) {
x = 0;
dx = -dx;
}
// hit the right edge?
if (x > 499) {
x = 499;
dx = -dx;
}
}
}
color randomColor() {
return color(random(0, 255), random(0, 255), random(0, 255));
}
BouncingBall randomBouncingBall() {
return new BouncingBall(random(50, 200), random(50, 200),
random(-2.0, 2.0), random(-2.0, 2.0),
random(25, 76),
color(random(0, 256),
random(0, 256),
random(0, 256))
);
}
ArrayList<BouncingBall> ballList; // initially null, ballList is a name ( can be any name)
int NUM_BALLS = 10;
void setup() {
size(500, 500);
// create the initially empty ArrayList of BouncingBall objects
ballList = new ArrayList<BouncingBall>(); //ballList is a name ( can be any name)
ballList.add(randomBouncingBall()); // add some BouncingBall
ballList.add(randomBouncingBall()); // objects
ballList.add(randomBouncingBall());
ballList.add(randomBouncingBall()); // add some BouncingBall
ballList.add(randomBouncingBall()); // objects
ballList.add(randomBouncingBall());
int i = 0;
while (i < NUM_BALLS) {
ballList.add(randomBouncingBall());
i += 1;
}
//shoot = false;
}
void draw() {
background(255);
drawGun();
// render and update all the balls
for (BouncingBall b : ballList) {
b.render();
b.update();
}
}
void drawGun() {
noStroke();
fill(0, 255, 0);
rect(mouseX, 480, 66, 20);
//if(shoot==true) {
//void mousePressed() {
//ballList.add(randomBouncingBall());
//}
//void setup() {
// size(500, 500);
// smooth();
//// ball1 = randomBouncingBall();
//// ball2 = randomBouncingBall();
//// ball3 = randomBouncingBall();
//// ball100 = randomBouncingBall();
// a = makeRandomBall();
// b = makeRandomBall();
// c = makeRandomBall();
//// a = new BouncingBall(250, 250, -1.25, 1, 50, color(0, 0 ,255));
//// b = new BouncingBall(350, 250, -1.04, -1.11, 50, color(255, 0 ,0));
//// c = new BouncingBall(350, 250, 1.04, -0.11, 50, color(0, 255, 0));
//}
//
//void draw () {
// background(255);
//
//// ball1.render();
//// ball1.update();
////
//// ball2.render();
//// ball2.update();
////
//// ball3.render();
//// ball3.update();
////
//// ball100.render();
//// ball100.update();
// b.render();
// b.update();
//
// a.render();
// a.update();
//
// c.render();
// c.update();
//}
}
Hi, there.
Above codes are my bouncing ball.
However, instead of making bouncing balls, how can I just make my random bouncing balls to falling balls?
I don't want them bouncing, but keep them falling from top to bottom and reset... I think the problem here is:
BouncingBall randomBouncingBall() {
return new BouncingBall(random(50, 200), random(50, 200),
random(-2.0, 2.0), random(-2.0, 2.0),
random(25, 76),
color(random(0, 256),
random(0, 256),
random(0, 256))
);
}
how do I make random falling ball instead of random bouncing balls
and how do I make them repeated ? after balls get to the bottom? reset the program?
Thank YOu.
1