Compare commits

..

No commits in common. '33989e28eeb837f14d4f72d7ded40e47da696feb' and '70f9cb68011ec698f23df657a626af64c303b2ce' have entirely different histories.

@ -28,8 +28,6 @@ public class Bullet {
rect.y = this.y; rect.y = this.y;
rect.width = WIDTH; rect.width = WIDTH;
rect.height = HEIGHT; rect.height = HEIGHT;
tf.bullets.add(this);
} }
public Group getGroup() { public Group getGroup() {

@ -1,14 +0,0 @@
package com.example.tankbattle;
public class DefaultFireStrategy implements FireStrategy {
@Override
public void fire(Tank t) {
int bX = t.x + Tank.WIDTH/2 - Bullet.WIDTH/2;
int bY = t.y + Tank.HEIGHT/2 - Bullet.HEIGHT/2;
new Bullet(bX, bY, t.dir, t.group, t.tf);
if(t.group == Group.GOOD) new Thread(()->new Audio("audio/tank_fire.wav").play()).start();
}
}

@ -1,5 +0,0 @@
package com.example.tankbattle;
public interface FireStrategy {
void fire(Tank Tank);
}

@ -1,17 +0,0 @@
package com.example.tankbattle;
public class FourDirFireStrategy implements FireStrategy {
@Override
public void fire(Tank t) {
int bX = t.x + Tank.WIDTH/2 - Bullet.WIDTH/2;
int bY = t.y + Tank.HEIGHT/2 - Bullet.HEIGHT/2;
Dir[] dirs = Dir.values();
for(Dir dir : dirs) {
new Bullet(bX, bY, dir, t.group, t.tf);
}
if(t.group == Group.GOOD) new Thread(()->new Audio("audio/tank_fire.wav").play()).start();
}
}

@ -4,10 +4,8 @@ public class Main {
public static void main(String[] args) throws InterruptedException { public static void main(String[] args) throws InterruptedException {
TankFrame tf = new TankFrame(); TankFrame tf = new TankFrame();
int initTankCount = Integer.valueOf(PropertyMgr.get("initTankCount"));
// 初始化敌方坦克 // 初始化敌方坦克
for (int i = 0; i < initTankCount; i++) { for (int i = 0; i < 5; i++) {
tf.tanks.add(new Tank(50 + i * 80, 200, Dir.DOWN, Group.BAD, tf)); tf.tanks.add(new Tank(50 + i * 80, 200, Dir.DOWN, Group.BAD, tf));
} }

@ -1,29 +0,0 @@
package com.example.tankbattle;
import java.io.IOException;
import java.util.Properties;
public class PropertyMgr {
private static Properties properties = new Properties();
static {
try {
properties.load(PropertyMgr.class.getClassLoader().getResourceAsStream("config.properties"));
} catch (IOException exception) {
exception.printStackTrace();
}
}
public static String get(String key) {
if (properties == null) {
return null;
}
return properties.getProperty(key);
}
//int getInt(key)
//getString(key)
public static void main(String[] args) {
System.out.println(PropertyMgr.get("initTankCount"));
}
}

@ -5,22 +5,22 @@ import java.io.IOException;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
public class ResourceMgr { public class ResourceMgr {
public static BufferedImage goodTankL, goodTankU, goodTankR, goodTankD; public static BufferedImage GoodTankL, GoodTankU, GoodTankR, GoodTankD;
public static BufferedImage badTankL, badTankU, badTankR, badTankD; public static BufferedImage BadTankL, BadTankU, BadTankR, BadTankD;
public static BufferedImage bulletL, bulletU,bulletR, bulletD; public static BufferedImage bulletL, bulletU,bulletR, bulletD;
public static BufferedImage[] explodes = new BufferedImage[16]; public static BufferedImage[] explodes = new BufferedImage[16];
static { static {
try { try {
goodTankU = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/GoodTank1.png")); GoodTankU = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/GoodTank1.png"));
goodTankL = ImageUtil.rotateImage(goodTankU, -90); GoodTankL = ImageUtil.rotateImage(GoodTankU, -90);
goodTankR = ImageUtil.rotateImage(goodTankU, 90); GoodTankR = ImageUtil.rotateImage(GoodTankU, 90);
goodTankD = ImageUtil.rotateImage(goodTankU, 180); GoodTankD = ImageUtil.rotateImage(GoodTankU, 180);
badTankU = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/BadTank1.png")); BadTankU = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/BadTank1.png"));
badTankL = ImageUtil.rotateImage(badTankU, -90); BadTankL = ImageUtil.rotateImage(BadTankU, -90);
badTankR = ImageUtil.rotateImage(badTankU, 90); BadTankR = ImageUtil.rotateImage(BadTankU, 90);
badTankD = ImageUtil.rotateImage(badTankU, 180); BadTankD = ImageUtil.rotateImage(BadTankU, 180);
bulletU = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/bulletU.png")); bulletU = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/bulletU.png"));
bulletL = ImageUtil.rotateImage(bulletU, -90); bulletL = ImageUtil.rotateImage(bulletU, -90);

@ -4,28 +4,26 @@ import java.awt.*;
import java.util.Random; import java.util.Random;
public class Tank { public class Tank {
private static final int SPEED = 2; private static final int SPEED = 1;
public static int WIDTH = ResourceMgr.goodTankU.getWidth(); public static int WIDTH = ResourceMgr.GoodTankD.getWidth();
public static int HEIGHT = ResourceMgr.goodTankU.getHeight(); public static int HEIGHT = ResourceMgr.GoodTankD.getHeight();
Rectangle rect = new Rectangle(); Rectangle rect = new Rectangle();
private Random random = new Random(); private Random random = new Random();
public int x,y; private int x,y;
public Dir dir = Dir.DOWN; private Dir dir = Dir.DOWN;
private boolean moving = true; private boolean moving = true;
public TankFrame tf = null; private TankFrame tf = null;
private boolean living = true; private boolean living = true;
public Group group = Group.BAD; private Group group = Group.BAD;
FireStrategy fs;
public Tank(int x, int y, Dir dir, Group group, TankFrame tf) { public Tank(int x, int y, Dir dir, Group group, TankFrame tf) {
super(); super();
@ -39,23 +37,12 @@ public class Tank {
rect.y = this.y; rect.y = this.y;
rect.width = WIDTH; rect.width = WIDTH;
rect.height = HEIGHT; rect.height = HEIGHT;
if(group == Group.GOOD) {
String goodFSName = PropertyMgr.get("goodFS");
try {
fs = (FireStrategy)Class.forName(goodFSName).getDeclaredConstructor().newInstance();
} catch (Exception e) {
e.printStackTrace();
}
} else {
fs = new DefaultFireStrategy();
}
} }
public void fire(){ public void fire(){
fs.fire(this); int bX = this.x + Tank.WIDTH / 2 - Bullet.WIDTH / 2;
int bY = this.y + Tank.HEIGHT / 2 - Bullet.HEIGHT / 2;
tf.bullets.add(new Bullet(bX, bY, this.dir, this.group, this.tf));
} }
public Dir getDir() { public Dir getDir() {
@ -136,16 +123,16 @@ public class Tank {
if (!living) tf.tanks.remove(this); if (!living) tf.tanks.remove(this);
switch (dir) { switch (dir) {
case LEFT: case LEFT:
g.drawImage(this.group == Group.GOOD ? ResourceMgr.goodTankL : ResourceMgr.badTankL, x, y, null); g.drawImage(this.group == Group.GOOD ? ResourceMgr.GoodTankL : ResourceMgr.BadTankL, x, y, null);
break; break;
case UP: case UP:
g.drawImage(this.group == Group.GOOD ? ResourceMgr.goodTankU : ResourceMgr.badTankU, x, y, null); g.drawImage(this.group == Group.GOOD ? ResourceMgr.GoodTankU : ResourceMgr.BadTankU, x, y, null);
break; break;
case RIGHT: case RIGHT:
g.drawImage(this.group == Group.GOOD ? ResourceMgr.goodTankR : ResourceMgr.badTankR, x, y, null); g.drawImage(this.group == Group.GOOD ? ResourceMgr.GoodTankR : ResourceMgr.BadTankR, x, y, null);
break; break;
case DOWN: case DOWN:
g.drawImage(this.group == Group.GOOD ? ResourceMgr.goodTankD : ResourceMgr.badTankD, x, y, null); g.drawImage(this.group == Group.GOOD ? ResourceMgr.GoodTankD : ResourceMgr.BadTankD, x, y, null);
break; break;
default: default:
break; break;

@ -20,8 +20,7 @@ public class TankFrame extends Frame {
List<Explode> explodes = new ArrayList<>(); List<Explode> explodes = new ArrayList<>();
static final int GAME_WIDTH = Integer.valueOf(PropertyMgr.get("gameWidth")); static final int GAME_WIDTH = 1080, GAME_HEIGHT = 960;
static final int GAME_HEIGHT = Integer.valueOf(PropertyMgr.get("gameHeight"));
public TankFrame() { public TankFrame() {
setSize(GAME_WIDTH, GAME_HEIGHT); setSize(GAME_WIDTH, GAME_HEIGHT);

@ -1,9 +0,0 @@
# tanks count at initialization
initTankCount=10
tankSpeed=5
bulletSpeed=10
gameWidth=1080
gameHeight=720
#fireStrategy
goodFS=com.mashibing.tank.FourDirFireStrategy
badFS=com.mashibing.tank.DefaultFireStrategy
Loading…
Cancel
Save