From 7764e0b07e618baa5f2382d4f25642c9688ed88b Mon Sep 17 00:00:00 2001 From: bingor Date: Sun, 9 Oct 2022 18:02:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9D=A6=E5=85=8B=E5=A4=A7=E6=88=98=EF=BC=88?= =?UTF-8?q?=E4=B8=80=E6=9C=9F=EF=BC=89-=E8=AE=BE=E8=AE=A1=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F-=E5=B0=86=E6=8A=BD=E8=B1=A1=E5=B7=A5=E5=8E=82?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E8=BF=90=E7=94=A8=E5=88=B0=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/msb/TankFrame.java | 7 ++- .../java/com/msb/factorys/CircleFactory.java | 38 +++++++++++++ .../java/com/msb/factorys/DefaultFactory.java | 37 +++++++++++++ .../msb/factorys/abstracts/GameFactory.java | 24 +++++++++ src/main/java/com/msb/inter/Paintable.java | 13 +++++ src/main/java/com/msb/model/Bullet.java | 4 +- .../java/com/msb/model/CircleExplode.java | 54 +++++++++++++++++++ src/main/java/com/msb/model/Explode.java | 4 +- src/main/java/com/msb/model/Tank.java | 7 ++- .../com/msb/model/abstracts/BaseBullet.java | 19 +++++++ .../com/msb/model/abstracts/BaseExplode.java | 19 +++++++ .../com/msb/model/abstracts/BaseTank.java | 19 +++++++ 12 files changed, 240 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/msb/factorys/CircleFactory.java create mode 100644 src/main/java/com/msb/factorys/DefaultFactory.java create mode 100644 src/main/java/com/msb/factorys/abstracts/GameFactory.java create mode 100644 src/main/java/com/msb/inter/Paintable.java create mode 100644 src/main/java/com/msb/model/CircleExplode.java create mode 100644 src/main/java/com/msb/model/abstracts/BaseBullet.java create mode 100644 src/main/java/com/msb/model/abstracts/BaseExplode.java create mode 100644 src/main/java/com/msb/model/abstracts/BaseTank.java 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 { + +}