From c73612128ed25506e1708717983de238b301e07f Mon Sep 17 00:00:00 2001 From: bjmashibing Date: Sun, 5 May 2019 21:51:29 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E5=86=99=E5=BE=88=E5=A4=9A=E5=86=85=E5=AE=B9?= =?UTF-8?q?=EF=BC=8CPropertyMgr=E5=8F=AF=E4=BB=A5=E6=8F=90=E4=BE=9B?= =?UTF-8?q?=E6=9B=B4=E5=8F=8B=E5=A5=BD=E7=9A=84=E4=BD=BF=E7=94=A8=E6=96=B9?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tank/src/com/mashibing/tank/PropertyMgr.java | 4 ++++ tank/src/config | 6 +++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/tank/src/com/mashibing/tank/PropertyMgr.java b/tank/src/com/mashibing/tank/PropertyMgr.java index 4fd4e87..0f8b5e0 100644 --- a/tank/src/com/mashibing/tank/PropertyMgr.java +++ b/tank/src/com/mashibing/tank/PropertyMgr.java @@ -19,7 +19,11 @@ public static Object get(String key) { return props.get(key); } + //int getInt(key) + //getString(key) + public static void main(String[] args) { System.out.println(PropertyMgr.get("initTankCount")); + } } diff --git a/tank/src/config b/tank/src/config index 5b7e8c7..c910cf6 100644 --- a/tank/src/config +++ b/tank/src/config @@ -1,2 +1,6 @@ #tanks count at initialization -initTankCount=10 \ No newline at end of file +initTankCount=10 +tankSpeed=5 +bulletSpeed=10 +gameWidth=1080 +gameHeight=720 \ No newline at end of file From 7c6041895fec4988772aea39687d9cda04747fcb Mon Sep 17 00:00:00 2001 From: bjmashibing Date: Thu, 9 May 2019 21:18:01 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E5=8F=91=E5=B0=84=E7=82=AE=E5=BC=B9?= =?UTF-8?q?=E5=BA=94=E7=94=A8=E7=AD=96=E7=95=A5=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tank/src/com/mashibing/tank/Bullet.java | 2 ++ .../mashibing/tank/DefaultFireStrategy.java | 15 ++++++++ tank/src/com/mashibing/tank/FireStrategy.java | 5 +++ .../mashibing/tank/FourDirFireStrategy.java | 18 ++++++++++ tank/src/com/mashibing/tank/Tank.java | 34 +++++++++++++------ tank/src/config | 5 ++- 6 files changed, 68 insertions(+), 11 deletions(-) create mode 100644 tank/src/com/mashibing/tank/DefaultFireStrategy.java create mode 100644 tank/src/com/mashibing/tank/FireStrategy.java create mode 100644 tank/src/com/mashibing/tank/FourDirFireStrategy.java diff --git a/tank/src/com/mashibing/tank/Bullet.java b/tank/src/com/mashibing/tank/Bullet.java index 70b3425..fcdeeb7 100644 --- a/tank/src/com/mashibing/tank/Bullet.java +++ b/tank/src/com/mashibing/tank/Bullet.java @@ -28,6 +28,8 @@ public Bullet(int x, int y, Dir dir, Group group, TankFrame tf) { rect.y = this.y; rect.width = WIDTH; rect.height = HEIGHT; + + tf.bullets.add(this); } diff --git a/tank/src/com/mashibing/tank/DefaultFireStrategy.java b/tank/src/com/mashibing/tank/DefaultFireStrategy.java new file mode 100644 index 0000000..1781636 --- /dev/null +++ b/tank/src/com/mashibing/tank/DefaultFireStrategy.java @@ -0,0 +1,15 @@ +package com.mashibing.tank; + +public class DefaultFireStrategy implements FireStrategy { + + @Override + public void fire(Tank t) { + int bX = t.x + Tank.WIDTH/2 - Bullet.WIDTH/2; + int bY = t.y + Tank.HEIGHT/2 - Bullet.HEIGHT/2; + + new Bullet(bX, bY, t.dir, t.group, t.tf); + + if(t.group == Group.GOOD) new Thread(()->new Audio("audio/tank_fire.wav").play()).start(); + } + +} diff --git a/tank/src/com/mashibing/tank/FireStrategy.java b/tank/src/com/mashibing/tank/FireStrategy.java new file mode 100644 index 0000000..455cdbd --- /dev/null +++ b/tank/src/com/mashibing/tank/FireStrategy.java @@ -0,0 +1,5 @@ +package com.mashibing.tank; + +public interface FireStrategy { + void fire(Tank t); +} diff --git a/tank/src/com/mashibing/tank/FourDirFireStrategy.java b/tank/src/com/mashibing/tank/FourDirFireStrategy.java new file mode 100644 index 0000000..c716aa5 --- /dev/null +++ b/tank/src/com/mashibing/tank/FourDirFireStrategy.java @@ -0,0 +1,18 @@ +package com.mashibing.tank; + +public class FourDirFireStrategy implements FireStrategy { + + @Override + public void fire(Tank t) { + int bX = t.x + Tank.WIDTH/2 - Bullet.WIDTH/2; + int bY = t.y + Tank.HEIGHT/2 - Bullet.HEIGHT/2; + + Dir[] dirs = Dir.values(); + for(Dir dir : dirs) { + new Bullet(bX, bY, dir, t.group, t.tf); + } + + if(t.group == Group.GOOD) new Thread(()->new Audio("audio/tank_fire.wav").play()).start(); + } + +} diff --git a/tank/src/com/mashibing/tank/Tank.java b/tank/src/com/mashibing/tank/Tank.java index c995a59..d23177f 100644 --- a/tank/src/com/mashibing/tank/Tank.java +++ b/tank/src/com/mashibing/tank/Tank.java @@ -2,9 +2,12 @@ import java.awt.Graphics; import java.awt.Rectangle; +import java.lang.reflect.InvocationTargetException; import java.util.Random; public class Tank { + + private static final int SPEED = 2; public static int WIDTH = ResourceMgr.goodTankU.getWidth(); @@ -14,14 +17,16 @@ public class Tank { private Random random = new Random(); - private int x, y; + int x, y; - private Dir dir = Dir.DOWN; + Dir dir = Dir.DOWN; private boolean moving = true; - private TankFrame tf = null; + TankFrame tf = null; private boolean living = true; - private Group group = Group.BAD; + Group group = Group.BAD; + + FireStrategy fs; public Tank(int x, int y, Dir dir, Group group, TankFrame tf) { super(); @@ -35,14 +40,23 @@ public Tank(int x, int y, Dir dir, Group group, TankFrame tf) { rect.y = this.y; rect.width = WIDTH; rect.height = HEIGHT; + + if(group == Group.GOOD) { + String goodFSName = (String)PropertyMgr.get("goodFS"); + + try { + fs = (FireStrategy)Class.forName(goodFSName).getDeclaredConstructor().newInstance(); + } catch (Exception e) { + e.printStackTrace(); + } + + } else { + fs = new DefaultFireStrategy(); + } } + public void fire() { - int bX = this.x + Tank.WIDTH/2 - Bullet.WIDTH/2; - int bY = this.y + Tank.HEIGHT/2 - Bullet.HEIGHT/2; - - tf.bullets.add(new Bullet(bX, bY, this.dir, this.group, this.tf)); - - if(this.group == Group.GOOD) new Thread(()->new Audio("audio/tank_fire.wav").play()).start(); + fs.fire(this); } public Dir getDir() { diff --git a/tank/src/config b/tank/src/config index c910cf6..7277eaa 100644 --- a/tank/src/config +++ b/tank/src/config @@ -3,4 +3,7 @@ initTankCount=10 tankSpeed=5 bulletSpeed=10 gameWidth=1080 -gameHeight=720 \ No newline at end of file +gameHeight=720 +#fireStrategy +goodFS=com.mashibing.tank.FourDirFireStrategy +badFS=com.mashibing.tank.DefaultFireStrategy From 75678a46992befc2f76d385d52f8aa08ab292523 Mon Sep 17 00:00:00 2001 From: bjmashibing Date: Tue, 14 May 2019 20:24:20 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E6=8A=BD=E8=B1=A1=E5=87=BAGameModel?= =?UTF-8?q?=EF=BC=8C=E5=B0=86Model=E5=92=8CView=E5=88=86=E7=A6=BB=EF=BC=8C?= =?UTF-8?q?=E5=90=8C=E6=97=B6=EF=BC=8CGameModel=E4=BD=9C=E4=B8=BAFacade?= =?UTF-8?q?=EF=BC=8C=E8=B4=9F=E8=B4=A3=E4=B8=8EFrame=E6=89=93=E4=BA=A4?= =?UTF-8?q?=E9=81=93=EF=BC=8C=E5=90=8C=E6=97=B6=E8=B4=9F=E8=B4=A3=E5=86=85?= =?UTF-8?q?=E9=83=A8=E4=BA=8B=E5=8A=A1=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tank/src/com/mashibing/tank/Bullet.java | 12 ++-- .../mashibing/tank/DefaultFireStrategy.java | 2 +- tank/src/com/mashibing/tank/Explode.java | 8 +-- .../mashibing/tank/FourDirFireStrategy.java | 2 +- tank/src/com/mashibing/tank/GameModel.java | 66 +++++++++++++++++++ tank/src/com/mashibing/tank/Main.java | 5 -- tank/src/com/mashibing/tank/Tank.java | 9 +-- tank/src/com/mashibing/tank/TankFrame.java | 44 ++----------- 8 files changed, 88 insertions(+), 60 deletions(-) create mode 100644 tank/src/com/mashibing/tank/GameModel.java diff --git a/tank/src/com/mashibing/tank/Bullet.java b/tank/src/com/mashibing/tank/Bullet.java index fcdeeb7..ed26237 100644 --- a/tank/src/com/mashibing/tank/Bullet.java +++ b/tank/src/com/mashibing/tank/Bullet.java @@ -14,22 +14,22 @@ public class Bullet { private Dir dir; private boolean living = true; - TankFrame tf = null; + GameModel gm = null; private Group group = Group.BAD; - public Bullet(int x, int y, Dir dir, Group group, TankFrame tf) { + public Bullet(int x, int y, Dir dir, Group group, GameModel gm) { this.x = x; this.y = y; this.dir = dir; this.group = group; - this.tf = tf; + this.gm = gm; rect.x = this.x; rect.y = this.y; rect.width = WIDTH; rect.height = HEIGHT; - tf.bullets.add(this); + gm.bullets.add(this); } @@ -43,7 +43,7 @@ public void setGroup(Group group) { public void paint(Graphics g) { if(!living) { - tf.bullets.remove(this); + gm.bullets.remove(this); } switch(dir) { @@ -97,7 +97,7 @@ public void collideWith(Tank tank) { this.die(); int eX = tank.getX() + Tank.WIDTH/2 - Explode.WIDTH/2; int eY = tank.getY() + Tank.HEIGHT/2 - Explode.HEIGHT/2; - tf.explodes.add(new Explode(eX, eY, tf)); + gm.explodes.add(new Explode(eX, eY, gm)); } } diff --git a/tank/src/com/mashibing/tank/DefaultFireStrategy.java b/tank/src/com/mashibing/tank/DefaultFireStrategy.java index 1781636..e1cac11 100644 --- a/tank/src/com/mashibing/tank/DefaultFireStrategy.java +++ b/tank/src/com/mashibing/tank/DefaultFireStrategy.java @@ -7,7 +7,7 @@ public void fire(Tank t) { int bX = t.x + Tank.WIDTH/2 - Bullet.WIDTH/2; int bY = t.y + Tank.HEIGHT/2 - Bullet.HEIGHT/2; - new Bullet(bX, bY, t.dir, t.group, t.tf); + new Bullet(bX, bY, t.dir, t.group, t.gm); if(t.group == Group.GOOD) new Thread(()->new Audio("audio/tank_fire.wav").play()).start(); } diff --git a/tank/src/com/mashibing/tank/Explode.java b/tank/src/com/mashibing/tank/Explode.java index f32038c..caf3d06 100644 --- a/tank/src/com/mashibing/tank/Explode.java +++ b/tank/src/com/mashibing/tank/Explode.java @@ -10,14 +10,14 @@ public class Explode { private int x, y; //private boolean living = true; - TankFrame tf = null; + GameModel gm = null; private int step = 0; - public Explode(int x, int y, TankFrame tf) { + public Explode(int x, int y, GameModel gm) { this.x = x; this.y = y; - this.tf = tf; + this.gm = gm; new Thread(()->new Audio("audio/explode.wav").play()).start(); } @@ -29,7 +29,7 @@ public void paint(Graphics g) { g.drawImage(ResourceMgr.explodes[step++], x, y, null); if(step >= ResourceMgr.explodes.length) - tf.explodes.remove(this); + gm.explodes.remove(this); } diff --git a/tank/src/com/mashibing/tank/FourDirFireStrategy.java b/tank/src/com/mashibing/tank/FourDirFireStrategy.java index c716aa5..0f57660 100644 --- a/tank/src/com/mashibing/tank/FourDirFireStrategy.java +++ b/tank/src/com/mashibing/tank/FourDirFireStrategy.java @@ -9,7 +9,7 @@ public void fire(Tank t) { Dir[] dirs = Dir.values(); for(Dir dir : dirs) { - new Bullet(bX, bY, dir, t.group, t.tf); + new Bullet(bX, bY, dir, t.group, t.gm); } if(t.group == Group.GOOD) new Thread(()->new Audio("audio/tank_fire.wav").play()).start(); diff --git a/tank/src/com/mashibing/tank/GameModel.java b/tank/src/com/mashibing/tank/GameModel.java new file mode 100644 index 0000000..f4118ce --- /dev/null +++ b/tank/src/com/mashibing/tank/GameModel.java @@ -0,0 +1,66 @@ +package com.mashibing.tank; + +import java.awt.Color; +import java.awt.Graphics; +import java.util.ArrayList; +import java.util.List; + +public class GameModel { + + Tank myTank = new Tank(200, 400, Dir.DOWN, Group.GOOD, this); + + List bullets = new ArrayList<>(); + List tanks = new ArrayList<>(); + List explodes = new ArrayList<>(); + + public GameModel() { + int initTankCount = Integer.parseInt((String) PropertyMgr.get("initTankCount")); + + // 初始化敌方坦克 + for (int i = 0; i < initTankCount; i++) { + tanks.add(new Tank(50 + i * 80, 200, Dir.DOWN, Group.BAD, this)); + } + } + + public void paint(Graphics g) { + Color c = g.getColor(); + g.setColor(Color.WHITE); + g.drawString("子弹的数量:" + bullets.size(), 10, 60); + g.drawString("敌人的数量:" + tanks.size(), 10, 80); + g.drawString("爆炸的数量:" + explodes.size(), 10, 100); + g.setColor(c); + + myTank.paint(g); + for (int i = 0; i < bullets.size(); i++) { + bullets.get(i).paint(g); + } + + for (int i = 0; i < tanks.size(); i++) { + tanks.get(i).paint(g); + } + + for (int i = 0; i < explodes.size(); i++) { + explodes.get(i).paint(g); + } + // collision detect + + for (int i = 0; i < bullets.size(); i++) { + for (int j = 0; j < tanks.size(); j++) + bullets.get(i).collideWith(tanks.get(j)); + } + + // for(Iterator it = bullets.iterator(); it.hasNext();) { + // Bullet b = it.next(); + // if(!b.live) it.remove(); + // } + + // for(Bullet b : bullets) { + // b.paint(g); + // } + } + + public Tank getMainTank() { + return myTank; + } + +} diff --git a/tank/src/com/mashibing/tank/Main.java b/tank/src/com/mashibing/tank/Main.java index b4692ad..42b8388 100644 --- a/tank/src/com/mashibing/tank/Main.java +++ b/tank/src/com/mashibing/tank/Main.java @@ -5,12 +5,7 @@ public class Main { public static void main(String[] args) throws InterruptedException { TankFrame tf = new TankFrame(); - int initTankCount = Integer.parseInt((String)PropertyMgr.get("initTankCount")); - //初始化敌方坦克 - for(int i=0; inew Audio("audio/war1.wav").loop()).start(); diff --git a/tank/src/com/mashibing/tank/Tank.java b/tank/src/com/mashibing/tank/Tank.java index d23177f..2a2670f 100644 --- a/tank/src/com/mashibing/tank/Tank.java +++ b/tank/src/com/mashibing/tank/Tank.java @@ -22,19 +22,20 @@ public class Tank { Dir dir = Dir.DOWN; private boolean moving = true; - TankFrame tf = null; + private boolean living = true; Group group = Group.BAD; FireStrategy fs; + GameModel gm; - public Tank(int x, int y, Dir dir, Group group, TankFrame tf) { + public Tank(int x, int y, Dir dir, Group group, GameModel gm) { super(); this.x = x; this.y = y; this.dir = dir; this.group = group; - this.tf = tf; + this.gm = gm; rect.x = this.x; rect.y = this.y; @@ -127,7 +128,7 @@ private void randomDir() { } public void paint(Graphics g) { - if(!living) tf.tanks.remove(this); + if(!living) gm.tanks.remove(this); diff --git a/tank/src/com/mashibing/tank/TankFrame.java b/tank/src/com/mashibing/tank/TankFrame.java index 94a488e..8c677c0 100644 --- a/tank/src/com/mashibing/tank/TankFrame.java +++ b/tank/src/com/mashibing/tank/TankFrame.java @@ -13,11 +13,9 @@ import java.util.List; public class TankFrame extends Frame { + + GameModel gm = new GameModel(); - Tank myTank = new Tank(200, 400, Dir.DOWN, Group.GOOD, this); - List bullets = new ArrayList<>(); - List tanks = new ArrayList<>(); - List explodes = new ArrayList<>(); static final int GAME_WIDTH = 1080, GAME_HEIGHT = 960; @@ -58,42 +56,9 @@ public void update(Graphics g) { @Override public void paint(Graphics g) { - Color c = g.getColor(); - g.setColor(Color.WHITE); - g.drawString("子弹的数量:" + bullets.size(), 10, 60); - g.drawString("敌人的数量:" + tanks.size(), 10, 80); - g.drawString("爆炸的数量:" + explodes.size(), 10, 100); - g.setColor(c); - - myTank.paint(g); - for (int i = 0; i < bullets.size(); i++) { - bullets.get(i).paint(g); - } - - for (int i = 0; i < tanks.size(); i++) { - tanks.get(i).paint(g); - } - - for (int i = 0; i < explodes.size(); i++) { - explodes.get(i).paint(g); - } - //collision detect - - for(int i=0; i it = bullets.iterator(); it.hasNext();) { - // Bullet b = it.next(); - // if(!b.live) it.remove(); - // } - - // for(Bullet b : bullets) { - // b.paint(g); - // } } @@ -148,7 +113,7 @@ public void keyReleased(KeyEvent e) { break; case KeyEvent.VK_CONTROL: - myTank.fire(); + gm.getMainTank().fire(); break; default: @@ -159,6 +124,7 @@ public void keyReleased(KeyEvent e) { } private void setMainTankDir() { + Tank myTank = gm.getMainTank(); if (!bL && !bU && !bR && !bD) myTank.setMoving(false); From 8c098b5da81ceff5cdcaba3ea5476989c3843ae6 Mon Sep 17 00:00:00 2001 From: bjmashibing Date: Tue, 14 May 2019 22:07:54 +0800 Subject: [PATCH 4/8] =?UTF-8?q?=E8=B4=A3=E4=BB=BB=E9=93=BE=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tank/src/com/mashibing/tank/Bullet.java | 15 +- tank/src/com/mashibing/tank/Explode.java | 4 +- tank/src/com/mashibing/tank/FireStrategy.java | 5 - tank/src/com/mashibing/tank/GameModel.java | 63 ++++---- tank/src/com/mashibing/tank/GameObject.java | 9 ++ tank/src/com/mashibing/tank/Tank.java | 146 ++++++++++-------- .../tank/cor/BulletTankCollider.java | 27 ++++ tank/src/com/mashibing/tank/cor/Collider.java | 7 + .../com/mashibing/tank/cor/ColliderChain.java | 33 ++++ .../mashibing/tank/cor/TankTankCollider.java | 23 +++ .../{ => strategy}/DefaultFireStrategy.java | 7 +- .../mashibing/tank/strategy/FireStrategy.java | 7 + .../{ => strategy}/FourDirFireStrategy.java | 8 +- tank/src/config | 6 +- 14 files changed, 250 insertions(+), 110 deletions(-) delete mode 100644 tank/src/com/mashibing/tank/FireStrategy.java create mode 100644 tank/src/com/mashibing/tank/GameObject.java create mode 100644 tank/src/com/mashibing/tank/cor/BulletTankCollider.java create mode 100644 tank/src/com/mashibing/tank/cor/Collider.java create mode 100644 tank/src/com/mashibing/tank/cor/ColliderChain.java create mode 100644 tank/src/com/mashibing/tank/cor/TankTankCollider.java rename tank/src/com/mashibing/tank/{ => strategy}/DefaultFireStrategy.java (64%) create mode 100644 tank/src/com/mashibing/tank/strategy/FireStrategy.java rename tank/src/com/mashibing/tank/{ => strategy}/FourDirFireStrategy.java (64%) diff --git a/tank/src/com/mashibing/tank/Bullet.java b/tank/src/com/mashibing/tank/Bullet.java index ed26237..919fb36 100644 --- a/tank/src/com/mashibing/tank/Bullet.java +++ b/tank/src/com/mashibing/tank/Bullet.java @@ -3,7 +3,7 @@ import java.awt.Graphics; import java.awt.Rectangle; -public class Bullet { +public class Bullet extends GameObject { private static final int SPEED = 6; public static int WIDTH = ResourceMgr.bulletD.getWidth(); public static int HEIGHT = ResourceMgr.bulletD.getHeight(); @@ -29,7 +29,7 @@ public Bullet(int x, int y, Dir dir, Group group, GameModel gm) { rect.width = WIDTH; rect.height = HEIGHT; - gm.bullets.add(this); + gm.add(this); } @@ -43,7 +43,7 @@ public void setGroup(Group group) { public void paint(Graphics g) { if(!living) { - gm.bullets.remove(this); + gm.remove(this); } switch(dir) { @@ -89,17 +89,20 @@ private void move() { } - public void collideWith(Tank tank) { - if(this.group == tank.getGroup()) return; + public boolean collideWith(Tank tank) { + if(this.group == tank.getGroup()) return false; if(rect.intersects(tank.rect)) { tank.die(); this.die(); int eX = tank.getX() + Tank.WIDTH/2 - Explode.WIDTH/2; int eY = tank.getY() + Tank.HEIGHT/2 - Explode.HEIGHT/2; - gm.explodes.add(new Explode(eX, eY, gm)); + gm.add(new Explode(eX, eY, gm)); + return true; } + return false; + } private void die() { diff --git a/tank/src/com/mashibing/tank/Explode.java b/tank/src/com/mashibing/tank/Explode.java index caf3d06..29b404a 100644 --- a/tank/src/com/mashibing/tank/Explode.java +++ b/tank/src/com/mashibing/tank/Explode.java @@ -3,7 +3,7 @@ import java.awt.Graphics; import java.awt.Rectangle; -public class Explode { +public class Explode extends GameObject { public static int WIDTH = ResourceMgr.explodes[0].getWidth(); public static int HEIGHT = ResourceMgr.explodes[0].getHeight(); @@ -29,7 +29,7 @@ public void paint(Graphics g) { g.drawImage(ResourceMgr.explodes[step++], x, y, null); if(step >= ResourceMgr.explodes.length) - gm.explodes.remove(this); + gm.remove(this); } diff --git a/tank/src/com/mashibing/tank/FireStrategy.java b/tank/src/com/mashibing/tank/FireStrategy.java deleted file mode 100644 index 455cdbd..0000000 --- a/tank/src/com/mashibing/tank/FireStrategy.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.mashibing.tank; - -public interface FireStrategy { - void fire(Tank t); -} diff --git a/tank/src/com/mashibing/tank/GameModel.java b/tank/src/com/mashibing/tank/GameModel.java index f4118ce..446e826 100644 --- a/tank/src/com/mashibing/tank/GameModel.java +++ b/tank/src/com/mashibing/tank/GameModel.java @@ -5,58 +5,65 @@ import java.util.ArrayList; import java.util.List; +import com.mashibing.tank.cor.ColliderChain; + public class GameModel { Tank myTank = new Tank(200, 400, Dir.DOWN, Group.GOOD, this); - List bullets = new ArrayList<>(); - List tanks = new ArrayList<>(); - List explodes = new ArrayList<>(); +// List bullets = new ArrayList<>(); +// List tanks = new ArrayList<>(); +// List explodes = new ArrayList<>(); + ColliderChain chain = new ColliderChain(); + + private List objects = new ArrayList<>(); public GameModel() { int initTankCount = Integer.parseInt((String) PropertyMgr.get("initTankCount")); // 初始化敌方坦克 for (int i = 0; i < initTankCount; i++) { - tanks.add(new Tank(50 + i * 80, 200, Dir.DOWN, Group.BAD, this)); + add(new Tank(50 + i * 80, 200, Dir.DOWN, Group.BAD, this)); } } + + public void add(GameObject go) { + this.objects.add(go); + } + + public void remove(GameObject go) { + this.objects.remove(go); + } public void paint(Graphics g) { Color c = g.getColor(); g.setColor(Color.WHITE); - g.drawString("子弹的数量:" + bullets.size(), 10, 60); - g.drawString("敌人的数量:" + tanks.size(), 10, 80); - g.drawString("爆炸的数量:" + explodes.size(), 10, 100); +// g.drawString("子弹的数量:" + bullets.size(), 10, 60); +// g.drawString("敌人的数量:" + tanks.size(), 10, 80); +// g.drawString("爆炸的数量:" + explodes.size(), 10, 100); g.setColor(c); myTank.paint(g); - for (int i = 0; i < bullets.size(); i++) { - bullets.get(i).paint(g); - } - - for (int i = 0; i < tanks.size(); i++) { - tanks.get(i).paint(g); - } - - for (int i = 0; i < explodes.size(); i++) { - explodes.get(i).paint(g); + for (int i = 0; i < objects.size(); i++) { + objects.get(i).paint(g); } - // collision detect - for (int i = 0; i < bullets.size(); i++) { - for (int j = 0; j < tanks.size(); j++) - bullets.get(i).collideWith(tanks.get(j)); + //互相碰撞 + for(int i=0; i it = bullets.iterator(); it.hasNext();) { - // Bullet b = it.next(); - // if(!b.live) it.remove(); - // } +// for (int i = 0; i < bullets.size(); i++) { +// for (int j = 0; j < tanks.size(); j++) +// bullets.get(i).collideWith(tanks.get(j)); +// } - // for(Bullet b : bullets) { - // b.paint(g); - // } + } public Tank getMainTank() { diff --git a/tank/src/com/mashibing/tank/GameObject.java b/tank/src/com/mashibing/tank/GameObject.java new file mode 100644 index 0000000..8021b99 --- /dev/null +++ b/tank/src/com/mashibing/tank/GameObject.java @@ -0,0 +1,9 @@ +package com.mashibing.tank; + +import java.awt.Graphics; + +public abstract class GameObject { + int x, y; + + public abstract void paint(Graphics g); +} diff --git a/tank/src/com/mashibing/tank/Tank.java b/tank/src/com/mashibing/tank/Tank.java index 2a2670f..9340b29 100644 --- a/tank/src/com/mashibing/tank/Tank.java +++ b/tank/src/com/mashibing/tank/Tank.java @@ -5,30 +5,33 @@ import java.lang.reflect.InvocationTargetException; import java.util.Random; -public class Tank { - - +import com.mashibing.tank.strategy.DefaultFireStrategy; +import com.mashibing.tank.strategy.FireStrategy; + +public class Tank extends GameObject { + private static final int SPEED = 2; public static int WIDTH = ResourceMgr.goodTankU.getWidth(); public static int HEIGHT = ResourceMgr.goodTankU.getHeight(); - + Rectangle rect = new Rectangle(); - + private Random random = new Random(); - int x, y; + public int x, y; + //int oldX, oldY - Dir dir = Dir.DOWN; + public Dir dir = Dir.DOWN; private boolean moving = true; - + private boolean living = true; - Group group = Group.BAD; - + + public Group group = Group.BAD; FireStrategy fs; - GameModel gm; - + + public GameModel gm; public Tank(int x, int y, Dir dir, Group group, GameModel gm) { super(); this.x = x; @@ -36,45 +39,61 @@ public Tank(int x, int y, Dir dir, Group group, GameModel gm) { this.dir = dir; this.group = group; this.gm = gm; - + rect.x = this.x; rect.y = this.y; rect.width = WIDTH; rect.height = HEIGHT; - - if(group == Group.GOOD) { - String goodFSName = (String)PropertyMgr.get("goodFS"); - + + if (group == Group.GOOD) { + String goodFSName = (String) PropertyMgr.get("goodFS"); + try { - fs = (FireStrategy)Class.forName(goodFSName).getDeclaredConstructor().newInstance(); + fs = (FireStrategy) Class.forName(goodFSName).getDeclaredConstructor().newInstance(); } catch (Exception e) { e.printStackTrace(); } - + } else { fs = new DefaultFireStrategy(); } } - + + private void boundsCheck() { + if (this.x < 2) + x = 2; + if (this.y < 28) + y = 28; + if (this.x > TankFrame.GAME_WIDTH - Tank.WIDTH - 2) + x = TankFrame.GAME_WIDTH - Tank.WIDTH - 2; + if (this.y > TankFrame.GAME_HEIGHT - Tank.HEIGHT - 2) + y = TankFrame.GAME_HEIGHT - Tank.HEIGHT - 2; + } + + public void die() { + this.living = false; + } + public void fire() { fs.fire(this); } - + public Dir getDir() { return dir; } - - public int getX() { - return x; - } - - + public Group getGroup() { return group; } - public void setGroup(Group group) { - this.group = group; + + public Rectangle getRect() { + return rect; + } + + public int getX() { + return x; } + public int getY() { return y; } @@ -84,9 +103,12 @@ public boolean isMoving() { } private void move() { + //oldX = x + //oldY = y; - if(!moving) return ; - + if (!moving) + return; + switch (dir) { case LEFT: x -= SPEED; @@ -101,61 +123,56 @@ private void move() { y += SPEED; break; } - - if(this.group == Group.BAD && random.nextInt(100) > 95) + + if (this.group == Group.BAD && random.nextInt(100) > 95) this.fire(); - - if(this.group == Group.BAD && random.nextInt(100) > 95) + + if (this.group == Group.BAD && random.nextInt(100) > 95) randomDir(); - + boundsCheck(); - //update rect + // update rect rect.x = this.x; rect.y = this.y; - - } - private void boundsCheck() { - if (this.x < 2) x = 2; - if (this.y < 28) y = 28; - if (this.x > TankFrame.GAME_WIDTH- Tank.WIDTH -2) x = TankFrame.GAME_WIDTH - Tank.WIDTH -2; - if (this.y > TankFrame.GAME_HEIGHT - Tank.HEIGHT -2 ) y = TankFrame.GAME_HEIGHT -Tank.HEIGHT -2; } - - private void randomDir() { - - this.dir = Dir.values()[random.nextInt(4)]; - } - + public void paint(Graphics g) { - if(!living) gm.tanks.remove(this); - - - - switch(dir) { + if (!living) + gm.remove(this); + + switch (dir) { case LEFT: - g.drawImage(this.group == Group.GOOD? ResourceMgr.goodTankL : ResourceMgr.badTankL, x, y, null); + g.drawImage(this.group == Group.GOOD ? ResourceMgr.goodTankL : ResourceMgr.badTankL, x, y, null); break; case UP: - g.drawImage(this.group == Group.GOOD? ResourceMgr.goodTankU : ResourceMgr.badTankU, x, y, null); + g.drawImage(this.group == Group.GOOD ? ResourceMgr.goodTankU : ResourceMgr.badTankU, x, y, null); break; case RIGHT: - g.drawImage(this.group == Group.GOOD? ResourceMgr.goodTankR : ResourceMgr.badTankR, x, y, null); + g.drawImage(this.group == Group.GOOD ? ResourceMgr.goodTankR : ResourceMgr.badTankR, x, y, null); break; case DOWN: - g.drawImage(this.group == Group.GOOD? ResourceMgr.goodTankD : ResourceMgr.badTankD, x, y, null); + g.drawImage(this.group == Group.GOOD ? ResourceMgr.goodTankD : ResourceMgr.badTankD, x, y, null); break; } - + move(); - + } + private void randomDir() { + + this.dir = Dir.values()[random.nextInt(4)]; + } public void setDir(Dir dir) { this.dir = dir; } + public void setGroup(Group group) { + this.group = group; + } + public void setMoving(boolean moving) { this.moving = moving; } @@ -167,10 +184,9 @@ public void setX(int x) { public void setY(int y) { this.y = y; } - public void die() { - this.living = false; - } - + public void stop() { + moving = false; + } } diff --git a/tank/src/com/mashibing/tank/cor/BulletTankCollider.java b/tank/src/com/mashibing/tank/cor/BulletTankCollider.java new file mode 100644 index 0000000..47542fd --- /dev/null +++ b/tank/src/com/mashibing/tank/cor/BulletTankCollider.java @@ -0,0 +1,27 @@ +package com.mashibing.tank.cor; + +import com.mashibing.tank.Bullet; +import com.mashibing.tank.GameObject; +import com.mashibing.tank.Tank; + +public class BulletTankCollider implements Collider { + + @Override + public boolean collide(GameObject o1, GameObject o2) { + if(o1 instanceof Bullet && o2 instanceof Tank) { + Bullet b = (Bullet)o1; + Tank t = (Tank)o2; + //TODO copy code from method collideWith + if(b.collideWith(t)) { + return false; + } + + } else if (o1 instanceof Tank && o2 instanceof Bullet) { + return collide(o2, o1); + } + + return true; + + } + +} diff --git a/tank/src/com/mashibing/tank/cor/Collider.java b/tank/src/com/mashibing/tank/cor/Collider.java new file mode 100644 index 0000000..e82a1c7 --- /dev/null +++ b/tank/src/com/mashibing/tank/cor/Collider.java @@ -0,0 +1,7 @@ +package com.mashibing.tank.cor; + +import com.mashibing.tank.GameObject; + +public interface Collider { + boolean collide(GameObject o1, GameObject o2); +} diff --git a/tank/src/com/mashibing/tank/cor/ColliderChain.java b/tank/src/com/mashibing/tank/cor/ColliderChain.java new file mode 100644 index 0000000..b68d822 --- /dev/null +++ b/tank/src/com/mashibing/tank/cor/ColliderChain.java @@ -0,0 +1,33 @@ +package com.mashibing.tank.cor; + +import java.util.LinkedList; +import java.util.List; + +import com.mashibing.tank.GameObject; + +public class ColliderChain implements Collider { + private List colliders = new LinkedList<>(); + + public ColliderChain() { + add(new BulletTankCollider()); + add(new TankTankCollider()); + } + + + public void add(Collider c) { + colliders.add(c); + } + + + public boolean collide(GameObject o1, GameObject o2) { + for(int i=0; i Date: Thu, 16 May 2019 21:16:34 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E9=87=8D=E6=9E=84?= =?UTF-8?q?=EF=BC=8C=E8=A7=A3=E8=80=A6=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tank/src/com/mashibing/tank/Bullet.java | 32 +++------- tank/src/com/mashibing/tank/Explode.java | 7 +-- tank/src/com/mashibing/tank/GameModel.java | 62 ++++++++++++------- tank/src/com/mashibing/tank/Tank.java | 26 +++++--- tank/src/com/mashibing/tank/TankFrame.java | 4 +- tank/src/com/mashibing/tank/Wall.java | 30 +++++++++ .../tank/cor/BulletTankCollider.java | 10 ++- .../tank/cor/BulletWallCollider.java | 28 +++++++++ .../com/mashibing/tank/cor/ColliderChain.java | 2 + .../mashibing/tank/cor/TankTankCollider.java | 3 +- .../mashibing/tank/cor/TankWallCollider.java | 28 +++++++++ .../tank/strategy/DefaultFireStrategy.java | 2 +- .../tank/strategy/FourDirFireStrategy.java | 2 +- 13 files changed, 172 insertions(+), 64 deletions(-) create mode 100644 tank/src/com/mashibing/tank/Wall.java create mode 100644 tank/src/com/mashibing/tank/cor/BulletWallCollider.java create mode 100644 tank/src/com/mashibing/tank/cor/TankWallCollider.java diff --git a/tank/src/com/mashibing/tank/Bullet.java b/tank/src/com/mashibing/tank/Bullet.java index 919fb36..cc2cbbd 100644 --- a/tank/src/com/mashibing/tank/Bullet.java +++ b/tank/src/com/mashibing/tank/Bullet.java @@ -8,28 +8,28 @@ public class Bullet extends GameObject { public static int WIDTH = ResourceMgr.bulletD.getWidth(); public static int HEIGHT = ResourceMgr.bulletD.getHeight(); - Rectangle rect = new Rectangle(); + public Rectangle rect = new Rectangle(); private int x, y; private Dir dir; private boolean living = true; - GameModel gm = null; - private Group group = Group.BAD; + + public Group group = Group.BAD; - public Bullet(int x, int y, Dir dir, Group group, GameModel gm) { + public Bullet(int x, int y, Dir dir, Group group) { this.x = x; this.y = y; this.dir = dir; this.group = group; - this.gm = gm; + rect.x = this.x; rect.y = this.y; rect.width = WIDTH; rect.height = HEIGHT; - gm.add(this); + GameModel.getInstance().add(this); } @@ -43,7 +43,7 @@ public void setGroup(Group group) { public void paint(Graphics g) { if(!living) { - gm.remove(this); + GameModel.getInstance().remove(this); } switch(dir) { @@ -89,23 +89,9 @@ private void move() { } - public boolean collideWith(Tank tank) { - if(this.group == tank.getGroup()) return false; - - if(rect.intersects(tank.rect)) { - tank.die(); - this.die(); - int eX = tank.getX() + Tank.WIDTH/2 - Explode.WIDTH/2; - int eY = tank.getY() + Tank.HEIGHT/2 - Explode.HEIGHT/2; - gm.add(new Explode(eX, eY, gm)); - return true; - } - - return false; - - } + - private void die() { + public void die() { this.living = false; } } diff --git a/tank/src/com/mashibing/tank/Explode.java b/tank/src/com/mashibing/tank/Explode.java index 29b404a..29b8f42 100644 --- a/tank/src/com/mashibing/tank/Explode.java +++ b/tank/src/com/mashibing/tank/Explode.java @@ -10,16 +10,15 @@ public class Explode extends GameObject { private int x, y; //private boolean living = true; - GameModel gm = null; private int step = 0; - public Explode(int x, int y, GameModel gm) { + public Explode(int x, int y) { this.x = x; this.y = y; - this.gm = gm; new Thread(()->new Audio("audio/explode.wav").play()).start(); + GameModel.getInstance().add(this); } @@ -29,7 +28,7 @@ public void paint(Graphics g) { g.drawImage(ResourceMgr.explodes[step++], x, y, null); if(step >= ResourceMgr.explodes.length) - gm.remove(this); + GameModel.getInstance().remove(this); } diff --git a/tank/src/com/mashibing/tank/GameModel.java b/tank/src/com/mashibing/tank/GameModel.java index 446e826..a3a2d85 100644 --- a/tank/src/com/mashibing/tank/GameModel.java +++ b/tank/src/com/mashibing/tank/GameModel.java @@ -9,28 +9,49 @@ public class GameModel { - Tank myTank = new Tank(200, 400, Dir.DOWN, Group.GOOD, this); + private static final GameModel INSTANCE = new GameModel(); + + static { + INSTANCE.init(); + } -// List bullets = new ArrayList<>(); -// List tanks = new ArrayList<>(); -// List explodes = new ArrayList<>(); + Tank myTank; + + // List bullets = new ArrayList<>(); + // List tanks = new ArrayList<>(); + // List explodes = new ArrayList<>(); ColliderChain chain = new ColliderChain(); - + private List objects = new ArrayList<>(); - public GameModel() { + public static GameModel getInstance() { + return INSTANCE; + } + + private GameModel() {} + + private void init() { + // 初始化主战坦克 + myTank = new Tank(200, 400, Dir.DOWN, Group.GOOD); + int initTankCount = Integer.parseInt((String) PropertyMgr.get("initTankCount")); // 初始化敌方坦克 for (int i = 0; i < initTankCount; i++) { - add(new Tank(50 + i * 80, 200, Dir.DOWN, Group.BAD, this)); + new Tank(50 + i * 80, 200, Dir.DOWN, Group.BAD); } + + // 初始化墙 + add(new Wall(150, 150, 200, 50)); + add(new Wall(550, 150, 200, 50)); + add(new Wall(300, 300, 50, 200)); + add(new Wall(550, 300, 50, 200)); } - + public void add(GameObject go) { this.objects.add(go); } - + public void remove(GameObject go) { this.objects.remove(go); } @@ -38,9 +59,9 @@ public void remove(GameObject go) { public void paint(Graphics g) { Color c = g.getColor(); g.setColor(Color.WHITE); -// g.drawString("子弹的数量:" + bullets.size(), 10, 60); -// g.drawString("敌人的数量:" + tanks.size(), 10, 80); -// g.drawString("爆炸的数量:" + explodes.size(), 10, 100); + // g.drawString("子弹的数量:" + bullets.size(), 10, 60); + // g.drawString("敌人的数量:" + tanks.size(), 10, 80); + // g.drawString("爆炸的数量:" + explodes.size(), 10, 100); g.setColor(c); myTank.paint(g); @@ -48,22 +69,21 @@ public void paint(Graphics g) { objects.get(i).paint(g); } - //互相碰撞 - for(int i=0; inew Audio("audio/tank_fire.wav").play()).start(); } diff --git a/tank/src/com/mashibing/tank/strategy/FourDirFireStrategy.java b/tank/src/com/mashibing/tank/strategy/FourDirFireStrategy.java index dbc21c1..cd0b9ef 100644 --- a/tank/src/com/mashibing/tank/strategy/FourDirFireStrategy.java +++ b/tank/src/com/mashibing/tank/strategy/FourDirFireStrategy.java @@ -15,7 +15,7 @@ public void fire(Tank t) { Dir[] dirs = Dir.values(); for(Dir dir : dirs) { - new Bullet(bX, bY, dir, t.group, t.gm); + new Bullet(bX, bY, dir, t.group); } if(t.group == Group.GOOD) new Thread(()->new Audio("audio/tank_fire.wav").play()).start(); From d93891778257bdde7dca7d9bd49c6cd4df4875f3 Mon Sep 17 00:00:00 2001 From: bjmashibing Date: Thu, 16 May 2019 22:09:56 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E8=A3=85=E9=A5=B0=E5=99=A8=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tank/src/com/mashibing/tank/Bullet.java | 12 +++++- tank/src/com/mashibing/tank/Explode.java | 11 +++++- tank/src/com/mashibing/tank/GameObject.java | 4 +- tank/src/com/mashibing/tank/Main.java | 2 +- tank/src/com/mashibing/tank/Tank.java | 12 +++++- tank/src/com/mashibing/tank/Wall.java | 10 +++++ .../mashibing/tank/decorator/GODecorator.java | 19 ++++++++++ .../tank/decorator/RectDecorator.java | 38 +++++++++++++++++++ .../tank/decorator/TailDecorator.java | 38 +++++++++++++++++++ .../tank/strategy/DefaultFireStrategy.java | 9 ++++- 10 files changed, 149 insertions(+), 6 deletions(-) create mode 100644 tank/src/com/mashibing/tank/decorator/GODecorator.java create mode 100644 tank/src/com/mashibing/tank/decorator/RectDecorator.java create mode 100644 tank/src/com/mashibing/tank/decorator/TailDecorator.java diff --git a/tank/src/com/mashibing/tank/Bullet.java b/tank/src/com/mashibing/tank/Bullet.java index cc2cbbd..f0770f9 100644 --- a/tank/src/com/mashibing/tank/Bullet.java +++ b/tank/src/com/mashibing/tank/Bullet.java @@ -10,7 +10,7 @@ public class Bullet extends GameObject { public Rectangle rect = new Rectangle(); - private int x, y; + private Dir dir; private boolean living = true; @@ -94,4 +94,14 @@ private void move() { public void die() { this.living = false; } + + @Override + public int getWidth() { + return WIDTH; + } + + @Override + public int getHeight() { + return HEIGHT; + } } diff --git a/tank/src/com/mashibing/tank/Explode.java b/tank/src/com/mashibing/tank/Explode.java index 29b8f42..942a234 100644 --- a/tank/src/com/mashibing/tank/Explode.java +++ b/tank/src/com/mashibing/tank/Explode.java @@ -7,7 +7,7 @@ public class Explode extends GameObject { public static int WIDTH = ResourceMgr.explodes[0].getWidth(); public static int HEIGHT = ResourceMgr.explodes[0].getHeight(); - private int x, y; + //private boolean living = true; @@ -33,6 +33,15 @@ public void paint(Graphics g) { } + @Override + public int getWidth() { + return WIDTH; + } + + @Override + public int getHeight() { + return HEIGHT; + } } diff --git a/tank/src/com/mashibing/tank/GameObject.java b/tank/src/com/mashibing/tank/GameObject.java index 8021b99..9a5dd39 100644 --- a/tank/src/com/mashibing/tank/GameObject.java +++ b/tank/src/com/mashibing/tank/GameObject.java @@ -3,7 +3,9 @@ import java.awt.Graphics; public abstract class GameObject { - int x, y; + public int x, y; public abstract void paint(Graphics g); + public abstract int getWidth(); + public abstract int getHeight(); } diff --git a/tank/src/com/mashibing/tank/Main.java b/tank/src/com/mashibing/tank/Main.java index 42b8388..85fa001 100644 --- a/tank/src/com/mashibing/tank/Main.java +++ b/tank/src/com/mashibing/tank/Main.java @@ -10,7 +10,7 @@ public static void main(String[] args) throws InterruptedException { new Thread(()->new Audio("audio/war1.wav").loop()).start(); while(true) { - Thread.sleep(25); + Thread.sleep(1000); tf.repaint(); } diff --git a/tank/src/com/mashibing/tank/Tank.java b/tank/src/com/mashibing/tank/Tank.java index 820d2e9..c40bd2f 100644 --- a/tank/src/com/mashibing/tank/Tank.java +++ b/tank/src/com/mashibing/tank/Tank.java @@ -19,7 +19,7 @@ public class Tank extends GameObject { private Random random = new Random(); - public int x, y; + int oldX, oldY; public Dir dir = Dir.DOWN; @@ -196,5 +196,15 @@ public void setY(int y) { public void stop() { moving = false; } + + @Override + public int getWidth() { + return WIDTH; + } + + @Override + public int getHeight() { + return HEIGHT; + } } diff --git a/tank/src/com/mashibing/tank/Wall.java b/tank/src/com/mashibing/tank/Wall.java index bfa1457..9c1e0fe 100644 --- a/tank/src/com/mashibing/tank/Wall.java +++ b/tank/src/com/mashibing/tank/Wall.java @@ -27,4 +27,14 @@ public void paint(Graphics g) { g.setColor(c); } + @Override + public int getWidth() { + return w; + } + + @Override + public int getHeight() { + return h; + } + } diff --git a/tank/src/com/mashibing/tank/decorator/GODecorator.java b/tank/src/com/mashibing/tank/decorator/GODecorator.java new file mode 100644 index 0000000..30d3983 --- /dev/null +++ b/tank/src/com/mashibing/tank/decorator/GODecorator.java @@ -0,0 +1,19 @@ +package com.mashibing.tank.decorator; + +import java.awt.Graphics; + +import com.mashibing.tank.GameObject; + +public abstract class GODecorator extends GameObject { + + GameObject go; + + public GODecorator(GameObject go) { + + this.go = go; + } + + @Override + public abstract void paint(Graphics g); + +} diff --git a/tank/src/com/mashibing/tank/decorator/RectDecorator.java b/tank/src/com/mashibing/tank/decorator/RectDecorator.java new file mode 100644 index 0000000..1f6a5b7 --- /dev/null +++ b/tank/src/com/mashibing/tank/decorator/RectDecorator.java @@ -0,0 +1,38 @@ +package com.mashibing.tank.decorator; + +import java.awt.Color; +import java.awt.Graphics; + +import com.mashibing.tank.GameObject; + +public class RectDecorator extends GODecorator { + + public RectDecorator(GameObject go) { + super(go); + } + + @Override + public void paint(Graphics g) { + this.x = go.x; + this.y = go.y; + + go.paint(g); + + Color c = g.getColor(); + g.setColor(Color.WHITE); + g.drawRect(super.go.x, super.go.y, super.go.getWidth()+2, super.go.getHeight()+2); + g.setColor(c); + } + + + @Override + public int getWidth() { + return super.go.getWidth(); + } + + @Override + public int getHeight() { + return super.go.getHeight(); + } + +} diff --git a/tank/src/com/mashibing/tank/decorator/TailDecorator.java b/tank/src/com/mashibing/tank/decorator/TailDecorator.java new file mode 100644 index 0000000..5ff0623 --- /dev/null +++ b/tank/src/com/mashibing/tank/decorator/TailDecorator.java @@ -0,0 +1,38 @@ +package com.mashibing.tank.decorator; + +import java.awt.Color; +import java.awt.Graphics; + +import com.mashibing.tank.GameObject; + +public class TailDecorator extends GODecorator { + + public TailDecorator(GameObject go) { + + super(go); + } + + @Override + public void paint(Graphics g) { + this.x = go.x; + this.y = go.y; + go.paint(g); + + Color c = g.getColor(); + g.setColor(Color.WHITE); + g.drawLine(go.x, go.y, go.x + getWidth(), go.y + getHeight()); + g.setColor(c); + } + + + @Override + public int getWidth() { + return super.go.getWidth(); + } + + @Override + public int getHeight() { + return super.go.getHeight(); + } + +} diff --git a/tank/src/com/mashibing/tank/strategy/DefaultFireStrategy.java b/tank/src/com/mashibing/tank/strategy/DefaultFireStrategy.java index 04d2300..acd2ef7 100644 --- a/tank/src/com/mashibing/tank/strategy/DefaultFireStrategy.java +++ b/tank/src/com/mashibing/tank/strategy/DefaultFireStrategy.java @@ -2,8 +2,11 @@ import com.mashibing.tank.Audio; import com.mashibing.tank.Bullet; +import com.mashibing.tank.GameModel; import com.mashibing.tank.Group; import com.mashibing.tank.Tank; +import com.mashibing.tank.decorator.RectDecorator; +import com.mashibing.tank.decorator.TailDecorator; public class DefaultFireStrategy implements FireStrategy { @@ -12,7 +15,11 @@ public void fire(Tank t) { int bX = t.x + Tank.WIDTH/2 - Bullet.WIDTH/2; int bY = t.y + Tank.HEIGHT/2 - Bullet.HEIGHT/2; - new Bullet(bX, bY, t.dir, t.group); + //Bug? new Bullet把自己加了一遍 + GameModel.getInstance().add( + new RectDecorator( + new TailDecorator( + new Bullet(bX, bY, t.dir, t.group)))); if(t.group == Group.GOOD) new Thread(()->new Audio("audio/tank_fire.wav").play()).start(); } From 2769783c9ac5fa09cce4471bd5ee69ea83bd867b Mon Sep 17 00:00:00 2001 From: bjmashibing Date: Sun, 19 May 2019 21:31:04 +0800 Subject: [PATCH 7/8] =?UTF-8?q?Observer=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tank/src/com/mashibing/tank/Main.java | 2 +- tank/src/com/mashibing/tank/Tank.java | 17 +++++++++++++++-- tank/src/com/mashibing/tank/TankFrame.java | 6 +++++- .../mashibing/tank/observer/TankFireEvent.java | 16 ++++++++++++++++ .../tank/observer/TankFireHandler.java | 13 +++++++++++++ .../tank/observer/TankFireObserver.java | 5 +++++ .../tank/strategy/DefaultFireStrategy.java | 9 +++++---- 7 files changed, 60 insertions(+), 8 deletions(-) create mode 100644 tank/src/com/mashibing/tank/observer/TankFireEvent.java create mode 100644 tank/src/com/mashibing/tank/observer/TankFireHandler.java create mode 100644 tank/src/com/mashibing/tank/observer/TankFireObserver.java diff --git a/tank/src/com/mashibing/tank/Main.java b/tank/src/com/mashibing/tank/Main.java index 85fa001..42b8388 100644 --- a/tank/src/com/mashibing/tank/Main.java +++ b/tank/src/com/mashibing/tank/Main.java @@ -10,7 +10,7 @@ public static void main(String[] args) throws InterruptedException { new Thread(()->new Audio("audio/war1.wav").loop()).start(); while(true) { - Thread.sleep(1000); + Thread.sleep(25); tf.repaint(); } diff --git a/tank/src/com/mashibing/tank/Tank.java b/tank/src/com/mashibing/tank/Tank.java index c40bd2f..fbb7bdb 100644 --- a/tank/src/com/mashibing/tank/Tank.java +++ b/tank/src/com/mashibing/tank/Tank.java @@ -2,9 +2,13 @@ import java.awt.Graphics; import java.awt.Rectangle; -import java.lang.reflect.InvocationTargetException; +import java.util.Arrays; +import java.util.List; import java.util.Random; +import com.mashibing.tank.observer.TankFireEvent; +import com.mashibing.tank.observer.TankFireHandler; +import com.mashibing.tank.observer.TankFireObserver; import com.mashibing.tank.strategy.DefaultFireStrategy; import com.mashibing.tank.strategy.FireStrategy; @@ -18,7 +22,6 @@ public class Tank extends GameObject { public Rectangle rect = new Rectangle(); private Random random = new Random(); - int oldX, oldY; @@ -75,6 +78,8 @@ private void boundsCheck() { public void die() { this.living = false; } + + public void fire() { fs.fire(this); @@ -206,5 +211,13 @@ public int getWidth() { public int getHeight() { return HEIGHT; } + + private List fireObservers = Arrays.asList(new TankFireHandler()); + public void handleFireKey() { + TankFireEvent event = new TankFireEvent(this); + for(TankFireObserver o : fireObservers) { + o.actionOnFire(event); + } + } } diff --git a/tank/src/com/mashibing/tank/TankFrame.java b/tank/src/com/mashibing/tank/TankFrame.java index 45bbdb8..aec4443 100644 --- a/tank/src/com/mashibing/tank/TankFrame.java +++ b/tank/src/com/mashibing/tank/TankFrame.java @@ -30,6 +30,7 @@ public TankFrame() { @Override public void windowClosing(WindowEvent e) { // bjmashibing/tank + System.exit(0); } @@ -95,6 +96,7 @@ public void keyPressed(KeyEvent e) { @Override public void keyReleased(KeyEvent e) { + int key = e.getKeyCode(); switch (key) { case KeyEvent.VK_LEFT: @@ -111,7 +113,7 @@ public void keyReleased(KeyEvent e) { break; case KeyEvent.VK_CONTROL: - gm.getMainTank().fire(); + gm.getMainTank().handleFireKey(); break; default: @@ -141,3 +143,5 @@ private void setMainTankDir() { } } } + +//e.getSource().repaint() \ No newline at end of file diff --git a/tank/src/com/mashibing/tank/observer/TankFireEvent.java b/tank/src/com/mashibing/tank/observer/TankFireEvent.java new file mode 100644 index 0000000..e974a40 --- /dev/null +++ b/tank/src/com/mashibing/tank/observer/TankFireEvent.java @@ -0,0 +1,16 @@ +package com.mashibing.tank.observer; + +import com.mashibing.tank.Tank; + +public class TankFireEvent { + Tank tank; + + public Tank getSource() { + return tank; + } + + public TankFireEvent(Tank tank) { + this.tank = tank; + } + +} diff --git a/tank/src/com/mashibing/tank/observer/TankFireHandler.java b/tank/src/com/mashibing/tank/observer/TankFireHandler.java new file mode 100644 index 0000000..1fb80aa --- /dev/null +++ b/tank/src/com/mashibing/tank/observer/TankFireHandler.java @@ -0,0 +1,13 @@ +package com.mashibing.tank.observer; + +import com.mashibing.tank.Tank; + +public class TankFireHandler implements TankFireObserver { + + @Override + public void actionOnFire(TankFireEvent e) { + Tank t = e.getSource(); + t.fire(); + } + +} diff --git a/tank/src/com/mashibing/tank/observer/TankFireObserver.java b/tank/src/com/mashibing/tank/observer/TankFireObserver.java new file mode 100644 index 0000000..b5253fc --- /dev/null +++ b/tank/src/com/mashibing/tank/observer/TankFireObserver.java @@ -0,0 +1,5 @@ +package com.mashibing.tank.observer; + +public interface TankFireObserver { + void actionOnFire(TankFireEvent e); +} diff --git a/tank/src/com/mashibing/tank/strategy/DefaultFireStrategy.java b/tank/src/com/mashibing/tank/strategy/DefaultFireStrategy.java index acd2ef7..715db3d 100644 --- a/tank/src/com/mashibing/tank/strategy/DefaultFireStrategy.java +++ b/tank/src/com/mashibing/tank/strategy/DefaultFireStrategy.java @@ -16,10 +16,11 @@ public void fire(Tank t) { int bY = t.y + Tank.HEIGHT/2 - Bullet.HEIGHT/2; //Bug? new Bullet把自己加了一遍 - GameModel.getInstance().add( - new RectDecorator( - new TailDecorator( - new Bullet(bX, bY, t.dir, t.group)))); +// GameModel.getInstance().add( +// new RectDecorator( +// new TailDecorator( +// new Bullet(bX, bY, t.dir, t.group)))); + new Bullet(bX, bY, t.dir, t.group); if(t.group == Group.GOOD) new Thread(()->new Audio("audio/tank_fire.wav").play()).start(); } From f7fbb386a4a585732d3b1e67012ff065bd4df3ba Mon Sep 17 00:00:00 2001 From: bjmashibing Date: Tue, 28 May 2019 22:03:22 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E6=8C=89=E4=B8=8BS=E9=94=AE=E5=AD=98?= =?UTF-8?q?=E7=9B=98=EF=BC=8C=E6=8C=89=E4=B8=8BL=E9=94=AELoad?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tank/src/com/mashibing/tank/GameModel.java | 46 +++++++++++++++++++ tank/src/com/mashibing/tank/GameObject.java | 3 +- tank/src/com/mashibing/tank/TankFrame.java | 6 +++ .../tank/observer/TankFireObserver.java | 4 +- .../mashibing/tank/strategy/FireStrategy.java | 4 +- 5 files changed, 60 insertions(+), 3 deletions(-) diff --git a/tank/src/com/mashibing/tank/GameModel.java b/tank/src/com/mashibing/tank/GameModel.java index a3a2d85..76c24fd 100644 --- a/tank/src/com/mashibing/tank/GameModel.java +++ b/tank/src/com/mashibing/tank/GameModel.java @@ -2,6 +2,13 @@ import java.awt.Color; import java.awt.Graphics; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.List; @@ -89,5 +96,44 @@ public void paint(Graphics g) { public Tank getMainTank() { return myTank; } + + public void save() { + File f = new File("c:/mashibing/tank.data"); + ObjectOutputStream oos = null; + try { + oos = new ObjectOutputStream(new FileOutputStream(f)); + oos.writeObject(myTank); + oos.writeObject(objects); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + if(oos != null) { + try { + oos.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + public void load() { + File f = new File("c:/mashibing/tank.data"); + try { + ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f)); + myTank = (Tank)ois.readObject(); + objects = (List)ois.readObject(); + + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } } diff --git a/tank/src/com/mashibing/tank/GameObject.java b/tank/src/com/mashibing/tank/GameObject.java index 9a5dd39..5eb144c 100644 --- a/tank/src/com/mashibing/tank/GameObject.java +++ b/tank/src/com/mashibing/tank/GameObject.java @@ -1,8 +1,9 @@ package com.mashibing.tank; import java.awt.Graphics; +import java.io.Serializable; -public abstract class GameObject { +public abstract class GameObject implements Serializable { public int x, y; public abstract void paint(Graphics g); diff --git a/tank/src/com/mashibing/tank/TankFrame.java b/tank/src/com/mashibing/tank/TankFrame.java index aec4443..363974e 100644 --- a/tank/src/com/mashibing/tank/TankFrame.java +++ b/tank/src/com/mashibing/tank/TankFrame.java @@ -84,6 +84,12 @@ public void keyPressed(KeyEvent e) { case KeyEvent.VK_DOWN: bD = true; break; + case KeyEvent.VK_S: + gm.save(); + break; + case KeyEvent.VK_L: + gm.load(); + break; default: break; diff --git a/tank/src/com/mashibing/tank/observer/TankFireObserver.java b/tank/src/com/mashibing/tank/observer/TankFireObserver.java index b5253fc..1abe9af 100644 --- a/tank/src/com/mashibing/tank/observer/TankFireObserver.java +++ b/tank/src/com/mashibing/tank/observer/TankFireObserver.java @@ -1,5 +1,7 @@ package com.mashibing.tank.observer; -public interface TankFireObserver { +import java.io.Serializable; + +public interface TankFireObserver extends Serializable { void actionOnFire(TankFireEvent e); } diff --git a/tank/src/com/mashibing/tank/strategy/FireStrategy.java b/tank/src/com/mashibing/tank/strategy/FireStrategy.java index 01720f5..7676c4c 100644 --- a/tank/src/com/mashibing/tank/strategy/FireStrategy.java +++ b/tank/src/com/mashibing/tank/strategy/FireStrategy.java @@ -1,7 +1,9 @@ package com.mashibing.tank.strategy; +import java.io.Serializable; + import com.mashibing.tank.Tank; -public interface FireStrategy { +public interface FireStrategy extends Serializable { void fire(Tank t); }