坦克大战(一期)-设计模式-将门面模型使用到项目中,使用GameModel作为facade与Frame打交道

DesignPatterns
bingor 2 years ago
parent 98546b1759
commit 0092ad544e

@ -1,12 +1,5 @@
package com.msb; package com.msb;
import com.msb.base.PropertyMgr;
import com.msb.enums.DirEnum;
import com.msb.enums.GroupEnum;
import com.msb.model.Tank;
import java.util.Random;
/** /**
* @Author bingor * @Author bingor
* @Date 2022-09-29 11:02 * @Date 2022-09-29 11:02
@ -17,15 +10,6 @@ public class TankDemo {
public static void main(String[] args) throws InterruptedException { public static void main(String[] args) throws InterruptedException {
TankFrame tankFrame = new TankFrame(); TankFrame tankFrame = new TankFrame();
Random random = new Random();
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));
tankFrame.tanks.add(new Tank(100 + i*80, 100, DirEnum.DOWN, GroupEnum.BAD, tankFrame));
}
//现实当中,虽然可以过按键来改变方块的坐标并且重新刷新画板来实现移动,但是敌方的坦克应该是自动在跑 //现实当中,虽然可以过按键来改变方块的坐标并且重新刷新画板来实现移动,但是敌方的坦克应该是自动在跑
while (true) { while (true) {

@ -1,22 +1,13 @@
package com.msb; package com.msb;
import com.msb.enums.DirEnum; import com.msb.enums.DirEnum;
import com.msb.enums.GroupEnum; import com.msb.model.GameModel;
import com.msb.factorys.CircleFactory;
import com.msb.factorys.DefaultFactory;
import com.msb.factorys.abstracts.GameFactory;
import com.msb.model.Bullet;
import com.msb.model.Explode;
import com.msb.model.Tank;
import com.msb.model.abstracts.BaseExplode;
import java.awt.*; import java.awt.*;
import java.awt.event.KeyAdapter; import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter; import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects; import java.util.Objects;
/** /**
@ -27,15 +18,10 @@ import java.util.Objects;
*/ */
public class TankFrame extends Frame { public class TankFrame extends Frame {
Tank myTank = new Tank(100, 500, DirEnum.RIGHT, GroupEnum.GOOD, this); GameModel gameModel = new GameModel();
// Explode explode = new Explode(100, 100, this);
// Bullet bullet = new Bullet(200, 200, DirEnum.DOWN);
public List<Bullet> bullets = new ArrayList<>();
public List<Tank> tanks = new ArrayList<>(); //敌方坦克
public List<BaseExplode> explodes = new ArrayList<>(); //爆炸效果
public static final int GAME_WIDTH = 1000; public static final int GAME_WIDTH = 1000;
public static final int GAME_HEIGHT = 800; public static final int GAME_HEIGHT = 800;
public GameFactory gameFactory = new CircleFactory();
public TankFrame() { public TankFrame() {
@ -60,36 +46,7 @@ public class TankFrame extends Frame {
@Override @Override
public void paint(Graphics g) { public void paint(Graphics g) {
// super.paint(g); // super.paint(g);
Color color = g.getColor(); gameModel.paint(g);
g.setColor(Color.WHITE);
g.drawString("子弹的数量:" + bullets.size(), 10, 60);
g.drawString("敌方坦克数量:" + tanks.size(), 10, 90);
g.drawString("爆炸的数量:" + explodes.size(), 10, 120);
g.setColor(color);
myTank.paint(g);
// explode.paint(g);
//画出敌方坦克
for (int i=0; i<tanks.size(); i++) {
tanks.get(i).paint(g);
}
for (int i=0; i<bullets.size(); i++) {
bullets.get(i).paint(g);
}
for (int i=0; i<bullets.size(); i++) {
for (int j=0; j<tanks.size(); j++) {
bullets.get(i).collide(tanks.get(j));
}
}
//爆炸效果
for (int i=0; i<explodes.size(); i++) {
explodes.get(i).paint(g);
}
} }
//双缓冲,解决闪烁现象 //双缓冲,解决闪烁现象
@ -138,7 +95,7 @@ public class TankFrame extends Frame {
case KeyEvent.VK_RIGHT : bR = false; break; case KeyEvent.VK_RIGHT : bR = false; break;
case KeyEvent.VK_UP : bU = false; break; case KeyEvent.VK_UP : bU = false; break;
case KeyEvent.VK_DOWN : bD = false; break; case KeyEvent.VK_DOWN : bD = false; break;
case KeyEvent.VK_SPACE: myTank.fire(); break; case KeyEvent.VK_SPACE: gameModel.getMainTank().fire(); break;
default: break; default: break;
} }
setDir(); setDir();
@ -147,16 +104,16 @@ public class TankFrame extends Frame {
private void setDir() { private void setDir() {
//当没有任何方向键按下的时候,那么让坦克停止 //当没有任何方向键按下的时候,那么让坦克停止
if( !bL && !bR && !bU && !bD ) { if( !bL && !bR && !bU && !bD ) {
myTank.setMove(false); gameModel.getMainTank().setMove(false);
} else { } else {
myTank.setMove(true); gameModel.getMainTank().setMove(true);
} }
//为什么不用if-else 因为有可能是一起按两个方向键,那最后拿到哪个算哪个 //为什么不用if-else 因为有可能是一起按两个方向键,那最后拿到哪个算哪个
if(bL) myTank.setDir(DirEnum.LEFT); if(bL) gameModel.getMainTank().setDir(DirEnum.LEFT);
if(bR) myTank.setDir(DirEnum.RIGHT); if(bR) gameModel.getMainTank().setDir(DirEnum.RIGHT);
if(bU) myTank.setDir(DirEnum.UP); if(bU) gameModel.getMainTank().setDir(DirEnum.UP);
if(bD) myTank.setDir(DirEnum.DOWN); if(bD) gameModel.getMainTank().setDir(DirEnum.DOWN);
} }
} }

@ -5,10 +5,9 @@ package com.msb.factorys;/**
* @Version: 1.0 * @Version: 1.0
*/ */
import com.msb.TankFrame;
import com.msb.factorys.abstracts.GameFactory; import com.msb.factorys.abstracts.GameFactory;
import com.msb.model.CircleExplode; import com.msb.model.CircleExplode;
import com.msb.model.Explode; import com.msb.model.GameModel;
import com.msb.model.abstracts.BaseBullet; import com.msb.model.abstracts.BaseBullet;
import com.msb.model.abstracts.BaseExplode; import com.msb.model.abstracts.BaseExplode;
import com.msb.model.abstracts.BaseTank; import com.msb.model.abstracts.BaseTank;
@ -22,17 +21,17 @@ import com.msb.model.abstracts.BaseTank;
*/ */
public class CircleFactory extends GameFactory { public class CircleFactory extends GameFactory {
@Override @Override
public BaseTank createTank(int x, int y, TankFrame tf) { public BaseTank createTank(int x, int y, GameModel gameModel) {
return null; return null;
} }
@Override @Override
public BaseBullet createBullet(int x, int y, TankFrame tf) { public BaseBullet createBullet(int x, int y, GameModel gameModel) {
return null; return null;
} }
@Override @Override
public BaseExplode createExplode(int x, int y, TankFrame tf) { public BaseExplode createExplode(int x, int y, GameModel gameModel) {
return new CircleExplode(x, y, tf); return new CircleExplode(x, y, gameModel);
} }
} }

@ -5,9 +5,9 @@ package com.msb.factorys;/**
* @Version: 1.0 * @Version: 1.0
*/ */
import com.msb.TankFrame;
import com.msb.factorys.abstracts.GameFactory; import com.msb.factorys.abstracts.GameFactory;
import com.msb.model.Explode; import com.msb.model.Explode;
import com.msb.model.GameModel;
import com.msb.model.abstracts.BaseBullet; import com.msb.model.abstracts.BaseBullet;
import com.msb.model.abstracts.BaseExplode; import com.msb.model.abstracts.BaseExplode;
import com.msb.model.abstracts.BaseTank; import com.msb.model.abstracts.BaseTank;
@ -21,17 +21,17 @@ import com.msb.model.abstracts.BaseTank;
*/ */
public class DefaultFactory extends GameFactory { public class DefaultFactory extends GameFactory {
@Override @Override
public BaseTank createTank(int x, int y, TankFrame tf) { public BaseTank createTank(int x, int y, GameModel gameModel) {
return null; return null;
} }
@Override @Override
public BaseBullet createBullet(int x, int y, TankFrame tf) { public BaseBullet createBullet(int x, int y, GameModel gameModel) {
return null; return null;
} }
@Override @Override
public BaseExplode createExplode(int x, int y, TankFrame tf) { public BaseExplode createExplode(int x, int y, GameModel gameModel) {
return new Explode(x, y, tf); return new Explode(x, y, gameModel);
} }
} }

@ -5,7 +5,7 @@ package com.msb.factorys.abstracts;/**
* @Version: 1.0 * @Version: 1.0
*/ */
import com.msb.TankFrame; import com.msb.model.GameModel;
import com.msb.model.abstracts.BaseBullet; import com.msb.model.abstracts.BaseBullet;
import com.msb.model.abstracts.BaseExplode; import com.msb.model.abstracts.BaseExplode;
import com.msb.model.abstracts.BaseTank; import com.msb.model.abstracts.BaseTank;
@ -18,7 +18,7 @@ import com.msb.model.abstracts.BaseTank;
*@Version 3.0 *@Version 3.0
*/ */
public abstract class GameFactory { public abstract class GameFactory {
public abstract BaseTank createTank(int x, int y, TankFrame tf); public abstract BaseTank createTank(int x, int y, GameModel gameModel);
public abstract BaseBullet createBullet(int x, int y, TankFrame tf); public abstract BaseBullet createBullet(int x, int y, GameModel gameModel);
public abstract BaseExplode createExplode(int x, int y, TankFrame tf); public abstract BaseExplode createExplode(int x, int y, GameModel gameModel);
} }

@ -21,6 +21,6 @@ public class DefaultFireStrategy implements FireStrategy {
public void fire(Tank tank) { public void fire(Tank tank) {
int bX = tank.getX() + Tank.WIDTH/2 - Bullet.WIDTH/2; int bX = tank.getX() + Tank.WIDTH/2 - Bullet.WIDTH/2;
int bY = tank.getY()+Tank.HEIGHT/2-Bullet.HEIGHT/2; int bY = tank.getY()+Tank.HEIGHT/2-Bullet.HEIGHT/2;
new Bullet(bX, bY, tank.getDir(), tank.getGroup(), tank.getTankFrame()); new Bullet(bX, bY, tank.getDir(), tank.getGroup(), tank.getGameModel());
} }
} }

@ -23,7 +23,7 @@ public class FourFireStrategy implements FireStrategy {
int bX = tank.getX() + Tank.WIDTH/2 - Bullet.WIDTH/2; int bX = tank.getX() + Tank.WIDTH/2 - Bullet.WIDTH/2;
int bY = tank.getY()+Tank.HEIGHT/2-Bullet.HEIGHT/2; int bY = tank.getY()+Tank.HEIGHT/2-Bullet.HEIGHT/2;
for (DirEnum value : DirEnum.values()) { for (DirEnum value : DirEnum.values()) {
new Bullet(bX, bY, value, tank.getGroup(), tank.getTankFrame()); new Bullet(bX, bY, value, tank.getGroup(), tank.getGameModel());
} }
} }
} }

