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(); }