坦克大战(一期)-设计模式-将调停者模式使用到项目中

DesignPatterns
bingor 2 years ago
parent 0092ad544e
commit 09d84ecaec

@ -10,7 +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();
//现实当中,虽然可以过按键来改变方块的坐标并且重新刷新画板来实现移动,但是敌方的坦克应该是自动在跑 //现实当中,虽然可以过按键来改变方块的坐标并且重新刷新画板来实现移动,但是敌方的坦克应该是自动在跑
while (true) { while (true) {
Thread.sleep(50); Thread.sleep(50);

@ -19,8 +19,8 @@ import java.util.Objects;
public class TankFrame extends Frame { public class TankFrame extends Frame {
GameModel gameModel = new GameModel(); GameModel gameModel = new GameModel();
public static final int GAME_WIDTH = 1000; public static final int GAME_WIDTH = 1400;
public static final int GAME_HEIGHT = 800; public static final int GAME_HEIGHT = 1050;
public TankFrame() { public TankFrame() {

@ -32,6 +32,7 @@ public class DefaultFactory extends GameFactory {
@Override @Override
public BaseExplode createExplode(int x, int y, GameModel gameModel) { public BaseExplode createExplode(int x, int y, GameModel gameModel) {
return new Explode(x, y, gameModel); // return new Explode(x, y, gameModel);
return null;
} }
} }

@ -0,0 +1,13 @@
package com.msb.inter;
import com.msb.model.abstracts.GameObject;
/**
* @Author bingor
* @Date 2022/10/11 9:39
* @Description: com.msb.inter
* @Version: 1.0
*/
public interface Collider {
public void collide(GameObject o1, GameObject o2);
}

@ -0,0 +1,38 @@
package com.msb.inter.impl;/**
* @Author bingor
* @Date 2022/10/11 9:41
* @Description: com.msb.inter.impl
* @Version: 1.0
*/
import com.msb.inter.Collider;
import com.msb.model.Bullet;
import com.msb.model.Tank;
import com.msb.model.abstracts.GameObject;
/**
*@ClassName BulletTankCollider
*@Description TODO
*@Author bingor
*@Date 2022/10/11 9:41
*@Version 3.0
*/
public class BulletTankCollider implements Collider {
@Override
public void collide(GameObject o1, GameObject o2) {
if(o1 instanceof Bullet && o2 instanceof Tank) {
Bullet bullet = (Bullet) o1;
Tank tank = (Tank) o2;
if(bullet.getGroup() == tank.getGroup()) return;
if(bullet.getRectangle().intersects(tank.getRectangle())) {
bullet.die();
tank.die();
}
} else if (o1 instanceof Tank && o2 instanceof Bullet) {
collide(o2, o1);
} else {
return;
}
}
}

@ -0,0 +1,33 @@
package com.msb.inter.impl;/**
* @Author bingor
* @Date 2022/10/11 9:41
* @Description: com.msb.inter.impl
* @Version: 1.0
*/
import com.msb.inter.Collider;
import com.msb.model.Tank;
import com.msb.model.abstracts.GameObject;
/**
*@ClassName BulletTankCollider
*@Description TODO
*@Author bingor
*@Date 2022/10/11 9:41
*@Version 3.0
*/
public class TankTankCollider implements Collider {
@Override
public void collide(GameObject o1, GameObject o2) {
if(o1 instanceof Tank && o2 instanceof Tank) {
Tank tank1 = (Tank) o1;
Tank tank2 = (Tank) o2;
if(tank1.getRectangle().intersects(tank2.getRectangle())) {
tank1.goBack();
tank2.goBack();
}
} else {
return;
}
}
}

@ -4,7 +4,7 @@ import com.msb.TankFrame;
import com.msb.base.ResourcesMgr; import com.msb.base.ResourcesMgr;
import com.msb.enums.DirEnum; import com.msb.enums.DirEnum;
import com.msb.enums.GroupEnum; import com.msb.enums.GroupEnum;
import com.msb.model.abstracts.BaseBullet; import com.msb.model.abstracts.GameObject;
import java.awt.*; import java.awt.*;
@ -14,9 +14,10 @@ import java.awt.*;
* @Description: * @Description:
* @Version: 1.0 * @Version: 1.0
*/ */
public class Bullet extends BaseBullet { //public class Bullet extends BaseBullet {
public class Bullet extends GameObject {
private int x, y; // private int x, y;
private DirEnum dir; private DirEnum dir;
public static final int WIDTH = ResourcesMgr.bulletD.getWidth(); public static final int WIDTH = ResourcesMgr.bulletD.getWidth();
public static final int HEIGHT = ResourcesMgr.bulletD.getHeight(); public static final int HEIGHT = ResourcesMgr.bulletD.getHeight();
@ -38,13 +39,13 @@ public class Bullet extends BaseBullet {
rectangle.width = WIDTH; rectangle.width = WIDTH;
rectangle.height = HEIGHT; rectangle.height = HEIGHT;
gameModel.bullets.add(this); gameModel.gameObjects.add(this);
} }
@Override @Override
public void paint(Graphics g) { public void paint(Graphics g) {
if( ! this.live) { if( ! this.live) {
gameModel.bullets.remove(this); gameModel.gameObjects.remove(this);
return; return;
} }
@ -109,7 +110,15 @@ public class Bullet extends BaseBullet {
} }
private void die() { public void die() {
this.live = false; this.live = false;
} }
public GroupEnum getGroup() {
return group;
}
public Rectangle getRectangle() {
return rectangle;
}
} }

@ -36,7 +36,8 @@ public class CircleExplode extends BaseExplode {
step++; step++;
if(step >= 10) { if(step >= 10) {
step = 0; step = 0;
gameModel.explodes.remove(this); // gameModel.explodes.remove(this);
gameModel.gameObjects.remove(this);
} }
g.setColor(color); g.setColor(color);

@ -2,6 +2,7 @@ package com.msb.model;
import com.msb.base.ResourcesMgr; import com.msb.base.ResourcesMgr;
import com.msb.model.abstracts.BaseExplode; import com.msb.model.abstracts.BaseExplode;
import com.msb.model.abstracts.GameObject;
import java.awt.*; import java.awt.*;
@ -11,9 +12,10 @@ import java.awt.*;
* @Description: com.msb * @Description: com.msb
* @Version: 1.0 * @Version: 1.0
*/ */
public class Explode extends BaseExplode { //public class Explode extends BaseExplode {
public class Explode extends GameObject {
private int x,y; // private int x,y;
private GameModel gameModel; 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();
@ -32,7 +34,8 @@ 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;
gameModel.explodes.remove(this); // gameModel.explodes.remove(this);
gameModel.gameObjects.remove(this);
} }
} }

@ -11,7 +11,10 @@ import com.msb.enums.GroupEnum;
import com.msb.factorys.CircleFactory; import com.msb.factorys.CircleFactory;
import com.msb.factorys.DefaultFactory; import com.msb.factorys.DefaultFactory;
import com.msb.factorys.abstracts.GameFactory; import com.msb.factorys.abstracts.GameFactory;
import com.msb.inter.impl.BulletTankCollider;
import com.msb.inter.impl.TankTankCollider;
import com.msb.model.abstracts.BaseExplode; import com.msb.model.abstracts.BaseExplode;
import com.msb.model.abstracts.GameObject;
import java.awt.*; import java.awt.*;
import java.util.ArrayList; import java.util.ArrayList;
@ -27,56 +30,72 @@ import java.util.List;
public class GameModel { public class GameModel {
Tank myTank = new Tank(100, 500, DirEnum.RIGHT, GroupEnum.GOOD, this); 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<Bullet> bullets = new ArrayList<>();
public List<Tank> tanks = new ArrayList<>(); //敌方坦克 public List<Tank> tanks = new ArrayList<>(); //敌方坦克
public List<BaseExplode> explodes = new ArrayList<>(); //爆炸效果 public List<Explode> explodes = new ArrayList<>(); //爆炸效果
*/
public List<GameObject> gameObjects = new ArrayList<>();
private BulletTankCollider bulletTankCollider = new BulletTankCollider();
private TankTankCollider tankTankCollider = new TankTankCollider();
// public GameFactory gameFactory = new DefaultFactory(); // public GameFactory gameFactory = new DefaultFactory();
public GameModel() { public GameModel() {
// Random random = new Random();
int initTankCount = Integer.parseInt(PropertyMgr.getString("initTankCount")); int initTankCount = Integer.parseInt(PropertyMgr.getString("initTankCount"));
//创建5个敌方坦克 //创建5个敌方坦克
for (int i=0; i<initTankCount; i++) { 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.valueOf(random.nextInt(30)%4), GroupEnum.BAD, tankFrame));
this.tanks.add(new Tank(100 + i*80, 100, DirEnum.DOWN, GroupEnum.BAD, this)); this.gameObjects.add(new Tank(100 + i*120, 100, DirEnum.DOWN, GroupEnum.BAD, this));
} }
} }
public void paint(Graphics g) { public void paint(Graphics g) {
Color color = g.getColor(); Color color = g.getColor();
g.setColor(Color.WHITE); g.setColor(Color.WHITE);
g.drawString("子弹的数量:" + bullets.size(), 10, 60); /*g.drawString("子弹的数量:" + bullets.size(), 10, 60);
g.drawString("敌方坦克数量:" + tanks.size(), 10, 90); g.drawString("敌方坦克数量:" + tanks.size(), 10, 90);
g.drawString("爆炸的数量:" + explodes.size(), 10, 120); g.drawString("爆炸的数量:" + explodes.size(), 10, 120);*/
g.setColor(color); g.setColor(color);
myTank.paint(g); myTank.paint(g);
// explode.paint(g); // explode.paint(g);
//画出敌方坦克 //画出敌方坦克
for (int i=0; i<tanks.size(); i++) { /*for (int i=0; i<tanks.size(); i++) {
tanks.get(i).paint(g); tanks.get(i).paint(g);
} }
//画出子弹
for (int i=0; i<bullets.size(); i++) { for (int i=0; i<bullets.size(); i++) {
bullets.get(i).paint(g); bullets.get(i).paint(g);
} }
//爆炸效果
for (int i=0; i<explodes.size(); i++) {
explodes.get(i).paint(g);
}*/
for (int i=0; i<bullets.size(); i++) { /*for (int i=0; i<bullets.size(); i++) {
for (int j=0; j<tanks.size(); j++) { for (int j=0; j<tanks.size(); j++) {
bullets.get(i).collide(tanks.get(j)); bullets.get(i).collide(tanks.get(j));
} }
}*/
for (int i=0; i<gameObjects.size(); i++) {
gameObjects.get(i).paint(g);
} }
//爆炸效果 for (int i=0; i<gameObjects.size(); i++) {
for (int i=0; i<explodes.size(); i++) { for (int j=i+1; j<gameObjects.size(); j++) {
explodes.get(i).paint(g); // bullets.get(i).collide(tanks.get(j));
GameObject o1 = gameObjects.get(i);
GameObject o2 = gameObjects.get(j);
bulletTankCollider.collide(o1, o2);
tankTankCollider.collide(o1, o2);
} }
} }
}
public Tank getMainTank() { public Tank getMainTank() {
return myTank; return myTank;
} }

@ -6,7 +6,7 @@ import com.msb.base.ResourcesMgr;
import com.msb.enums.DirEnum; import com.msb.enums.DirEnum;
import com.msb.enums.GroupEnum; import com.msb.enums.GroupEnum;
import com.msb.inter.FireStrategy; import com.msb.inter.FireStrategy;
import com.msb.model.abstracts.BaseTank; import com.msb.model.abstracts.GameObject;
import java.awt.*; import java.awt.*;
import java.util.Random; import java.util.Random;
@ -17,9 +17,9 @@ import java.util.Random;
* @Description: com.msb * @Description: com.msb
* @Version: 1.0 * @Version: 1.0
*/ */
public class Tank extends BaseTank { //public class Tank extends BaseTank {
public class Tank extends GameObject {
private int x,y;
private DirEnum dir; private DirEnum dir;
private static final int SPEED = 4; private static final int SPEED = 4;
private boolean move = true; private boolean move = true;
@ -33,6 +33,8 @@ public class Tank extends BaseTank {
private Rectangle rectangle = new Rectangle(); private Rectangle rectangle = new Rectangle();
private FireStrategy fireStrategy; private FireStrategy fireStrategy;
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, GameModel gameModel) {
this.x = x; this.x = x;
this.y = y; this.y = y;
@ -64,7 +66,7 @@ public class Tank extends BaseTank {
public void paint(Graphics g) { public void paint(Graphics g) {
if( ! live) { if( ! live) {
gameModel.tanks.remove(this); gameModel.gameObjects.remove(this);
return; return;
} }
@ -91,6 +93,9 @@ public class Tank extends BaseTank {
if( ! move) return; if( ! move) return;
this.oldX = this.x;
this.oldY = this.y;
switch (dir) { switch (dir) {
case UP: y -= SPEED; break; case UP: y -= SPEED; break;
case DOWN: y += SPEED; break; case DOWN: y += SPEED; break;
@ -145,7 +150,7 @@ 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;
gameModel.explodes.add(new Explode(eX, eY, gameModel)); gameModel.gameObjects.add(new Explode(eX, eY, gameModel));
// gameModel.explodes.add(gameModel.gameFactory.createExplode(eX, eY, gameModel)); // gameModel.explodes.add(gameModel.gameFactory.createExplode(eX, eY, gameModel));
} }
@ -160,4 +165,17 @@ public class Tank extends BaseTank {
public GameModel getGameModel() { public GameModel getGameModel() {
return gameModel; return gameModel;
} }
public int getOldX() {
return oldX;
}
public int getOldY() {
return oldY;
}
public void goBack() {
this.x = this.oldX;
this.y = this.oldY;
}
} }

@ -0,0 +1,20 @@
package com.msb.model.abstracts;/**
* @Author bingor
* @Date 2022/10/11 9:23
* @Description: com.msb.model.abstracts
* @Version: 1.0
*/
import java.awt.*;
/**
*@ClassName GameObject
*@Description TODO
*@Author bingor
*@Date 2022/10/11 9:23
*@Version 3.0
*/
public abstract class GameObject {
protected int x,y;
public abstract void paint(Graphics g);
}
Loading…
Cancel
Save