私のプロジェクトでは、移動する四角形に 1 ~ 3 のランダムな値を割り当てることになっています。クリックするたびに、フリーズするために必要なクリック数が減るはずです。カウントが 0 になるとフリーズします。凍結した四角形が別の長方形に触れた場合、凍結に必要なクリック数の新しい乱数で再び移動を開始する必要があります。複数のクリックでフリーズする方法に行き詰まっています。
import java.util.Random;
public class Main {
public static void main(String[] args) {
MovingRectangle[] rectangles = new MovingRectangle[5];
Random rng = new Random();
for (int i = 0; i < rectangles.length; i++) {
rectangles[i] = new MovingRectangle(
rng.nextDouble(), // x in [0, 1)
rng.nextDouble(), // y in [0, 1)
rng.nextDouble() * 0.02 - 0.01, // vx in [-0.01, 0.01)
rng.nextDouble() * 0.02 - 0.01, // vy in [-0.01, 0.01)
rng.nextDouble() * 0.2 + 0.1, // width in [0.1, 0.3)
rng.nextDouble() * 0.2 + 0.1 // height in [0.1, 0.3)
);
}
while (true) {
StdDraw.clear(StdDraw.GRAY);
boolean clickCount = false;
int clickStatus = rng.nextInt(3);
int frozenCount = 0;
for (int i = 0; i < rectangles.length; i++) {
if (StdDraw.mousePressed()) {
++clickStatus;
if (clickStatus > 3) {
clickStatus = 3;
}
} else {
clickStatus = 0;
}
if(clickStatus == 3) {
clickCount = true;
}
if (StdDraw.mousePressed()
&& rectangles[i].containsPoint(StdDraw.mouseX(), StdDraw.mouseY()) && (clickCount = true)) {
rectangles[i].freeze();
}
rectangles[i].draw();
rectangles[i].update();
if (rectangles[i].isFrozen()) {
frozenCount++;
}
for (int j = i+1; j < rectangles.length; j++) {
if (rectangles[i].collidesWith(rectangles[j])) {
rectangles[i].unfreeze();
rectangles[j].unfreeze();
}
}
}
StdDraw.setPenColor(StdDraw.WHITE);
StdDraw.filledRectangle(0.2, 0.03, 0.2, 0.03);
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.textLeft(0.0, 0.03, "Rectangles remaining: " +
(rectangles.length - frozenCount));
if (frozenCount == rectangles.length) {
StdDraw.setPenColor(StdDraw.WHITE);
StdDraw.filledRectangle(0.5, 0.5, 0.1, 0.05);
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.text(0.5, 0.5, "You Win!");
}
StdDraw.show(25);
}
}
}
import java.util.Random;
public class MovingRectangle {
private double x;
private double y;
private double vx;
private double vy;
private boolean isFrozen;
private boolean frozen;
private int red;
private int green;
private int blue;
private double halfWidth;
private double halfHeight;
Random rng;
public MovingRectangle(double x, double y, double vx,
double vy, double width, double height) {
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
this.isFrozen = false;
this.halfWidth = width / 2;
this.halfHeight = height / 2;
rng = new Random();
randomColor();
}
public void randomColor() {
red = rng.nextInt(256);
green = rng.nextInt(256);
blue = rng.nextInt(256);
}
public void draw() {
int a = rng.nextInt(4);
if (isFrozen) {
StdDraw.setPenColor(StdDraw.RED);
} else {
StdDraw.setPenColor(red, green, blue);
}
StdDraw.filledRectangle(x, y, halfWidth, halfHeight);
StdDraw.setPenColor(StdDraw.BLACK);
//StdDraw.text(x, y, "" + a);
StdDraw.setPenRadius(0.01);
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.rectangle(x, y, halfWidth, halfHeight);
}
public void update() {
if (isFrozen) {
return;
}
x += vx;
y += vy;
if (x - halfWidth < 0) {
vx *= -1;
x = 0 + halfWidth;
randomColor();
}
if (x + halfWidth > 1) {
vx *= -1;
x = 1 - halfWidth;
randomColor();
}
if (y - halfHeight < 0) {
vy *= -1;
y = 0 + halfHeight;
randomColor();
}
if (y + halfHeight > 1) {
vy *= -1;
y = 1 - halfHeight;
randomColor();
}
}
public void freeze() {
isFrozen = true;
}
public void unfreeze() {
if (!isFrozen) {
return;
}
isFrozen = false;
}
public boolean isFrozen() {
// returns true if the rectangle is frozen
return isFrozen;
}
public boolean containsPoint(double a, double b) {
// Returns true if and only if the point (a, b)
// is contained inside the rectangle
return a > x - halfWidth // left edge
&& a < x + halfWidth // right edge
&& b > y - halfHeight // bottom edge
&& b < y + halfHeight; // top edge
}
public int getX() {
return (int) x;
}public int getY() {
return (int) y;
}
public boolean collidesWith(MovingRectangle other) {
return x < other.x + other.halfWidth && x + halfWidth > other.x && y < other.y +
other.halfHeight && y + halfHeight > other.y;
}
public boolean atSameLocation(MovingRectangle other) {
return this.x == other.getX() && this.y == other.getY();
}
}