자바에서 작은 풍선 게임을 만들고 코드를 작성하려고합니다. 따라서 새로운 '풍선'객체를 만들 때 화면에서 겹치지 않게 만듭니다.절대 겹치지 않는 그래픽 객체 만들기 Java
내가 지금까지 가지고있는 코드는 다음과 같습니다
public void newGame(){
UI.clearGraphics();
this.currentScore = 0;
this.totalPopped = 0;
for (int i = 0; i < this.balloons.length-1; i++) {
this.balloons[i] = new Balloon(50 + Math.random()*400, 50 + Math.random()*400);
for (int j = i + 1; j < this.balloons.length; j++) {
if (this.balloons[i] !=null && this.balloons[j] != null && this.balloons[i].isTouching(balloons[j])) {
this.balloons[j] = new Balloon(50 + Math.random()*400, 50+ Math.random()*400);
}
}
this.balloons[i].draw();
}
UI.printMessage("New game: click on a balloon. High score = "+this.highScore);
}
은 무 isTouching 방법 사용 :
public void draw(){
UI.setColor(color);
UI.fillOval(centerX-radius, centerY-radius, radius*2, radius*2);
if (!this.popped){
UI.setColor(Color.black);
UI.drawOval(centerX-radius, centerY-radius, radius*2, radius*2);
}
}
/** Returns true if this Balloon is touching the other balloon, and false otherwise
* Returns false if either balloon is popped. */
public boolean isTouching(Balloon other){
if (this.popped || other.popped) return false;
double dx = other.centerX - this.centerX;
double dy = other.centerY - this.centerY;
double dist = other.radius + this.radius;
return (Math.hypot(dx,dy) < dist);
}
어떻게 풍선을 만들 때 그래서 나는, 그들 중 누구도 이것을 쓸 수 없습니다 서로 만지고 있니?
당신은 간단히 ballon의 경계 Rectangle을 계산하고 intersects 메서드를 사용하여 겹치기를 결정할 수 있습니다. – MadProgrammer