diff --git a/src/main/java/com/msb/TankFrame.java b/src/main/java/com/msb/TankFrame.java index 948ed68..4289eb1 100644 --- a/src/main/java/com/msb/TankFrame.java +++ b/src/main/java/com/msb/TankFrame.java @@ -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 bullets = new ArrayList<>(); public List tanks = new ArrayList<>(); //敌方坦克 - public List explodes = new ArrayList<>(); //爆炸效果 + public List explodes = new ArrayList<>(); //爆炸效果 public static final int GAME_WIDTH = 1000; public static final int GAME_HEIGHT = 800; + public GameFactory gameFactory = new CircleFactory(); public TankFrame() { diff --git a/src/main/java/com/msb/factorys/CircleFactory.java b/src/main/java/com/msb/factorys/CircleFactory.java new file mode 100644 index 0000000..891e8f6 --- /dev/null +++ b/src/main/java/com/msb/factorys/CircleFactory.java @@ -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); + } +} diff --git a/src/main/java/com/msb/factorys/DefaultFactory.java b/src/main/java/com/msb/factorys/DefaultFactory.java new file mode 100644 index 0000000..ef219a9 --- /dev/null +++ b/src/main/java/com/msb/factorys/DefaultFactory.java @@ -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); + } +} diff --git a/src/main/java/com/msb/factorys/abstracts/GameFactory.java b/src/main/java/com/msb/factorys/abstracts/GameFactory.java new file mode 100644 index 0000000..b6b3a1c --- /dev/null +++ b/src/main/java/com/msb/factorys/abstracts/GameFactory.java @@ -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); +} diff --git a/src/main/java/com/msb/inter/Paintable.java b/src/main/java/com/msb/inter/Paintable.java new file mode 100644 index 0000000..fe9905a --- /dev/null +++ b/src/main/java/com/msb/inter/Paintable.java @@ -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); +} diff --git a/src/main/java/com/msb/model/Bullet.java b/src/main/java/com/msb/model/Bullet.java index 093720f..920982a 100644 --- a/src/main/java/com/msb/model/Bullet.java +++ b/src/main/java/com/msb/model/Bullet.java @@ -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); diff --git a/src/main/java/com/msb/model/CircleExplode.java b/src/main/java/com/msb/model/CircleExplode.java new file mode 100644 index 0000000..0dc3b59 --- /dev/null +++ b/src/main/java/com/msb/model/CircleExplode.java @@ -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; + } +} diff --git a/src/main/java/com/msb/model/Explode.java b/src/main/java/com/msb/model/Explode.java index 3000eeb..12da812 100644 --- a/src/main/java/com/msb/model/Explode.java +++ b/src/main/java/com/msb/model/Explode.java @@ -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); diff --git a/src/main/java/com/msb/model/Tank.java b/src/main/java/com/msb/model/Tank.java index c732b50..c0a1d32 100644 --- a/src/main/java/com/msb/model/Tank.java +++ b/src/main/java/com/msb/model/Tank.java @@ -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() { diff --git a/src/main/java/com/msb/model/abstracts/BaseBullet.java b/src/main/java/com/msb/model/abstracts/BaseBullet.java new file mode 100644 index 0000000..7e90474 --- /dev/null +++ b/src/main/java/com/msb/model/abstracts/BaseBullet.java @@ -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 { + +} diff --git a/src/main/java/com/msb/model/abstracts/BaseExplode.java b/src/main/java/com/msb/model/abstracts/BaseExplode.java new file mode 100644 index 0000000..f6377c5 --- /dev/null +++ b/src/main/java/com/msb/model/abstracts/BaseExplode.java @@ -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 { + +} diff --git a/src/main/java/com/msb/model/abstracts/BaseTank.java b/src/main/java/com/msb/model/abstracts/BaseTank.java new file mode 100644 index 0000000..beaf164 --- /dev/null +++ b/src/main/java/com/msb/model/abstracts/BaseTank.java @@ -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 { + +}