坦克大战(一期)-解决new Rectangle的问题

Network
bingor_yhj 2 years ago
parent 90f675cbdb
commit 5451bafed3

@ -18,7 +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; private Rectangle rectangle = new 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;
@ -26,7 +26,11 @@ 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);
rectangle.x = this.x;
rectangle.x = this.y;
rectangle.width = WIDTH;
rectangle.height = HEIGHT;
} }
public void paint(Graphics g) { public void paint(Graphics g) {
@ -61,6 +65,9 @@ public class Bullet {
default: break; default: break;
} }
rectangle.x = this.x;
rectangle.y = this.y;
if(x<0 || y<0 || x>TankFrame.GAME_WIDTH || y>TankFrame.GAME_HEIGHT) this.live = false; if(x<0 || y<0 || x>TankFrame.GAME_WIDTH || y>TankFrame.GAME_HEIGHT) this.live = false;
} }
@ -79,13 +86,18 @@ public class Bullet {
if(this.group == tank.getGroup()) return; if(this.group == tank.getGroup()) return;
//TODO 每次碰撞的时候都小new 一个 Rectangle,应该用一个Rectangle来记录子弹的位置产生内存占用垃圾回收器会时不时运行一下 //TODO 每次碰撞的时候都小new 一个 Rectangle,应该用一个Rectangle来记录子弹的位置产生内存占用垃圾回收器会时不时运行一下
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();
tank.die();
}*/
if(rectangle.intersects(tank.getRectangle())) {
this.die(); this.die();
tank.die(); tank.die();
} }
} }
private void die() { private void die() {

@ -22,7 +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; private Rectangle rectangle = new 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;
@ -30,7 +30,11 @@ 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);
rectangle.x = this.x;
rectangle.x = this.y;
rectangle.width = WIDTH;
rectangle.height = HEIGHT;
} }
public void paint(Graphics g) { public void paint(Graphics g) {
@ -71,6 +75,9 @@ public class Tank {
default: break; default: break;
} }
rectangle.x = this.x;
rectangle.y = this.y;
//敌方坦克随机发射炮弹 //敌方坦克随机发射炮弹
if(this.group == GroupEnum.BAD && random.nextInt(100) > 95) this.fire(); if(this.group == GroupEnum.BAD && random.nextInt(100) > 95) this.fire();
//敌方坦克随机改变方向进行 //敌方坦克随机改变方向进行
@ -120,8 +127,8 @@ public class Tank {
return group; return group;
} }
/*public Rectangle getRectangle() { public Rectangle getRectangle() {
return rectangle; return rectangle;
}*/ }
} }

Loading…
Cancel
Save