坦克大战(一期)-设计模式-装饰者模式应用到子弹上-有bug版

DesignPatterns
bingor 2 years ago
parent b4b9ac582b
commit 20198f66df

@ -12,7 +12,8 @@ public class TankDemo {
TankFrame tankFrame = new TankFrame();
//现实当中,虽然可以过按键来改变方块的坐标并且重新刷新画板来实现移动,但是敌方的坦克应该是自动在跑
while (true) {
Thread.sleep(50);
// Thread.sleep(50);
Thread.sleep(1000);
tankFrame.repaint();
}
}

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

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

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

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

@ -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) {

@ -46,4 +46,14 @@ public class Explode extends GameObject {
return y;
}
@Override
public int getWidth() {
return WIDTH;
}
@Override
public int getHeight() {
return HEIGHT;
}
}

@ -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;
}
}

@ -37,10 +37,12 @@ public class Wall extends GameObject {
g.setColor(color);
}
@Override
public int getWidth() {
return width;
}
@Override
public int getHeight() {
return height;
}

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

Loading…
Cancel
Save