坦克大战练习设计模式
利用装饰者模式可以有效的较少类的数量,提高同一代码的利用率。其核心将被装饰的类聚合在装饰类中,通过调用装饰类,来达到对类装饰的目的
类图:
原有类父类
package com.wwj.tank;
import java.awt.*;
/**
* 所有游戏物体的父类
*/
public abstract class GameObject {
int x,y;
public abstract void paint(Graphics g);
public abstract int getWidth();
public abstract int getHeight();
}
package com.wwj.tank;
import java.awt.*;
public class Bullert extends GameObject{
public static int WIDTH = ResourceMgr.INSTANCE.getBulletD().getWidth();
public static int HEIGHT = ResourceMgr.INSTANCE.getBulletD().getHeight();
Rectangle rect = new Rectangle();
private Dir dir;
private Boolean living = true;
private int speed = 10;
private Group group;
private boolean canDie = true;
public Bullert(int x, int y,Dir dir,Group group) {
this.x = x;
this.y = y;
this.dir = dir;
this.group = group;
rect.x = x;
rect.y = y;
rect.width = WIDTH;
rect.height = HEIGHT;
}
@Override
public void paint(Graphics g) {
if(dir == Dir.UP){g.drawImage(ResourceMgr.INSTANCE.getBulletU(),x,y,null);}
if(dir == Dir.RIGHT){g.drawImage(ResourceMgr.INSTANCE.getBulletR(),x,y,null);}
if(dir == Dir.DOWN){g.drawImage(ResourceMgr.INSTANCE.getBulletD(),x,y,null);}
if(dir == Dir.LEFT){g.drawImage(ResourceMgr.INSTANCE.getBulletL(),x,y,null);}
move();
if(!living){
GameMode.getInstance().remove(this);
}
}
@Override
public int getWidth() {
return WIDTH;
}
@Override
public int getHeight() {
return HEIGHT;
}
private void move() {
if(dir == Dir.UP){y -= speed;}
if(dir == Dir.RIGHT){x += speed;}
if(dir == Dir.DOWN){y += speed;}
if(dir == Dir.LEFT){x -= speed;}
rect.x = this.x;
rect.y = this.y;
if(x<0 || y<0 || x>GameMode.GAMEWIDTF || y>GameMode.GAMEHEIGHT){
this.living = false;
}
}
//子弹消亡
public void die() {
if(canDie){
this.living = false;
}
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public boolean isCanDie() {
return canDie;
}
public void setCanDie(boolean canDie) {
this.canDie = canDie;
}
public static int getWIDTH() {
return WIDTH;
}
public static void setWIDTH(int WIDTH) {
Bullert.WIDTH = WIDTH;
}
public static int getHEIGHT() {
return HEIGHT;
}
public static void setHEIGHT(int HEIGHT) {
Bullert.HEIGHT = HEIGHT;
}
public Rectangle getRect() {
return rect;
}
public void setRect(Rectangle rect) {
this.rect = rect;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Dir getDir() {
return dir;
}
public void setDir(Dir dir) {
this.dir = dir;
}
public Boolean getLiving() {
return living;
}
public void setLiving(Boolean living) {
this.living = living;
}
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
}
package com.wwj.tank;
import java.awt.*;
public abstract class GODecorator extends GameObject{
GameObject go;
public GODecorator(GameObject go){
this.go = go;
}
@Override
public void paint(Graphics g) {
super.x = go.x;
super.y =go.y;
go.paint(g);
}
}
package com.wwj.tank;
import java.awt.*;
public class TailDecorator extends GODecorator{
public TailDecorator(GameObject go) {
super(go);
}
@Override
public void paint(Graphics g) {
super.paint(g);
Color c = g.getColor();
g.setColor(Color.YELLOW);
g.drawRect(this.x,this.y,go.getWidth(),go.getHeight());
g.setColor(c);
}
@Override
public int getWidth() {
return 0;
}
@Override
public int getHeight() {
return 0;
}
}
package com.wwj.tank;
public class FireGeneral implements Fire {
@Override
public void fire(Tank tank) {
int bx = tank.getX() + Tank.WIDTH/2 - Bullert.WIDTH/2;
int by = tank.getY() + Tank.WIDTH/2 - Bullert.WIDTH/2;
Bullert bullert = new Bullert(bx, by, tank.getDir(), tank.getGroup());
bullert.setSpeed(10);
TailDecorator tailDecorator = new TailDecorator(bullert);
GameMode.getInstance().add(tailDecorator);
}
}