diff --git a/docs/坦克大战笔记.docx b/docs/坦克大战笔记.docx index bd38f63..8435f34 100644 Binary files a/docs/坦克大战笔记.docx and b/docs/坦克大战笔记.docx differ diff --git a/src/com/demo/tank/course3/Bullet.java b/src/com/demo/tank/course3/Bullet.java index e3b46bf..f8de131 100644 --- a/src/com/demo/tank/course3/Bullet.java +++ b/src/com/demo/tank/course3/Bullet.java @@ -9,12 +9,14 @@ public class Bullet { public static final int WIDTH = ResourceManager.bulletD.getWidth(); public static final int HEIGHT = ResourceManager.bulletD.getHeight(); private boolean live = true; + private Group group = Group.BAD; private TankFrameV3 tf; - public Bullet(int x, int y, Direction direction, TankFrameV3 tf) { + public Bullet(int x, int y, Direction direction, Group group, TankFrameV3 tf) { this.x = x; this.y = y; this.direction = direction; + this.group = group; this.tf = tf; } @@ -59,6 +61,8 @@ public class Bullet { //检测是否跟坦克碰撞 public void collideWith(Tank tank){ + //关闭队友伤害 + if(this.group == tank.getGroup()) return; Rectangle bulletRect = new Rectangle(x, y, WIDTH, HEIGHT); Rectangle tankRect = new Rectangle(tank.getX(), tank.getY(), Tank.WIDTH, Tank.HEIGHT); if(bulletRect.intersects(tankRect)){ @@ -102,4 +106,12 @@ public class Bullet { public void setLive(boolean live) { this.live = live; } + + public Group getGroup() { + return group; + } + + public void setGroup(Group group) { + this.group = group; + } } diff --git a/src/com/demo/tank/course3/Explode.java b/src/com/demo/tank/course3/Explode.java new file mode 100644 index 0000000..af3c32f --- /dev/null +++ b/src/com/demo/tank/course3/Explode.java @@ -0,0 +1,43 @@ +package com.demo.tank.course3; + +import java.awt.*; + +public class Explode { + private int x, y; + public static final int WIDTH = ResourceManager.explodes[0].getWidth(); + public static final int HEIGHT = ResourceManager.explodes[0].getHeight(); + private boolean living = true; + private TankFrameV3 tf; + + private int step = 0; + + public Explode(int x, int y, TankFrameV3 tf) { + this.x = x; + this.y = y; + this.tf = tf; + } + + public void paint(Graphics g){ + g.drawImage(ResourceManager.explodes[step++], x, y, null); + if(step >= ResourceManager.explodes.length){ + step = 0; + } + } + + 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; + } + +} diff --git a/src/com/demo/tank/course3/Group.java b/src/com/demo/tank/course3/Group.java new file mode 100644 index 0000000..e37c523 --- /dev/null +++ b/src/com/demo/tank/course3/Group.java @@ -0,0 +1,5 @@ +package com.demo.tank.course3; + +public enum Group { + GOOD, BAD +} diff --git a/src/com/demo/tank/course3/MainV3.java b/src/com/demo/tank/course3/MainV3.java index ffd0a53..97edf45 100644 --- a/src/com/demo/tank/course3/MainV3.java +++ b/src/com/demo/tank/course3/MainV3.java @@ -6,7 +6,7 @@ public class MainV3 { public static void main(String[] args) throws InterruptedException, IOException { TankFrameV3 tf = new TankFrameV3(); for(int i = 0; i < 5; i++){ - tf.enemyTanks.add(new Tank(50 + i*80, 200, Direction.DOWN, tf)); + tf.enemyTanks.add(new Tank(50 + i*80, 200, Direction.DOWN, Group.BAD, tf)); } while (true){ diff --git a/src/com/demo/tank/course3/ResourceManager.java b/src/com/demo/tank/course3/ResourceManager.java index e981221..288ef1a 100644 --- a/src/com/demo/tank/course3/ResourceManager.java +++ b/src/com/demo/tank/course3/ResourceManager.java @@ -7,6 +7,7 @@ import java.io.IOException; public class ResourceManager { public static BufferedImage tankL, tankR, tankU, tankD; public static BufferedImage bulletL, bulletR, bulletU, bulletD; + public static BufferedImage[] explodes = new BufferedImage[16]; static { try { @@ -19,6 +20,10 @@ public class ResourceManager { bulletR = ImageIO.read(ResourceManager.class.getClassLoader().getResourceAsStream("com/demo/tank/images/bulletR.gif")); bulletU = ImageIO.read(ResourceManager.class.getClassLoader().getResourceAsStream("com/demo/tank/images/bulletU.gif")); bulletD = ImageIO.read(ResourceManager.class.getClassLoader().getResourceAsStream("com/demo/tank/images/bulletD.gif")); + + for (int i= 0; i<16; i++){ + explodes[i] = ImageIO.read(ResourceManager.class.getClassLoader().getResourceAsStream("com/demo/tank/images/e"+ (i+1)+".gif")); + } } catch (IOException e) { e.printStackTrace(); } diff --git a/src/com/demo/tank/course3/Tank.java b/src/com/demo/tank/course3/Tank.java index cd30222..b18d1ee 100644 --- a/src/com/demo/tank/course3/Tank.java +++ b/src/com/demo/tank/course3/Tank.java @@ -1,22 +1,25 @@ package com.demo.tank.course3; import java.awt.*; +import java.util.Random; public class Tank { private int x,y; private Direction dir; - private static final int SPEED = 10; - private boolean moving; + private static final int SPEED = 8; + private boolean moving = false; private boolean living = true; + private Group group = Group.BAD; private TankFrameV3 tankFrame; public static final int WIDTH = ResourceManager.tankD.getWidth(); public static final int HEIGHT = ResourceManager.tankD.getHeight(); + private Random random = new Random(); - - public Tank(int x, int y, Direction dir, TankFrameV3 tankFrame) { + public Tank(int x, int y, Direction dir, Group group, TankFrameV3 tankFrame) { this.x = x; this.y = y; this.dir = dir; + this.group = group; this.tankFrame = tankFrame; } @@ -55,6 +58,13 @@ public class Tank { default: break; } +// if(random.nextInt(10) > 8) this.fire(); + } + + public void fire() { + int bx = x + Tank.WIDTH/2 - Bullet.WIDTH/2; + int by = y + Tank.HEIGHT/2 - Bullet.HEIGHT/2; + tankFrame.bullets.add(new Bullet(bx, by, this.dir, this.group, tankFrame)); } public void die(){ @@ -101,9 +111,11 @@ public class Tank { this.living = living; } - public void fire() { - int bx = x + Tank.WIDTH/2 - Bullet.WIDTH/2; - int by = y + Tank.HEIGHT/2 - Bullet.HEIGHT/2; - tankFrame.bullets.add(new Bullet(bx, by, this.dir, tankFrame)); + public Group getGroup() { + return group; + } + + public void setGroup(Group group) { + this.group = group; } } diff --git a/src/com/demo/tank/course3/TankFrameV3.java b/src/com/demo/tank/course3/TankFrameV3.java index a994fea..5c136cf 100644 --- a/src/com/demo/tank/course3/TankFrameV3.java +++ b/src/com/demo/tank/course3/TankFrameV3.java @@ -13,10 +13,11 @@ public class TankFrameV3 extends Frame { public static final int GAME_HEIGHT = 600; Image image = null; - Tank tank = new Tank(500, 500, Direction.UP, this); + Tank tank = new Tank(500, 500, Direction.UP, Group.GOOD,this); // Bullet bullet = new Bullet(520, 440, Direction.UP); List bullets = new ArrayList(); List enemyTanks = new ArrayList<>(); + Explode explode = new Explode(200, 300, this); public TankFrameV3(){ setVisible(true); @@ -57,6 +58,8 @@ public class TankFrameV3 extends Frame { tank.paint(g); + explode.paint(g); + for(int i = 0; i< enemyTanks.size(); i++){ enemyTanks.get(i).paint(g); }