From ef41cce8edc315c5abeb51247bfc4ceb5c2db0d1 Mon Sep 17 00:00:00 2001 From: bingor Date: Wed, 12 Oct 2022 15:20:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9D=A6=E5=85=8B=E5=A4=A7=E6=88=98=EF=BC=88?= =?UTF-8?q?=E4=B8=80=E6=9C=9F=EF=BC=89-=E8=AE=BE=E8=AE=A1=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F-=E6=8A=8AGameModel=E5=81=9A=E6=88=90=E5=8D=95?= =?UTF-8?q?=E4=BE=8B=EF=BC=88=E9=A5=BF=E6=B1=89=E5=BC=8F=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/msb/TankFrame.java | 17 ++++--- .../msb/inter/impl/DefaultFireStrategy.java | 3 +- .../com/msb/inter/impl/FourFireStrategy.java | 3 +- src/main/java/com/msb/model/Bullet.java | 8 ++-- src/main/java/com/msb/model/Explode.java | 6 +-- src/main/java/com/msb/model/GameModel.java | 26 ++++++++-- src/main/java/com/msb/model/Tank.java | 13 ++--- src/main/java/com/msb/model/Wall.java | 48 +++++++++++++++++++ src/test/java/CommonTest.java | 15 ++++++ 9 files changed, 106 insertions(+), 33 deletions(-) create mode 100644 src/main/java/com/msb/model/Wall.java diff --git a/src/main/java/com/msb/TankFrame.java b/src/main/java/com/msb/TankFrame.java index 34227b5..ec1be33 100644 --- a/src/main/java/com/msb/TankFrame.java +++ b/src/main/java/com/msb/TankFrame.java @@ -18,7 +18,6 @@ import java.util.Objects; */ public class TankFrame extends Frame { - GameModel gameModel = new GameModel(); public static final int GAME_WIDTH = 1400; public static final int GAME_HEIGHT = 1050; @@ -46,7 +45,7 @@ public class TankFrame extends Frame { @Override public void paint(Graphics g) { // super.paint(g); - gameModel.paint(g); + GameModel.getInstance().paint(g); } //双缓冲,解决闪烁现象 @@ -95,7 +94,7 @@ public class TankFrame extends Frame { case KeyEvent.VK_RIGHT : bR = false; break; case KeyEvent.VK_UP : bU = false; break; case KeyEvent.VK_DOWN : bD = false; break; - case KeyEvent.VK_SPACE: gameModel.getMainTank().fire(); break; + case KeyEvent.VK_SPACE: GameModel.getInstance().getMainTank().fire(); break; default: break; } setDir(); @@ -104,16 +103,16 @@ public class TankFrame extends Frame { private void setDir() { //当没有任何方向键按下的时候,那么让坦克停止 if( !bL && !bR && !bU && !bD ) { - gameModel.getMainTank().setMove(false); + GameModel.getInstance().getMainTank().setMove(false); } else { - gameModel.getMainTank().setMove(true); + GameModel.getInstance().getMainTank().setMove(true); } //为什么不用if-else 因为有可能是一起按两个方向键,那最后拿到哪个算哪个 - if(bL) gameModel.getMainTank().setDir(DirEnum.LEFT); - if(bR) gameModel.getMainTank().setDir(DirEnum.RIGHT); - if(bU) gameModel.getMainTank().setDir(DirEnum.UP); - if(bD) gameModel.getMainTank().setDir(DirEnum.DOWN); + if(bL) GameModel.getInstance().getMainTank().setDir(DirEnum.LEFT); + if(bR) GameModel.getInstance().getMainTank().setDir(DirEnum.RIGHT); + if(bU) GameModel.getInstance().getMainTank().setDir(DirEnum.UP); + if(bD) GameModel.getInstance().getMainTank().setDir(DirEnum.DOWN); } } diff --git a/src/main/java/com/msb/inter/impl/DefaultFireStrategy.java b/src/main/java/com/msb/inter/impl/DefaultFireStrategy.java index e0c4584..c48d25c 100644 --- a/src/main/java/com/msb/inter/impl/DefaultFireStrategy.java +++ b/src/main/java/com/msb/inter/impl/DefaultFireStrategy.java @@ -21,6 +21,7 @@ public class DefaultFireStrategy implements FireStrategy { public void fire(Tank tank) { int bX = tank.getX() + Tank.WIDTH/2 - Bullet.WIDTH/2; int bY = tank.getY()+Tank.HEIGHT/2-Bullet.HEIGHT/2; - new Bullet(bX, bY, tank.getDir(), tank.getGroup(), tank.getGameModel()); +// new Bullet(bX, bY, tank.getDir(), tank.getGroup(), tank.getGameModel()); + new Bullet(bX, bY, tank.getDir(), tank.getGroup()); } } diff --git a/src/main/java/com/msb/inter/impl/FourFireStrategy.java b/src/main/java/com/msb/inter/impl/FourFireStrategy.java index 336a105..5134847 100644 --- a/src/main/java/com/msb/inter/impl/FourFireStrategy.java +++ b/src/main/java/com/msb/inter/impl/FourFireStrategy.java @@ -23,7 +23,8 @@ public class FourFireStrategy implements FireStrategy { int bX = tank.getX() + Tank.WIDTH/2 - Bullet.WIDTH/2; int bY = tank.getY()+Tank.HEIGHT/2-Bullet.HEIGHT/2; for (DirEnum value : DirEnum.values()) { - new Bullet(bX, bY, value, tank.getGroup(), tank.getGameModel()); +// new Bullet(bX, bY, value, tank.getGroup(), tank.getGameModel()); + new Bullet(bX, bY, value, tank.getGroup()); } } } diff --git a/src/main/java/com/msb/model/Bullet.java b/src/main/java/com/msb/model/Bullet.java index 6366ee1..5c3960d 100644 --- a/src/main/java/com/msb/model/Bullet.java +++ b/src/main/java/com/msb/model/Bullet.java @@ -24,28 +24,26 @@ public class Bullet extends GameObject { public static final int SPEED = 8; private boolean live = true; private GroupEnum group = GroupEnum.BAD; - private GameModel gameModel; private Rectangle rectangle = new Rectangle(); - public Bullet(int x, int y, DirEnum dir, GroupEnum group, GameModel gameModel) { + public Bullet(int x, int y, DirEnum dir, GroupEnum group) { this.x = x; this.y = y; this.dir = dir; this.group = group; - this.gameModel = gameModel; rectangle.x = this.x; rectangle.x = this.y; rectangle.width = WIDTH; rectangle.height = HEIGHT; - gameModel.gameObjects.add(this); + GameModel.getInstance().add(this); } @Override public void paint(Graphics g) { if( ! this.live) { - gameModel.gameObjects.remove(this); + GameModel.getInstance().remove(this); return; } diff --git a/src/main/java/com/msb/model/Explode.java b/src/main/java/com/msb/model/Explode.java index ae8628a..df8e5c3 100644 --- a/src/main/java/com/msb/model/Explode.java +++ b/src/main/java/com/msb/model/Explode.java @@ -16,16 +16,14 @@ import java.awt.*; public class Explode extends GameObject { // private int x,y; - private GameModel gameModel; public static final int WIDTH = ResourcesMgr.explodeImages[0].getWidth(); public static final int HEIGHT = ResourcesMgr.explodeImages[0].getHeight(); // private boolean live = true; private int step = 0; - public Explode(int x, int y, GameModel gameModel) { + public Explode(int x, int y) { this.x = x; this.y = y; - this.gameModel = gameModel; } @Override @@ -35,7 +33,7 @@ public class Explode extends GameObject { if(step >= ResourcesMgr.explodeImages.length) { step = 0; // gameModel.explodes.remove(this); - gameModel.gameObjects.remove(this); + GameModel.getInstance().remove(this); } } diff --git a/src/main/java/com/msb/model/GameModel.java b/src/main/java/com/msb/model/GameModel.java index 3387eba..53dc60b 100644 --- a/src/main/java/com/msb/model/GameModel.java +++ b/src/main/java/com/msb/model/GameModel.java @@ -30,7 +30,9 @@ import java.util.List; */ public class GameModel { - Tank myTank = new Tank(100, 500, DirEnum.RIGHT, GroupEnum.GOOD, this); + public static final GameModel INSTANCE = new GameModel(); + + Tank myTank = new Tank(100, 500, DirEnum.RIGHT, GroupEnum.GOOD); /* public List bullets = new ArrayList<>(); public List tanks = new ArrayList<>(); //敌方坦克 @@ -40,12 +42,22 @@ public class GameModel { private ColliderChain colliderChain = new ColliderChain(); // public GameFactory gameFactory = new DefaultFactory(); - public GameModel() { + static { + INSTANCE.init(); + } + + private GameModel() {}; + + public static GameModel getInstance() { + return INSTANCE; + } + + public void init() { int initTankCount = Integer.parseInt(PropertyMgr.getString("initTankCount")); //创建5个敌方坦克 for (int i=0; i