tank explode factory

master
Jian Hu 3 years ago
parent 46b8868738
commit a7d85de1d5

@ -2,7 +2,6 @@ package com.demo.tank.course6;
import com.demo.tank.enums.Direction; import com.demo.tank.enums.Direction;
import com.demo.tank.enums.Group; import com.demo.tank.enums.Group;
import com.demo.tank.util.PropertyManager;
import com.demo.tank.util.ResourceManager; import com.demo.tank.util.ResourceManager;
import java.awt.*; import java.awt.*;
@ -36,9 +35,9 @@ public class Tank {
rect.height = Tank.HEIGHT; rect.height = Tank.HEIGHT;
if(this.group == Group.GOOD) { if(this.group == Group.GOOD) {
String className = PropertyManager.getString("good.tank.fire.strategy"); // String className = PropertyManager.getString("good.tank.fire.strategy");
try { try {
fireStrategy = (FireStrategy) Class.forName(className).newInstance(); fireStrategy = (FireStrategy) Class.forName("com.demo.tank.course6.FourDirectionFireStrategy").newInstance();
System.out.println(fireStrategy); System.out.println(fireStrategy);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
e.printStackTrace(); e.printStackTrace();

@ -1,12 +1,13 @@
package com.demo.tank.course7; package com.demo.tank.course7;
import com.demo.tank.course7.abstractFactory.BaseBullet;
import com.demo.tank.enums.Direction; import com.demo.tank.enums.Direction;
import com.demo.tank.enums.Group; import com.demo.tank.enums.Group;
import com.demo.tank.util.ResourceManager; import com.demo.tank.util.ResourceManager;
import java.awt.*; import java.awt.*;
public class Bullet { public class Bullet extends BaseBullet {
private int x, y; private int x, y;
private Direction direction; private Direction direction;
private static final int SPEED = 10; private static final int SPEED = 10;
@ -14,10 +15,10 @@ public class Bullet {
public static final int HEIGHT = ResourceManager.bulletD.getHeight(); public static final int HEIGHT = ResourceManager.bulletD.getHeight();
private boolean live = true; private boolean live = true;
private Group group = Group.BAD; private Group group = Group.BAD;
private TankFrameV6 tf; private TankFrameV7 tf;
Rectangle rect = new Rectangle(); Rectangle rect = new Rectangle();
public Bullet(int x, int y, Direction direction, Group group, TankFrameV6 tf) { public Bullet(int x, int y, Direction direction, Group group, TankFrameV7 tf) {
this.x = x; this.x = x;
this.y = y; this.y = y;
this.direction = direction; this.direction = direction;
@ -31,6 +32,7 @@ public class Bullet {
tf.bullets.add(this); tf.bullets.add(this);
} }
@Override
public void paint(Graphics g){ public void paint(Graphics g){
if(!live){ if(!live){
tf.bullets.remove(this); tf.bullets.remove(this);
@ -65,7 +67,7 @@ public class Bullet {
default: default:
break; break;
} }
if(x < 0 || y < 0 || x > TankFrameV6.GAME_WIDTH || y > TankFrameV6.GAME_HEIGHT){ if(x < 0 || y < 0 || x > TankFrameV7.GAME_WIDTH || y > TankFrameV7.GAME_HEIGHT){
live = false; live = false;
} }
rect.x = this.x; rect.x = this.x;
@ -82,7 +84,7 @@ public class Bullet {
//爆炸 //爆炸
int ex = tank.getX() + Tank.WIDTH/2 - Explode.WIDTH/2; int ex = tank.getX() + Tank.WIDTH/2 - Explode.WIDTH/2;
int ey = tank.getY() + Tank.HEIGHT/2 - Explode.HEIGHT/2; int ey = tank.getY() + Tank.HEIGHT/2 - Explode.HEIGHT/2;
tf.explodes.add(new Explode(ex, ey , tf)); tf.explodes.add(tf.gameFactory.createExplode(ex, ey , tf));
} }
} }

@ -1,25 +1,27 @@
package com.demo.tank.course7; package com.demo.tank.course7;
import com.demo.tank.course7.abstractFactory.BaseExplode;
import com.demo.tank.util.Audio; import com.demo.tank.util.Audio;
import com.demo.tank.util.ResourceManager; import com.demo.tank.util.ResourceManager;
import java.awt.*; import java.awt.*;
public class Explode { public class Explode extends BaseExplode {
private int x, y; private int x, y;
public static final int WIDTH = ResourceManager.explodes[0].getWidth(); public static final int WIDTH = ResourceManager.explodes[0].getWidth();
public static final int HEIGHT = ResourceManager.explodes[0].getHeight(); public static final int HEIGHT = ResourceManager.explodes[0].getHeight();
private TankFrameV6 tf; private TankFrameV7 tf;
private int step = 0; private int step = 0;
public Explode(int x, int y, TankFrameV6 tf) { public Explode(int x, int y, TankFrameV7 tf) {
this.x = x; this.x = x;
this.y = y; this.y = y;
this.tf = tf; this.tf = tf;
new Thread(() -> new Audio("audio/explode.wav").play()).start(); new Thread(() -> new Audio("audio/explode.wav").play()).start();
} }
@Override
public void paint(Graphics g){ public void paint(Graphics g){
g.drawImage(ResourceManager.explodes[step++], x, y, null); g.drawImage(ResourceManager.explodes[step++], x, y, null);
if(step >= ResourceManager.explodes.length){ if(step >= ResourceManager.explodes.length){

@ -6,9 +6,9 @@ import com.demo.tank.util.PropertyManager;
import java.io.IOException; import java.io.IOException;
public class MainV6 { public class MainV7 {
public static void main(String[] args) throws InterruptedException, IOException { public static void main(String[] args) throws InterruptedException, IOException {
TankFrameV6 tf = new TankFrameV6(); TankFrameV7 tf = new TankFrameV7();
int enemyTankNum = PropertyManager.getInt("enemy.tank.number"); int enemyTankNum = PropertyManager.getInt("enemy.tank.number");
for(int i = 0; i < enemyTankNum; i++){ for(int i = 0; i < enemyTankNum; i++){
tf.enemyTanks.add(new Tank(50 + i*80, 200, Direction.DOWN, Group.BAD, tf)); tf.enemyTanks.add(new Tank(50 + i*80, 200, Direction.DOWN, Group.BAD, tf));

@ -1,21 +1,21 @@
package com.demo.tank.course7; package com.demo.tank.course7;
import com.demo.tank.course7.abstractFactory.BaseTank;
import com.demo.tank.enums.Direction; import com.demo.tank.enums.Direction;
import com.demo.tank.enums.Group; import com.demo.tank.enums.Group;
import com.demo.tank.util.PropertyManager;
import com.demo.tank.util.ResourceManager; import com.demo.tank.util.ResourceManager;
import java.awt.*; import java.awt.*;
import java.util.Random; import java.util.Random;
public class Tank { public class Tank extends BaseTank {
private int x,y; private int x,y;
private Direction dir; private Direction dir;
private static final int SPEED = 8; private static final int SPEED = 8;
private boolean moving = true; private boolean moving = true;
private boolean living = true; private boolean living = true;
private Group group = Group.BAD; private Group group = Group.BAD;
TankFrameV6 tankFrame = null; TankFrameV7 tankFrame = null;
public static final int WIDTH = ResourceManager.tankD.getWidth(); public static final int WIDTH = ResourceManager.tankD.getWidth();
public static final int HEIGHT = ResourceManager.tankD.getHeight(); public static final int HEIGHT = ResourceManager.tankD.getHeight();
private Random random = new Random(); private Random random = new Random();
@ -23,7 +23,7 @@ public class Tank {
FireStrategy fireStrategy; FireStrategy fireStrategy;
public Tank(int x, int y, Direction dir, Group group, TankFrameV6 tankFrame) { public Tank(int x, int y, Direction dir, Group group, TankFrameV7 tankFrame) {
this.x = x; this.x = x;
this.y = y; this.y = y;
this.dir = dir; this.dir = dir;
@ -36,9 +36,9 @@ public class Tank {
rect.height = Tank.HEIGHT; rect.height = Tank.HEIGHT;
if(this.group == Group.GOOD) { if(this.group == Group.GOOD) {
String className = PropertyManager.getString("good.tank.fire.strategy"); // String className = PropertyManager.getString("good.tank.fire.strategy");
try { try {
fireStrategy = (FireStrategy) Class.forName(className).newInstance(); fireStrategy = (FireStrategy) Class.forName("com.demo.tank.course7.FourDirectionFireStrategy").newInstance();
System.out.println(fireStrategy); System.out.println(fireStrategy);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
@ -50,6 +50,7 @@ public class Tank {
}else if(this.group == Group.BAD) fireStrategy = new DefaultFireStrategy(); }else if(this.group == Group.BAD) fireStrategy = new DefaultFireStrategy();
} }
@Override
public void paint(Graphics g) { public void paint(Graphics g) {
if(!living) tankFrame.enemyTanks.remove(this); if(!living) tankFrame.enemyTanks.remove(this);
//根据方向绘制坦克 //根据方向绘制坦克
@ -103,9 +104,9 @@ public class Tank {
private void boundsCheck() { private void boundsCheck() {
if(x < 0) x = 0; if(x < 0) x = 0;
if(x > TankFrameV6.GAME_WIDTH - Tank.WIDTH) x = TankFrameV6.GAME_WIDTH - Tank.WIDTH; if(x > TankFrameV7.GAME_WIDTH - Tank.WIDTH) x = TankFrameV7.GAME_WIDTH - Tank.WIDTH;
if(y < 30) y = 30; //算上菜单条 if(y < 30) y = 30; //算上菜单条
if(y > TankFrameV6.GAME_HEIGHT - Tank.HEIGHT) y = TankFrameV6.GAME_HEIGHT - Tank.HEIGHT; if(y > TankFrameV7.GAME_HEIGHT - Tank.HEIGHT) y = TankFrameV7.GAME_HEIGHT - Tank.HEIGHT;
} }
private void randomDirection() { private void randomDirection() {
@ -168,11 +169,11 @@ public class Tank {
this.group = group; this.group = group;
} }
public TankFrameV6 getTankFrame() { public TankFrameV7 getTankFrame() {
return tankFrame; return tankFrame;
} }
public void setTankFrame(TankFrameV6 tankFrame) { public void setTankFrame(TankFrameV7 tankFrame) {
this.tankFrame = tankFrame; this.tankFrame = tankFrame;
} }

@ -1,5 +1,8 @@
package com.demo.tank.course7; package com.demo.tank.course7;
import com.demo.tank.course7.abstractFactory.BaseExplode;
import com.demo.tank.course7.abstractFactory.DefaultFactory;
import com.demo.tank.course7.abstractFactory.GameFactory;
import com.demo.tank.enums.Direction; import com.demo.tank.enums.Direction;
import com.demo.tank.enums.Group; import com.demo.tank.enums.Group;
@ -11,7 +14,7 @@ import java.awt.event.WindowEvent;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class TankFrameV6 extends Frame { public class TankFrameV7 extends Frame {
public static final int GAME_WIDTH = 1080; public static final int GAME_WIDTH = 1080;
public static final int GAME_HEIGHT = 800; public static final int GAME_HEIGHT = 800;
Image image = null; Image image = null;
@ -21,9 +24,11 @@ public class TankFrameV6 extends Frame {
List<Bullet> bullets = new ArrayList(); List<Bullet> bullets = new ArrayList();
List<Tank> enemyTanks = new ArrayList<>(); List<Tank> enemyTanks = new ArrayList<>();
List<Explode> explodes = new ArrayList<>(); List<BaseExplode> explodes = new ArrayList<>();
public TankFrameV6(){ GameFactory gameFactory = new DefaultFactory();
public TankFrameV7(){
setVisible(true); setVisible(true);
setBounds(400, 100 , GAME_WIDTH, GAME_HEIGHT); setBounds(400, 100 , GAME_WIDTH, GAME_HEIGHT);
setResizable(false); setResizable(false);

@ -3,5 +3,3 @@ tank.speed=5
bullet.speed=5 bullet.speed=5
game.width=1080 game.width=1080
game.height=720 game.height=720
good.tank.fire.strategy=com.demo.tank.course6.FourDirectionFireStrategy
bad.tank.fire.strategy=com.demo.tank.course6.DefaultFireStrategy
Loading…
Cancel
Save