@ -23,28 +23,28 @@ public class Bullet extends BaseBullet {
public static final int SPEED = 8; public static final int SPEED = 8;
private boolean live = true; private boolean live = true;
private GroupEnum group = GroupEnum.BAD; private GroupEnum group = GroupEnum.BAD;
private TankFrame tankFrame; private GameModel gameModel;
private Rectangle rectangle = new 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, GameModel gameModel) {
this.x = x; this.x = x;
this.y = y; this.y = y;
this.dir = dir; this.dir = dir;
this.group = group; this.group = group;
this.tankFrame = tankFrame; this.gameModel = gameModel;
rectangle.x = this.x; rectangle.x = this.x;
rectangle.x = this.y; rectangle.x = this.y;
rectangle.width = WIDTH; rectangle.width = WIDTH;
rectangle.height = HEIGHT; rectangle.height = HEIGHT;
tankFrame.bullets.add(this); gameModel.bullets.add(this);
} }
@Override @Override
public void paint(Graphics g) { public void paint(Graphics g) {
if( ! this.live) { if( ! this.live) {
tankFrame.bullets.remove(this); gameModel.bullets.remove(this);
return; return;
} }

@ -5,8 +5,6 @@ package com.msb.model;/**
* @Version: 1.0 * @Version: 1.0
*/ */
import com.msb.TankFrame;
import com.msb.base.ResourcesMgr;
import com.msb.model.abstracts.BaseExplode; import com.msb.model.abstracts.BaseExplode;
import java.awt.*; import java.awt.*;
@ -20,13 +18,13 @@ import java.awt.*;
*/ */
public class CircleExplode extends BaseExplode { public class CircleExplode extends BaseExplode {
private int x,y; private int x,y;
private TankFrame tankFrame; private GameModel gameModel;
private int step = 0; private int step = 0;
public CircleExplode(int x, int y, TankFrame tankFrame) { public CircleExplode(int x, int y, GameModel gameModel) {
this.x = x; this.x = x;
this.y = y; this.y = y;
this.tankFrame = tankFrame; this.gameModel = gameModel;
} }
@Override @Override
@ -38,7 +36,7 @@ public class CircleExplode extends BaseExplode {
step++; step++;
if(step >= 10) { if(step >= 10) {
step = 0; step = 0;
tankFrame.explodes.remove(this); gameModel.explodes.remove(this);
} }
g.setColor(color); g.setColor(color);

@ -1,6 +1,5 @@
package com.msb.model; package com.msb.model;
import com.msb.TankFrame;
import com.msb.base.ResourcesMgr; import com.msb.base.ResourcesMgr;
import com.msb.model.abstracts.BaseExplode; import com.msb.model.abstracts.BaseExplode;
@ -15,16 +14,16 @@ import java.awt.*;
public class Explode extends BaseExplode { public class Explode extends BaseExplode {
private int x,y; private int x,y;
private TankFrame tankFrame; private GameModel gameModel;
public static final int WIDTH = ResourcesMgr.explodeImages[0].getWidth(); public static final int WIDTH = ResourcesMgr.explodeImages[0].getWidth();
public static final int HEIGHT = ResourcesMgr.explodeImages[0].getHeight(); public static final int HEIGHT = ResourcesMgr.explodeImages[0].getHeight();
// private boolean live = true; // private boolean live = true;
private int step = 0; private int step = 0;
public Explode(int x, int y, TankFrame tankFrame) { public Explode(int x, int y, GameModel gameModel) {
this.x = x; this.x = x;
this.y = y; this.y = y;
this.tankFrame = tankFrame; this.gameModel = gameModel;
} }
@Override @Override
@ -33,7 +32,7 @@ public class Explode extends BaseExplode {
g.drawImage(ResourcesMgr.explodeImages[step++], x, y, null); g.drawImage(ResourcesMgr.explodeImages[step++], x, y, null);
if(step >= ResourcesMgr.explodeImages.length) { if(step >= ResourcesMgr.explodeImages.length) {
step = 0; step = 0;
tankFrame.explodes.remove(this); gameModel.explodes.remove(this);
} }
} }

@ -0,0 +1,83 @@
package com.msb.model;/**
* @Author bingor
* @Date 2022/10/10 16:30
* @Description: com.msb.model
* @Version: 1.0
*/
import com.msb.base.PropertyMgr;
import com.msb.enums.DirEnum;
import com.msb.enums.GroupEnum;
import com.msb.factorys.CircleFactory;
import com.msb.factorys.DefaultFactory;
import com.msb.factorys.abstracts.GameFactory;
import com.msb.model.abstracts.BaseExplode;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
/**
*@ClassName GameModel
*@Description TODO
*@Author bingor
*@Date 2022/10/10 16:30
*@Version 3.0
*/
public class GameModel {
Tank myTank = new Tank(100, 500, DirEnum.RIGHT, GroupEnum.GOOD, this);
// Explode explode = new Explode(100, 100, this);
// Bullet bullet = new Bullet(200, 200, DirEnum.DOWN);
public List<Bullet> bullets = new ArrayList<>();
public List<Tank> tanks = new ArrayList<>(); //敌方坦克
public List<BaseExplode> explodes = new ArrayList<>(); //爆炸效果
// public GameFactory gameFactory = new DefaultFactory();
public GameModel() {
// Random random = new Random();
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.tanks.add(new Tank(100 + i*80, 100, DirEnum.DOWN, GroupEnum.BAD, this));
}
}
public void paint(Graphics g) {
Color color = g.getColor();
g.setColor(Color.WHITE);
g.drawString("子弹的数量:" + bullets.size(), 10, 60);
g.drawString("敌方坦克数量:" + tanks.size(), 10, 90);
g.drawString("爆炸的数量:" + explodes.size(), 10, 120);
g.setColor(color);
myTank.paint(g);
// explode.paint(g);
//画出敌方坦克
for (int i=0; i<tanks.size(); i++) {
tanks.get(i).paint(g);
}
for (int i=0; i<bullets.size(); i++) {
bullets.get(i).paint(g);
}
for (int i=0; i<bullets.size(); i++) {
for (int j=0; j<tanks.size(); j++) {
bullets.get(i).collide(tanks.get(j));
}
}
//爆炸效果
for (int i=0; i<explodes.size(); i++) {
explodes.get(i).paint(g);
}
}
public Tank getMainTank() {
return myTank;
}
}

@ -24,7 +24,7 @@ public class Tank extends BaseTank {
private static final int SPEED = 4; private static final int SPEED = 4;
private boolean move = true; private boolean move = true;
//为了解决能够在坦克中发射子弹,将创建的子弹通过坦克发射出来,那么需要在坦克类中持有游戏窗口的引用 //为了解决能够在坦克中发射子弹,将创建的子弹通过坦克发射出来,那么需要在坦克类中持有游戏窗口的引用
private TankFrame tankFrame; private GameModel gameModel;
public static final int WIDTH = ResourcesMgr.goodTankU.getWidth(); public static final int WIDTH = ResourcesMgr.goodTankU.getWidth();
public static final int HEIGHT = ResourcesMgr.goodTankU.getHeight(); public static final int HEIGHT = ResourcesMgr.goodTankU.getHeight();
private boolean live = true; private boolean live = true;
@ -33,12 +33,12 @@ public class Tank extends BaseTank {
private Rectangle rectangle = new Rectangle(); private Rectangle rectangle = new Rectangle();
private FireStrategy fireStrategy; private FireStrategy fireStrategy;
public Tank(int x, int y, DirEnum dir, GroupEnum group, TankFrame tankFrame) { public Tank(int x, int y, DirEnum dir, GroupEnum group, GameModel gameModel) {
this.x = x; this.x = x;
this.y = y; this.y = y;
this.dir = dir; this.dir = dir;
this.group = group; this.group = group;
this.tankFrame = tankFrame; this.gameModel = gameModel;
rectangle.x = this.x; rectangle.x = this.x;
rectangle.x = this.y; rectangle.x = this.y;
@ -64,7 +64,7 @@ public class Tank extends BaseTank {
public void paint(Graphics g) { public void paint(Graphics g) {
if( ! live) { if( ! live) {
tankFrame.tanks.remove(this); gameModel.tanks.remove(this);
return; return;
} }
@ -145,8 +145,8 @@ public class Tank extends BaseTank {
this.live = false; this.live = false;
int eX = this.x + WIDTH/2 - Explode.WIDTH/2; int eX = this.x + WIDTH/2 - Explode.WIDTH/2;
int eY = this.y + HEIGHT/2 - Explode.HEIGHT/2; int eY = this.y + HEIGHT/2 - Explode.HEIGHT/2;
// tankFrame.explodes.add(new Explode(eX, eY, tankFrame)); gameModel.explodes.add(new Explode(eX, eY, gameModel));
tankFrame.explodes.add(tankFrame.gameFactory.createExplode(eX, eY, tankFrame)); // gameModel.explodes.add(gameModel.gameFactory.createExplode(eX, eY, gameModel));
} }
public GroupEnum getGroup() { public GroupEnum getGroup() {
@ -157,7 +157,7 @@ public class Tank extends BaseTank {
return rectangle; return rectangle;
} }
public TankFrame getTankFrame() { public GameModel getGameModel() {
return tankFrame; return gameModel;
} }
} }

Loading…
Cancel
Save