From 20198f66dfb144d3f98d55edd0ac1fb01964c189 Mon Sep 17 00:00:00 2001 From: bingor Date: Thu, 13 Oct 2022 17:31:10 +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-=E8=A3=85=E9=A5=B0=E8=80=85=E6=A8=A1=E5=BC=8F?= =?UTF-8?q?=E5=BA=94=E7=94=A8=E5=88=B0=E5=AD=90=E5=BC=B9=E4=B8=8A-?= =?UTF-8?q?=E6=9C=89bug=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/msb/TankDemo.java | 3 +- .../java/com/msb/decorator/GODecoratory.java | 29 +++++++++++ .../java/com/msb/decorator/RectDecorator.java | 48 ++++++++++++++++++ .../java/com/msb/decorator/TailDecorator.java | 49 +++++++++++++++++++ .../msb/inter/impl/DefaultFireStrategy.java | 12 ++++- src/main/java/com/msb/model/Bullet.java | 10 ++++ src/main/java/com/msb/model/Explode.java | 10 ++++ src/main/java/com/msb/model/Tank.java | 10 ++++ src/main/java/com/msb/model/Wall.java | 2 + .../com/msb/model/abstracts/GameObject.java | 5 +- 10 files changed, 175 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/msb/decorator/GODecoratory.java create mode 100644 src/main/java/com/msb/decorator/RectDecorator.java create mode 100644 src/main/java/com/msb/decorator/TailDecorator.java diff --git a/src/main/java/com/msb/TankDemo.java b/src/main/java/com/msb/TankDemo.java index 257b79e..725b1a8 100644 --- a/src/main/java/com/msb/TankDemo.java +++ b/src/main/java/com/msb/TankDemo.java @@ -12,7 +12,8 @@ public class TankDemo { TankFrame tankFrame = new TankFrame(); //现实当中,虽然可以过按键来改变方块的坐标并且重新刷新画板来实现移动,但是敌方的坦克应该是自动在跑 while (true) { - Thread.sleep(50); +// Thread.sleep(50); + Thread.sleep(1000); tankFrame.repaint(); } } diff --git a/src/main/java/com/msb/decorator/GODecoratory.java b/src/main/java/com/msb/decorator/GODecoratory.java new file mode 100644 index 0000000..e80d633 --- /dev/null +++ b/src/main/java/com/msb/decorator/GODecoratory.java @@ -0,0 +1,29 @@ +package com.msb.decorator;/** + * @Author bingor + * @Date 2022/10/13 10:19 + * @Description: com.msb.decorator + * @Version: 1.0 + */ + +import com.msb.model.abstracts.GameObject; + +import java.awt.*; + +/** + *@ClassName GODecoratory + *@Description TODO + *@Author bingor + *@Date 2022/10/13 10:19 + *@Version 3.0 + */ +public abstract class GODecoratory extends GameObject { + + protected GameObject gameObject; + + public GODecoratory(GameObject gameObject) { + this.gameObject = gameObject; + } + + @Override + public abstract void paint(Graphics g); +} diff --git a/src/main/java/com/msb/decorator/RectDecorator.java b/src/main/java/com/msb/decorator/RectDecorator.java new file mode 100644 index 0000000..1a02426 --- /dev/null +++ b/src/main/java/com/msb/decorator/RectDecorator.java @@ -0,0 +1,48 @@ +package com.msb.decorator;/** + * @Author bingor + * @Date 2022/10/13 10:23 + * @Description: com.msb.decorator + * @Version: 1.0 + */ + +import com.msb.model.abstracts.GameObject; + +import java.awt.*; + +/** + *@ClassName RectDecorator + *@Description TODO + *@Author bingor + *@Date 2022/10/13 10:23 + *@Version 3.0 + */ +public class RectDecorator extends GODecoratory { + + public RectDecorator(GameObject gameObject) { + super(gameObject); + } + + @Override + public void paint(Graphics g) { + this.x = gameObject.x; + this.y = gameObject.y; + gameObject.paint(g); //原来的样式 + + //需要装饰的样式 + Color color = g.getColor(); + g.setColor(Color.WHITE); + g.drawRect(super.gameObject.x, super.gameObject.y, super.gameObject.getWidth()+2, super.gameObject.getHeight()+2); +// g.drawRect(this.x, this.y, getWidth()+2, getHeight()+2); + g.setColor(color); + } + + @Override + public int getWidth() { + return super.gameObject.getWidth(); + } + + @Override + public int getHeight() { + return super.gameObject.getHeight(); + } +} diff --git a/src/main/java/com/msb/decorator/TailDecorator.java b/src/main/java/com/msb/decorator/TailDecorator.java new file mode 100644 index 0000000..c7d3cd2 --- /dev/null +++ b/src/main/java/com/msb/decorator/TailDecorator.java @@ -0,0 +1,49 @@ +package com.msb.decorator;/** + * @Author bingor + * @Date 2022/10/13 10:23 + * @Description: com.msb.decorator + * @Version: 1.0 + */ + +import com.msb.model.abstracts.GameObject; + +import java.awt.*; + +/** + *@ClassName RectDecorator + *@Description TODO + *@Author bingor + *@Date 2022/10/13 10:23 + *@Version 3.0 + */ +public class TailDecorator extends GODecoratory { + + public TailDecorator(GameObject gameObject) { + super(gameObject); + } + + @Override + public void paint(Graphics g) { + this.x = gameObject.x; + this.y = gameObject.y; + gameObject.paint(g); //原来的样式 + + //需要装饰的样式 + Color color = g.getColor(); + g.setColor(Color.YELLOW); +// g.drawLine(super.gameObject.x, super.gameObject.y, super.gameObject.x+getWidth(), super.gameObject.y+getHeight()); +// g.drawLine(gameObject.x, gameObject.y, gameObject.x+getWidth(), gameObject.y+getHeight()); + g.drawLine(this.x, this.y, this.x+getWidth(), this.y+getHeight()); + g.setColor(color); + } + + @Override + public int getWidth() { + return super.gameObject.getWidth(); + } + + @Override + public int getHeight() { + return super.gameObject.getHeight(); + } +} diff --git a/src/main/java/com/msb/inter/impl/DefaultFireStrategy.java b/src/main/java/com/msb/inter/impl/DefaultFireStrategy.java index c48d25c..0dcc054 100644 --- a/src/main/java/com/msb/inter/impl/DefaultFireStrategy.java +++ b/src/main/java/com/msb/inter/impl/DefaultFireStrategy.java @@ -5,7 +5,10 @@ package com.msb.inter.impl;/** * @Version: 1.0 */ +import com.msb.decorator.RectDecorator; +import com.msb.decorator.TailDecorator; import com.msb.model.Bullet; +import com.msb.model.GameModel; import com.msb.model.Tank; import com.msb.inter.FireStrategy; @@ -22,6 +25,13 @@ public class DefaultFireStrategy implements FireStrategy { int bX = tank.getX() + Tank.WIDTH/2 - Bullet.WIDTH/2; int bY = tank.getY()+Tank.HEIGHT/2-Bullet.HEIGHT/2; // new Bullet(bX, bY, tank.getDir(), tank.getGroup(), tank.getGameModel()); - new Bullet(bX, bY, tank.getDir(), tank.getGroup()); +// new Bullet(bX, bY, tank.getDir(), tank.getGroup()); + GameModel.INSTANCE.add( + new RectDecorator( + new TailDecorator( + new Bullet(bX, bY, tank.getDir(), tank.getGroup()) + ) + ) + ); } } diff --git a/src/main/java/com/msb/model/Bullet.java b/src/main/java/com/msb/model/Bullet.java index 5c3960d..9c5a55b 100644 --- a/src/main/java/com/msb/model/Bullet.java +++ b/src/main/java/com/msb/model/Bullet.java @@ -63,6 +63,16 @@ public class Bullet extends GameObject { moving(); } + @Override + public int getWidth() { + return WIDTH; + } + + @Override + public int getHeight() { + return HEIGHT; + } + public void moving() { switch (dir) { diff --git a/src/main/java/com/msb/model/Explode.java b/src/main/java/com/msb/model/Explode.java index df8e5c3..7d5e9db 100644 --- a/src/main/java/com/msb/model/Explode.java +++ b/src/main/java/com/msb/model/Explode.java @@ -46,4 +46,14 @@ public class Explode extends GameObject { return y; } + @Override + public int getWidth() { + return WIDTH; + } + + @Override + public int getHeight() { + return HEIGHT; + } + } diff --git a/src/main/java/com/msb/model/Tank.java b/src/main/java/com/msb/model/Tank.java index 0670b28..1a27933 100644 --- a/src/main/java/com/msb/model/Tank.java +++ b/src/main/java/com/msb/model/Tank.java @@ -171,4 +171,14 @@ public class Tank extends GameObject { this.x = this.oldX; this.y = this.oldY; } + + @Override + public int getWidth() { + return WIDTH; + } + + @Override + public int getHeight() { + return HEIGHT; + } } diff --git a/src/main/java/com/msb/model/Wall.java b/src/main/java/com/msb/model/Wall.java index e08eb3c..3b4aab1 100644 --- a/src/main/java/com/msb/model/Wall.java +++ b/src/main/java/com/msb/model/Wall.java @@ -37,10 +37,12 @@ public class Wall extends GameObject { g.setColor(color); } + @Override public int getWidth() { return width; } + @Override public int getHeight() { return height; } diff --git a/src/main/java/com/msb/model/abstracts/GameObject.java b/src/main/java/com/msb/model/abstracts/GameObject.java index c721f28..a137895 100644 --- a/src/main/java/com/msb/model/abstracts/GameObject.java +++ b/src/main/java/com/msb/model/abstracts/GameObject.java @@ -15,6 +15,9 @@ import java.awt.*; *@Version 3.0 */ public abstract class GameObject { - protected int x,y; +// protected int x,y; + public int x,y; public abstract void paint(Graphics g); + public abstract int getWidth(); + public abstract int getHeight(); }