From 5451bafed39bd5291801d9e051c1f3c062059544 Mon Sep 17 00:00:00 2001 From: bingor_yhj Date: Wed, 5 Oct 2022 21:01:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9D=A6=E5=85=8B=E5=A4=A7=E6=88=98(=E4=B8=80?= =?UTF-8?q?=E6=9C=9F)-=E8=A7=A3=E5=86=B3new=20Rectangle=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/msb/Bullet.java | 20 ++++++++++++++++---- src/com/msb/Tank.java | 15 +++++++++++---- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/com/msb/Bullet.java b/src/com/msb/Bullet.java index 314d003..d23e419 100644 --- a/src/com/msb/Bullet.java +++ b/src/com/msb/Bullet.java @@ -18,7 +18,7 @@ public class Bullet { private boolean live = true; private GroupEnum group = GroupEnum.BAD; private TankFrame tankFrame; -// private Rectangle rectangle; + private Rectangle rectangle = new Rectangle(); public Bullet(int x, int y, DirEnum dir, GroupEnum group, TankFrame tankFrame) { this.x = x; @@ -26,7 +26,11 @@ public class Bullet { this.dir = dir; this.group = group; 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) { @@ -61,6 +65,9 @@ public class Bullet { 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; } @@ -79,13 +86,18 @@ public class Bullet { if(this.group == tank.getGroup()) return; //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); if(bullteRec.intersects(tankRec)) { -// if(this.rectangle.intersects(tank.getRectangle())) { + this.die(); + tank.die(); + }*/ + + if(rectangle.intersects(tank.getRectangle())) { this.die(); tank.die(); } + } private void die() { diff --git a/src/com/msb/Tank.java b/src/com/msb/Tank.java index cd8bdb8..9904705 100644 --- a/src/com/msb/Tank.java +++ b/src/com/msb/Tank.java @@ -22,7 +22,7 @@ public class Tank { private boolean live = true; private Random random = new Random(); 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) { this.x = x; @@ -30,7 +30,11 @@ public class Tank { this.dir = dir; this.group = group; 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) { @@ -71,6 +75,9 @@ public class Tank { default: break; } + rectangle.x = this.x; + rectangle.y = this.y; + //敌方坦克随机发射炮弹 if(this.group == GroupEnum.BAD && random.nextInt(100) > 95) this.fire(); //敌方坦克随机改变方向进行 @@ -120,8 +127,8 @@ public class Tank { return group; } - /*public Rectangle getRectangle() { + public Rectangle getRectangle() { return rectangle; - }*/ + } }