求助?!
初学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;
}