坦克大战(一期)-敌人坦克随机动-进行2次调整

Network
bingor_yhj 2 years ago
parent 4cd5736900
commit 9ab12006b7

@ -18,6 +18,7 @@ public class Bullet {
private boolean live = true; private boolean live = true;
private GroupEnum group = GroupEnum.BAD; private GroupEnum group = GroupEnum.BAD;
private TankFrame tankFrame; private TankFrame tankFrame;
// private Rectangle rectangle;
public Bullet(int x, int y, DirEnum dir, GroupEnum group, TankFrame tankFrame) { public Bullet(int x, int y, DirEnum dir, GroupEnum group, TankFrame tankFrame) {
this.x = x; this.x = x;
@ -25,6 +26,7 @@ public class Bullet {
this.dir = dir; this.dir = dir;
this.group = group; this.group = group;
this.tankFrame = tankFrame; this.tankFrame = tankFrame;
// this.rectangle = new Rectangle(x, y, WIDTH, HEIGHT);
} }
public void paint(Graphics g) { public void paint(Graphics g) {
@ -80,6 +82,7 @@ public class Bullet {
Rectangle bullteRec = new Rectangle(this.x, this.y, WIDTH, HEIGHT); Rectangle bullteRec = new Rectangle(this.x, this.y, WIDTH, HEIGHT);
Rectangle tankRec = new Rectangle(tank.getX(), tank.getY(), Tank.WIDTH, Tank.HEIGHT); Rectangle tankRec = new Rectangle(tank.getX(), tank.getY(), Tank.WIDTH, Tank.HEIGHT);
if(bullteRec.intersects(tankRec)) { if(bullteRec.intersects(tankRec)) {
// if(this.rectangle.intersects(tank.getRectangle())) {
this.die(); this.die();
tank.die(); tank.die();
} }

@ -22,6 +22,7 @@ public class Tank {
private boolean live = true; private boolean live = true;
private Random random = new Random(); private Random random = new Random();
private GroupEnum group = GroupEnum.BAD; private GroupEnum group = GroupEnum.BAD;
// private Rectangle rectangle;
public Tank(int x, int y, DirEnum dir, GroupEnum group, TankFrame tankFrame) { public Tank(int x, int y, DirEnum dir, GroupEnum group, TankFrame tankFrame) {
this.x = x; this.x = x;
@ -29,6 +30,7 @@ public class Tank {
this.dir = dir; this.dir = dir;
this.group = group; this.group = group;
this.tankFrame = tankFrame; this.tankFrame = tankFrame;
// this.rectangle = new Rectangle(x, y, WIDTH, HEIGHT);
} }
public void paint(Graphics g) { public void paint(Graphics g) {
@ -52,6 +54,10 @@ public class Tank {
this.dir = dir; this.dir = dir;
} }
public DirEnum getDir() {
return dir;
}
public void moving() { public void moving() {
if( ! move) return; if( ! move) return;
@ -64,8 +70,17 @@ public class Tank {
default: break; default: break;
} }
if(random.nextInt(10) > 8) this.fire(); // this.boundsCheck();
//敌方坦克随机发射炮弹
if(this.group == GroupEnum.BAD && random.nextInt(100) > 95) this.fire();
//敌方坦克随机改变方向进行
if(this.group == GroupEnum.BAD && random.nextInt(100) > 95) this.randomDir();
}
public void randomDir() {
this.dir = DirEnum.values()[random.nextInt(4)];
} }
public void setMove(boolean move) { public void setMove(boolean move) {
@ -79,6 +94,12 @@ public class Tank {
tankFrame.bullets.add(new Bullet(bX, bY, this.dir, this.group, tankFrame)); tankFrame.bullets.add(new Bullet(bX, bY, this.dir, this.group, tankFrame));
} }
public void boundsCheck() {
if(x<0 || y<0 || x>TankFrame.GAME_WIDTH || y>TankFrame.GAME_HEIGHT) {
this.setDir(DirEnum.valueOf(random.nextInt(30)%4));
}
}
public int getX() { public int getX() {
return x; return x;
} }
@ -95,4 +116,9 @@ public class Tank {
public GroupEnum getGroup() { public GroupEnum getGroup() {
return group; return group;
} }
/*public Rectangle getRectangle() {
return rectangle;
}*/
} }

@ -16,7 +16,8 @@ public class TankDemo {
//创建5个敌方坦克 //创建5个敌方坦克
for (int i=0; i<5; i++) { for (int i=0; i<5; i++) {
tankFrame.tanks.add(new Tank(100 + i*80, 100, DirEnum.valueOf(random.nextInt(3)), GroupEnum.BAD, tankFrame)); // tankFrame.tanks.add(new Tank(100 + i*80, 100, DirEnum.valueOf(random.nextInt(30)%4), GroupEnum.BAD, tankFrame));
tankFrame.tanks.add(new Tank(100 + i*80, 100, DirEnum.DOWN, GroupEnum.BAD, tankFrame));
} }
//现实当中,虽然可以过按键来改变方块的坐标并且重新刷新画板来实现移动,但是敌方的坦克应该是自动在跑 //现实当中,虽然可以过按键来改变方块的坐标并且重新刷新画板来实现移动,但是敌方的坦克应该是自动在跑

@ -53,6 +53,7 @@ public class TankFrame extends Frame {
g.setColor(Color.WHITE); g.setColor(Color.WHITE);
g.drawString("子弹的数量:" + bullets.size(), 10, 60); g.drawString("子弹的数量:" + bullets.size(), 10, 60);
g.drawString("敌方坦克数量:" + tanks.size(), 10, 90); g.drawString("敌方坦克数量:" + tanks.size(), 10, 90);
g.drawString("爆炸的数量:" + explodes.size(), 10, 120);
g.setColor(color); g.setColor(color);
myTank.paint(g); myTank.paint(g);

Loading…
Cancel
Save