arrays - Creating graphics objects that never overlap java -
i'm making small balloon game in java , trying write code, when create new 'balloon' objects don't overlap on screen.
the code have far is:
public void newgame(){ ui.cleargraphics(); this.currentscore = 0; this.totalpopped = 0; (int = 0; < this.balloons.length-1; i++) { this.balloons[i] = new balloon(50 + math.random()*400, 50 + math.random()*400); (int j = + 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 balloon. high score = "+this.highscore); }
using draw , istouching methods:
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 balloon touching other balloon, , false otherwise * returns false if either balloon 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); }
how can write this, when balloons created, none of them touching each other?
right have 2 loops. in first loop balloons created. in second loop, every balloon tested against every other loop. test in first loop: after creating new balloon, test against existing balloons.
Comments
Post a Comment