坦克大战(一期)-设计模式-把GameModel做成单例(饿汉式)

DesignPatterns
bingor 2 years ago
parent d09a6719d7
commit ef41cce8ed

@ -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);
}
}

@ -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());
}
}

@ -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());
}
}
}

@ -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;
}

@ -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);
}
}

@ -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<Bullet> bullets = new ArrayList<>();
public List<Tank> 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<initTankCount; i++) {
// tankFrame.tanks.add(new Tank(100 + i*80, 100, DirEnum.valueOf(random.nextInt(30)%4), GroupEnum.BAD, tankFrame));
this.gameObjects.add(new Tank(100 + i*120, 100, DirEnum.DOWN, GroupEnum.BAD, this));
gameObjects.add(new Tank(100 + i*120, 100, DirEnum.DOWN, GroupEnum.BAD));
}
}
@ -98,4 +110,12 @@ public class GameModel {
public Tank getMainTank() {
return myTank;
}
public void add(GameObject gameObject) {
gameObjects.add(gameObject);
}
public void remove(GameObject gameObject) {
gameObjects.remove(gameObject);
}
}

@ -23,8 +23,6 @@ public class Tank extends GameObject {
private DirEnum dir;
private static final int SPEED = 4;
private boolean move = true;
//为了解决能够在坦克中发射子弹,将创建的子弹通过坦克发射出来,那么需要在坦克类中持有游戏窗口的引用
private GameModel gameModel;
public static final int WIDTH = ResourcesMgr.goodTankU.getWidth();
public static final int HEIGHT = ResourcesMgr.goodTankU.getHeight();
private boolean live = true;
@ -35,12 +33,11 @@ public class Tank extends GameObject {
private int oldX, oldY;
public Tank(int x, int y, DirEnum dir, GroupEnum group, GameModel gameModel) {
public Tank(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;
@ -66,7 +63,7 @@ public class Tank extends GameObject {
public void paint(Graphics g) {
if( ! live) {
gameModel.gameObjects.remove(this);
GameModel.getInstance().remove(this);
return;
}
@ -150,7 +147,7 @@ public class Tank extends GameObject {
this.live = false;
int eX = this.x + WIDTH/2 - Explode.WIDTH/2;
int eY = this.y + HEIGHT/2 - Explode.HEIGHT/2;
gameModel.gameObjects.add(new Explode(eX, eY, gameModel));
GameModel.getInstance().add(new Explode(eX, eY));
// gameModel.explodes.add(gameModel.gameFactory.createExplode(eX, eY, gameModel));
}
@ -162,10 +159,6 @@ public class Tank extends GameObject {
return rectangle;
}
public GameModel getGameModel() {
return gameModel;
}
public int getOldX() {
return oldX;
}

@ -0,0 +1,48 @@
package com.msb.model;/**
* @Author bingor
* @Date 2022/10/12 14:50
* @Description: com.msb.model
* @Version: 1.0
*/
import com.msb.model.abstracts.GameObject;
import java.awt.*;
/**
*@ClassName Wall
*@Description TODO
*@Author bingor
*@Date 2022/10/12 14:50
*@Version 3.0
*/
public class Wall extends GameObject {
private int width, height;
private Rectangle rectangle;
public Wall(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.rectangle = new Rectangle(x, y, width, height);
}
@Override
public void paint(Graphics g) {
GameModel.getInstance().paint(g);
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public Rectangle getRectangle() {
return rectangle;
}
}

@ -33,4 +33,19 @@ public class CommonTest {
System.out.println(DirEnum.valueOf(random.nextInt(3)));
}
@Test
public void testCommon() {
boolean bool = xh();
System.out.println(bool);
}
public boolean xh() {
int [] indexs = {1, 2, 3, 4, 5, 6, 7, 8, 9 };
for (int index : indexs) {
if(index == 5) return false;
System.out.println(index);
}
return true;
}
}

Loading…
Cancel
Save