求助?!

qingchunweiwei2013 2012-10-07 05:01:56
初学java,这是模拟挖掘机挖金子的代码,遇到金子加1分,遇到炸弹减3分。(红色代表挖掘机,黄色代表金子,黑色代表炸弹)
下面是一个关键类的代码。问题是,如何让被碰的圆圈消失?(我的方法太烂,不要见笑)



import java.awt.Graphics;

public class Rect extends xg.sdjzu.edu.cn.Rect {

public void drawMySelf(Graphics g) {
g.setColor(Color.RED);
g.fillOval(diggerX, diggerY, diggerW, diggerH);
g.setColor(Color.YELLOW);
g.drawString(this.diggerStrength + "", diggerX + 5, diggerY + 15);
for (int i = 0; i < mapGold.length; i++) {
g.fillOval(mapGold[i][0], mapGold[i][1], mapElementW, mapElementH);
}
g.setColor(Color.BLACK);
for (int i = 0; i < mapBomb.length; i++) {
g.fillOval(mapBomb[i][0], mapBomb[i][1], mapElementW, mapElementH);
}
}

public void moveRight() {
this.diggerX += 5;
this.hitDetect();

}

public void moveLeft() {
this.diggerX -= 5;
this.hitDetect();

}

public void moveDown() {
this.diggerY += 5;
this.hitDetect();

}

public void moveUp() {

this.diggerY -= 5;
this.hitDetect();

}

private boolean hitDetect() {
boolean hit = false;
for (int i = 0; i < mapGold.length; i++) {
double DistanceOfCenter =Math.sqrt(Math.pow((this.diggerX+10) - (mapGold[i][0]+10),2)+Math.pow((this.diggerY+10) - (mapGold[i][1]+10),2));
boolean Hit = DistanceOfCenter <= 20;

if (Hit) {
hit = true;
this.diggerStrength++;
mapGold[i][0] = -100;
mapGold[i][1] = -100;
break;
}
}
for (int i = 0; i < mapBomb.length; i++) {
double DistanceOfCenter =Math.sqrt(Math.pow((this.diggerX+10) - (mapBomb[i][0]+10),2)+Math.pow((this.diggerY+10) - (mapBomb[i][1]+10),2));

boolean Hit = DistanceOfCenter <= 20;


if (Hit) {
hit = true;
this.diggerStrength -= 3;
mapBomb[i][0] = -100;
mapBomb[i][1] = -100;
break;
}
}

return hit;
}

private int diggerX = 20;
private int diggerY = 20;
private int diggerW = 20;
private int diggerH = 20;
private int diggerStrength = 10;

private int[][] mapGold = { { 70, 80 }, { 60, 50 }, { 120, 200 },
{ 200, 140 } };
private int[][] mapBomb = { { 90, 80 }, { 160, 50 }, { 220, 150 },
{ 100, 140 } };
private int mapElementW = 20;
private int mapElementH = 20;

}
...全文
122 2 打赏 收藏 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
qingchunweiwei2013 2012-10-09
  • 打赏
  • 举报
回复
可惜我还没学集合,不过还是多谢了。能不能用if else语句来解决这个问题呢?
wwwcomcn123 2012-10-07
  • 打赏
  • 举报
回复
将圆圈都放到集合里
用类 Rectangle吧
碰到后从集合中移除
然后从集合中画出圆圈

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