Java 多次单击后冻结移动矩形
对于我的项目,我们应该为移动矩形指定一个介于1和3之间的随机值。每次单击后,应减少冻结所需的单击次数。当计数达到0时,它冻结。如果冻结的矩形被另一个矩形触摸,它应该重新开始移动,并根据需要冻结的点击量设置一个新的随机数。我被困在如何让它冻结与多次点击Java 多次单击后冻结移动矩形,java,random,rectangles,stddraw,Java,Random,Rectangles,Stddraw,对于我的项目,我们应该为移动矩形指定一个介于1和3之间的随机值。每次单击后,应减少冻结所需的单击次数。当计数达到0时,它冻结。如果冻结的矩形被另一个矩形触摸,它应该重新开始移动,并根据需要冻结的点击量设置一个新的随机数。我被困在如何让它冻结与多次点击 import java.util.Random; public class Main { public static void main(String[] args) { MovingRectangle[] rectangles = n
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;
公共班机{
公共静态void main(字符串[]args){
移动矩形[]矩形=新的移动矩形[5];
随机rng=新随机();
对于(int i=0;i3){
点击状态=3;
}
}否则{
单击状态=0;
}
如果(单击状态==3){
clickCount=true;
}
if(StdDraw.mousePressed()
&&矩形[i].containsPoint(StdDraw.mouseX(),StdDraw.mouseY())和&(clickCount=true)){
矩形[i]。冻结();
}
矩形[i].draw();
矩形[i].update();
if(矩形[i].isfreezed()){
frozenCount++;
}
对于(int j=i+1;j
}
import java.util.Random;
公共类移动矩形{
私人双x;
私人双y;
专用双vx;
私人双重身份;
私有布尔值被冻结;
私有布尔冻结;
私人int red;
私人室内绿化;
私人int蓝色;
私人双半宽;
私人双半身;
随机rng;
公共移动矩形(双x、双y、双vx、,
双宽、双宽、双高){
这个.x=x;
这个。y=y;
这是vx=vx;
this.vy=vy;
this.isfreezed=false;
此值为0.halfWidth=宽度/2;
这一点。半高=高度/2;
rng=新随机数();
随机颜色();
}
公共颜色(){
红色=rng.nextInt(256);
绿色=rng.nextInt(256);
蓝色=rng.nextInt(256);
}
公众抽签(){
int a=rng.nextInt(4);
如果(已冻结){
标准颜色(标准红色);
}否则{
标准颜色(红色、绿色、蓝色);
}
标准圆角直角(x、y、半宽、半高);
标准颜色(标准黑色);
//标准文本(x,y,“”+a);
标准设定半径(0.01);
标准颜色(标准黑色);
标准矩形(x,y,半宽,半高);
}
公共无效更新(){
如果(已冻结){
返回;
}
x+=vx;
y+=vy;
如果(x-半宽度<0){
vx*=-1;
x=0+半宽;
随机颜色();
}
如果(x+半宽度>1){
vx*=-1;
x=1——半宽;
随机颜色();
}
如果(y-半高<0){
vy*=-1;
y=0+半高;
随机颜色();
}
如果(y+半高>1){
vy*=-1;
y=1——半高;
随机颜色();
}
}
公众假期冻结{
isfreezed=true;
}
公开作废解冻(){
如果(!已冻结){
返回;
}
isfreezed=false;
}
公共布尔值已冻结(){
//如果矩形冻结,则返回true
退货已冻结;
}
公共布尔containsPoint(双a,双b){
//当且仅当点(a,b)为
//包含在矩形内
返回a>x-半宽度//左边缘
&&ay-半高//底边
&&bother.x&&yother.y;
}
公共布尔atSameLocation(移动矩形其他){
返回this.x==other.getX()&&this.y==other.getY();
}
}
跟踪在MovingRectangle对象类中单击矩形的次数。您需要引用该对象数据以确定是否应调用冻结方法
MovingRectangle的构造函数应该初始化将其冻结为1到3之间的值所需的单击次数
为此,你需要在电影中使用“getter”和“setter”
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();
}
}
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)
rng.nextInt(3) + 1
);
}
while (true) {
StdDraw.clear(StdDraw.GRAY);
if (StdDraw.mousePressed()) {
}
// where check click status
int clickCount = 0;
int clickStatus = 0;
int frozenCount = 0;
for (int i = 0; i < rectangles.length; i++) {
if (StdDraw.mousePressed()) {
++clickStatus;
if (clickStatus > 2) {
clickStatus = 2;
}
} else {
clickStatus = 0;
}
if(clickStatus == 1) {
clickCount++;
clickCount = 3;
}
if (StdDraw.mousePressed()
&& rectangles[i].containsPoint(StdDraw.mouseX(), StdDraw.mouseY())) {
rectangles[i].setclicksRemaining();
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();
rectangles[i].draw();
rectangles[j].draw();
}
}
}
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 {
Random rng;
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 int clicksRemaining;
private double halfWidth;
private double halfHeight;
// Your constructor for the MovingRectangle should
//initialize the number of clicks needed to freeze it to a value between 1 and 3.
public void clicksRemaining() {
clicksRemaining = rng.nextInt(3) + 1;
}
public void setclicksRemaining() {
if (clicksRemaining < 1) {
clicksRemaining = 1;
}
--clicksRemaining;
}
public int getclicksRemaining() {
return clicksRemaining;
}
public MovingRectangle(double x, double y, double vx,
double vy, double width, double height, int clicksRemaining) {
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
this.isFrozen = false;
this.halfWidth = width / 2;
this.halfHeight = height / 2;
this.clicksRemaining = clicksRemaining;
rng = new Random();
randomColor();
}
public void randomColor() {
red = rng.nextInt(256);
green = rng.nextInt(256);
blue = rng.nextInt(256);
}
public void draw() {
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, "" + clicksRemaining);
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;
}
clicksRemaining = rng.nextInt(3) + 1;
isFrozen = false;
}
public boolean isFrozen() {
return isFrozen;
}
public boolean containsPoint(double a, double b) {
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 int gethalfWidth() {
return (int) halfWidth;
}public int gethalfHeight() {
return (int) halfHeight;
}
public boolean collidesWith(MovingRectangle other) {
return x - halfWidth < other.x + other.halfWidth && x + halfWidth > other.x - other.halfWidth && y - halfHeight < other.y +
other.halfHeight && y + halfHeight > other.y - halfHeight;
}