I am trying to write a program that randomly generates 100 colored moving balls and a black ball that is faster and bigger than them. The black ball should swallow every ball it passes completely over, and when it does, its radius should increase and its speed should decrease. The ball it swallows should disappear. Also, every time a colored ball hits the edges of the region, a new colored ball should be formed. If black ball can swallow every colorful ball, the program should stop. I wrote a code, but I couldn't get the program I wanted. I'm very very far away from that. Obviously I couldn't figure out the mechanics of swallowing and erasing the balls. But I structured the skeleton of objects. Also there seems to be a problem when drawing the balls, for some reason the color of the bigger and faster ball is not black. Can anyone help?
(I already imported the StdDraw library, so it isn't the problem)
import java.util.Random;
import java.awt.Color;
public class BallEating {
private double x, y, vx, vy, r;
private Color c;
private static Color[] ballColors;
private static BallEating[] balls;
private static int number;
private static BallEating eatingBall;
public BallEating(double x, double y, double vx, double vy, double r, Color c) {
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
this.r = r;
this.c = c;
}
public void move() {
x = x + vx;
y = y + vy;
}
public void draw() {
StdDraw.filledCircle(getX(), getY(), getR());
StdDraw.setPenColor(getC());
/*
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
*/
}
public void drawBlackOne() {
StdDraw.filledCircle(eatingBall.getX(), eatingBall.getY(), eatingBall.getR());
StdDraw.setPenColor(eatingBall.getC());
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getVy() {
return vy;
}
public double getVx() {
return vx;
}
public double getR() {
return r;
}
public Color getC() {
return c;
}
public void setC(Color c) {
this.c = c;
}
public void setX(double x) {
this.x = x;
}
public void setY(double newY) {
y = newY;
}
public void setVx(double newVx) {
vx = newVx;
}
public void setVy(double newVy) {
vy = newVy;
}
public void setR(double newR) {
r = newR;
}
static {
ballColors = new Color[]{StdDraw.GREEN, StdDraw.RED, StdDraw.BLUE};
eatingBall = new BallEating(0.5, 0.5, 0.02, 0.05, 0.2, StdDraw.BLACK);
}
public static void setImage() {
StdDraw.enableDoubleBuffering();
StdDraw.setCanvasSize(500, 500);
StdDraw.setXscale(-1.0, 1.0);
StdDraw.setYscale(-1.0, 1.0);
}
private static void initialize() {
setImage();
number = 100;
balls = new BallEating[number];
Random random = new Random();
for (int i = 0; i < number; i++) {
balls[i] = new BallEating(0, 0, 0, 0, 0, StdDraw.WHITE);
balls[i].setVx(random.nextDouble() * 0.04 - 0.02);
balls[i].setVy(random.nextDouble() * 0.04 - 0.02);
balls[i].setR(random.nextDouble() * 0.08 + 0.02);
balls[i].setC(ballColors[(random.nextInt(3))]);
do {
balls[i].setX(random.nextDouble() * 2 - 1);
} while (Math.abs(balls[i].getX()) > (1 - balls[i].getR()));
do {
balls[i].setY(random.nextDouble() * 2 - 1);
} while (Math.abs(balls[i].getY()) > (1 - balls[i].getR()));
}
}
private static void loop() {
StdDraw.clear(StdDraw.BOOK_LIGHT_BLUE);
//eatingBall = new BallEating(0.5, 0.5, 0.05, 0.05, 0.2, StdDraw.BLACK);
eatingBall.move();
if (Math.abs(eatingBall.getX() + eatingBall.getVx()) > (1.0 - eatingBall.getR())) {
eatingBall.setVx((-1)*eatingBall.getVx());
}
if (Math.abs(eatingBall.getY() + eatingBall.getVy()) > (1.0 - eatingBall.getR())) {
eatingBall.setVy((-1)*eatingBall.getVy());
}
eatingBall.drawBlackOne();
for (int i = 0; i < number; i++) {
BallEating ball = balls[i];
ball.move();
if (Math.abs(ball.getX() + ball.getVx()) > (1.0 - ball.getR())) {
ball.setVx((-1)*ball.getVx());
}
if (Math.abs(ball.getY() + ball.getVy()) > (1.0 - ball.getR())) {
ball.setVy((-1)*ball.getVy());
}
}
for (int i = 0; i < number; i++) {
balls[i].draw();
}
StdDraw.show();
StdDraw.pause(20);
}
public static void blackEatingBall() {
//eatingBall = new BallEating(0.5, 0.5, 0.05, 0.05, 0.2, StdDraw.BLACK);
for(int i = 0; i < number; i++) {
BallEating ball = balls[i];
boolean a = true;
if(eatingBall.getX() == ball.getX() && eatingBall.getY() == ball.getY() && eatingBall.getR() > ball.getR()) {
eatingBall.setR(ball.getR() / 2 + eatingBall.getR());
eatingBall.setVx(eatingBall.getVx() / 1.2);
eatingBall.setVy(eatingBall.getVy() / 1.2);
balls[i] = null;
number--;
}
}
}
public static void main(String[] args) {
initialize();
while (true) {
blackEatingBall();
loop();
}
}
}