坦克大战(一期)-设计模式-将抽象工厂模式运用到项目中

DesignPatterns
bingor 2 years ago
parent bc16e1a196
commit 7764e0b07e

@ -2,9 +2,13 @@ package com.msb;
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.Bullet;
import com.msb.model.Explode;
import com.msb.model.Tank;
import com.msb.model.abstracts.BaseExplode;
import java.awt.*;
import java.awt.event.KeyAdapter;
@ -28,9 +32,10 @@ public class TankFrame extends Frame {
// Bullet bullet = new Bullet(200, 200, DirEnum.DOWN);
public List<Bullet> bullets = new ArrayList<>();
public List<Tank> tanks = new ArrayList<>(); //敌方坦克
public List<Explode> explodes = new ArrayList<>(); //爆炸效果
public List<BaseExplode> explodes = new ArrayList<>(); //爆炸效果
public static final int GAME_WIDTH = 1000;
public static final int GAME_HEIGHT = 800;
public GameFactory gameFactory = new CircleFactory();
public TankFrame() {

@ -0,0 +1,38 @@
package com.msb.factorys;/**
* @Author bingor
* @Date 2022/10/9 16:42
* @Description: com.msb.factorys.abstracts
* @Version: 1.0
*/
import com.msb.TankFrame;
import com.msb.factorys.abstracts.GameFactory;
import com.msb.model.CircleExplode;
import com.msb.model.Explode;
import com.msb.model.abstracts.BaseBullet;
import com.msb.model.abstracts.BaseExplode;
import com.msb.model.abstracts.BaseTank;
/**
*@ClassName DefautFactory
*@Description TODO
*@Author bingor
*@Date 2022/10/9 16:42
*@Version 3.0
*/
public class CircleFactory extends GameFactory {
@Override
public BaseTank createTank(int x, int y, TankFrame tf) {
return null;
}
@Override
public BaseBullet createBullet(int x, int y, TankFrame tf) {
return null;
}
@Override
public BaseExplode createExplode(int x, int y, TankFrame tf) {
return new CircleExplode(x, y, tf);
}
}

@ -0,0 +1,37 @@
package com.msb.factorys;/**
* @Author bingor
* @Date 2022/10/9 16:42
* @Description: com.msb.factorys.abstracts
* @Version: 1.0
*/
import com.msb.TankFrame;
import com.msb.factorys.abstracts.GameFactory;
import com.msb.model.Explode;
import com.msb.model.abstracts.BaseBullet;
import com.msb.model.abstracts.BaseExplode;
import com.msb.model.abstracts.BaseTank;
/**
*@ClassName DefautFactory
*@Description TODO
*@Author bingor
*@Date 2022/10/9 16:42
*@Version 3.0
*/
public class DefaultFactory extends GameFactory {
@Override
public BaseTank createTank(int x, int y, TankFrame tf) {
return null;
}
@Override
public BaseBullet createBullet(int x, int y, TankFrame tf) {
return null;
}
@Override
public BaseExplode createExplode(int x, int y, TankFrame tf) {
return new Explode(x, y, tf);
}
}

@ -0,0 +1,24 @@
package com.msb.factorys.abstracts;/**
* @Author bingor
* @Date 2022/10/9 16:36
* @Description: com.msb.model.abstracts
* @Version: 1.0
*/
import com.msb.TankFrame;
import com.msb.model.abstracts.BaseBullet;
import com.msb.model.abstracts.BaseExplode;
import com.msb.model.abstracts.BaseTank;
/**
*@ClassName AbstractFactory
*@Description TODO
*@Author bingor
*@Date 2022/10/9 16:36
*@Version 3.0
*/
public abstract class GameFactory {
public abstract BaseTank createTank(int x, int y, TankFrame tf);
public abstract BaseBullet createBullet(int x, int y, TankFrame tf);
public abstract BaseExplode createExplode(int x, int y, TankFrame tf);
}

@ -0,0 +1,13 @@
package com.msb.inter;
import java.awt.*;
/**
* @Author bingor
* @Date 2022/10/9 16:31
* @Description: com.msb.inter
* @Version: 1.0
*/
public interface Paintable {
public void paint(Graphics g);
}

@ -4,6 +4,7 @@ import com.msb.TankFrame;
import com.msb.base.ResourcesMgr;
import com.msb.enums.DirEnum;
import com.msb.enums.GroupEnum;
import com.msb.model.abstracts.BaseBullet;
import java.awt.*;
@ -13,7 +14,7 @@ import java.awt.*;
* @Description:
* @Version: 1.0
*/
public class Bullet {
public class Bullet extends BaseBullet {
private int x, y;
private DirEnum dir;
@ -40,6 +41,7 @@ public class Bullet {
tankFrame.bullets.add(this);
}
@Override
public void paint(Graphics g) {
if( ! this.live) {
tankFrame.bullets.remove(this);

@ -0,0 +1,54 @@
package com.msb.model;/**
* @Author bingor
* @Date 2022/10/9 17:42
* @Description: com.msb.model
* @Version: 1.0
*/
import com.msb.TankFrame;
import com.msb.base.ResourcesMgr;
import com.msb.model.abstracts.BaseExplode;
import java.awt.*;
/**
*@ClassName CycleExplode
*@Description TODO
*@Author bingor
*@Date 2022/10/9 17:42
*@Version 3.0
*/
public class CircleExplode extends BaseExplode {
private int x,y;
private TankFrame tankFrame;
private int step = 0;
public CircleExplode(int x, int y, TankFrame tankFrame) {
this.x = x;
this.y = y;
this.tankFrame = tankFrame;
}
@Override
public void paint(Graphics g) {
Color color = g.getColor();
g.setColor(Color.RED);
g.fillOval(x, y, 10*step, 10*step);
step++;
if(step >= 10) {
step = 0;
tankFrame.explodes.remove(this);
}
g.setColor(color);
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}

@ -2,6 +2,7 @@ package com.msb.model;
import com.msb.TankFrame;
import com.msb.base.ResourcesMgr;
import com.msb.model.abstracts.BaseExplode;
import java.awt.*;
@ -11,7 +12,7 @@ import java.awt.*;
* @Description: com.msb
* @Version: 1.0
*/
public class Explode {
public class Explode extends BaseExplode {
private int x,y;
private TankFrame tankFrame;
@ -26,6 +27,7 @@ public class Explode {
this.tankFrame = tankFrame;
}
@Override
public void paint(Graphics g) {
g.drawImage(ResourcesMgr.explodeImages[step++], x, y, null);

@ -6,6 +6,7 @@ import com.msb.base.ResourcesMgr;
import com.msb.enums.DirEnum;
import com.msb.enums.GroupEnum;
import com.msb.inter.FireStrategy;
import com.msb.model.abstracts.BaseTank;
import java.awt.*;
import java.util.Random;
@ -16,7 +17,7 @@ import java.util.Random;
* @Description: com.msb
* @Version: 1.0
*/
public class Tank {
public class Tank extends BaseTank {
private int x,y;
private DirEnum dir;
@ -59,6 +60,7 @@ public class Tank {
}
}
@Override
public void paint(Graphics g) {
if( ! live) {
@ -143,7 +145,8 @@ public class Tank {
this.live = false;
int eX = this.x + WIDTH/2 - Explode.WIDTH/2;
int eY = this.y + HEIGHT/2 - Explode.HEIGHT/2;
tankFrame.explodes.add(new Explode(eX, eY, tankFrame));
// tankFrame.explodes.add(new Explode(eX, eY, tankFrame));
tankFrame.explodes.add(tankFrame.gameFactory.createExplode(eX, eY, tankFrame));
}
public GroupEnum getGroup() {

@ -0,0 +1,19 @@
package com.msb.model.abstracts;/**
* @Author bingor
* @Date 2022/10/9 16:33
* @Description: com.msb.model.abstracts
* @Version: 1.0
*/
import com.msb.inter.Paintable;
/**
*@ClassName AbstractTank
*@Description TODO
*@Author bingor
*@Date 2022/10/9 16:33
*@Version 3.0
*/
public abstract class BaseBullet implements Paintable {
}

@ -0,0 +1,19 @@
package com.msb.model.abstracts;/**
* @Author bingor
* @Date 2022/10/9 16:33
* @Description: com.msb.model.abstracts
* @Version: 1.0
*/
import com.msb.inter.Paintable;
/**
*@ClassName AbstractTank
*@Description TODO
*@Author bingor
*@Date 2022/10/9 16:33
*@Version 3.0
*/
public abstract class BaseExplode implements Paintable {
}

@ -0,0 +1,19 @@
package com.msb.model.abstracts;/**
* @Author bingor
* @Date 2022/10/9 16:33
* @Description: com.msb.model.abstracts
* @Version: 1.0
*/
import com.msb.inter.Paintable;
/**
*@ClassName AbstractTank
*@Description TODO
*@Author bingor
*@Date 2022/10/9 16:33
*@Version 3.0
*/
public abstract class BaseTank implements Paintable {
}
Loading…
Cancel
Save