diff --git a/src/main/java/audio/explode.wav b/src/main/java/audio/explode.wav new file mode 100644 index 0000000..dc72ab1 Binary files /dev/null and b/src/main/java/audio/explode.wav differ diff --git a/src/main/java/audio/tank_fire.wav b/src/main/java/audio/tank_fire.wav new file mode 100644 index 0000000..85279eb Binary files /dev/null and b/src/main/java/audio/tank_fire.wav differ diff --git a/src/main/java/audio/tank_move.wav b/src/main/java/audio/tank_move.wav new file mode 100644 index 0000000..959b998 Binary files /dev/null and b/src/main/java/audio/tank_move.wav differ diff --git a/src/main/java/audio/war1.wav b/src/main/java/audio/war1.wav new file mode 100644 index 0000000..099a31e Binary files /dev/null and b/src/main/java/audio/war1.wav differ diff --git a/src/main/java/com/example/tankbattle/Audio.java b/src/main/java/com/example/tankbattle/Audio.java new file mode 100644 index 0000000..56e7cbc --- /dev/null +++ b/src/main/java/com/example/tankbattle/Audio.java @@ -0,0 +1,95 @@ +package com.example.tankbattle; + +import java.io.IOException; + +import javax.sound.sampled.AudioFormat; +import javax.sound.sampled.AudioInputStream; +import javax.sound.sampled.AudioSystem; +import javax.sound.sampled.DataLine; +import javax.sound.sampled.SourceDataLine; + +public class Audio { + + byte[] b = new byte[1024 * 1024 * 15]; + + public void loop() { + try { + + while (true) { + int len = 0; + sourceDataLine.open(audioFormat, 1024 * 1024 * 15); + sourceDataLine.start(); + //System.out.println(audioInputStream.markSupported()); + audioInputStream.mark(12358946); + while ((len = audioInputStream.read(b)) > 0) { + sourceDataLine.write(b, 0, len); + } + audioInputStream.reset(); + + sourceDataLine.drain(); + sourceDataLine.close(); + } + + } catch (Exception e) { + e.printStackTrace(); + } + } + + private AudioFormat audioFormat = null; + private SourceDataLine sourceDataLine = null; + private DataLine.Info dataLine_info = null; + + private AudioInputStream audioInputStream = null; + + public Audio(String fileName) { + try { + audioInputStream = AudioSystem.getAudioInputStream(Audio.class.getClassLoader().getResource(fileName)); + audioFormat = audioInputStream.getFormat(); + dataLine_info = new DataLine.Info(SourceDataLine.class, audioFormat); + sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLine_info); + //FloatControl volctrl=(FloatControl)sourceDataLine.getControl(FloatControl.Type.MASTER_GAIN); + //volctrl.setValue(-40);// + + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void play() { + try { + byte[] b = new byte[1024 * 5]; + int len = 0; + sourceDataLine.open(audioFormat, 1024 * 5); + sourceDataLine.start(); + System.out.println(audioInputStream.markSupported()); + // audioInputStream.mark(12358946); + while ((len = audioInputStream.read(b)) > 0) { + sourceDataLine.write(b, 0, len); + } + // audioInputStream.reset(); + + sourceDataLine.drain(); + sourceDataLine.close(); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + + public void close() { + try { + audioInputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) { + // Audio a = new Audio("audio/explode.wav"); + Audio a = new Audio("audio/war1.wav"); + a.loop(); + + } + +} diff --git a/src/main/java/com/example/tankbattle/Bullet.java b/src/main/java/com/example/tankbattle/Bullet.java index f5a3b09..e640117 100644 --- a/src/main/java/com/example/tankbattle/Bullet.java +++ b/src/main/java/com/example/tankbattle/Bullet.java @@ -4,7 +4,7 @@ import java.awt.Graphics; import java.awt.Rectangle; public class Bullet { - private static final int SPEED = 10; + private static final int SPEED = 6; public static int WIDTH = ResourceMgr.bulletD.getWidth(); public static int HEIGHT = ResourceMgr.bulletD.getHeight(); private int x, y; @@ -83,6 +83,9 @@ public class Bullet { if (rect1.intersects(rect2)) { tank.die(); this.die(); + int eX = tank.getX() + Tank.WIDTH / 2 - Explode.WIDTH / 2; + int eY = tank.getY() + Tank.HEIGHT / 2 - Explode.HEIGHT / 2; + tf.explodes.add(new Explode(eX, eY, tf)); } } diff --git a/src/main/java/com/example/tankbattle/Explode.java b/src/main/java/com/example/tankbattle/Explode.java index dcb53fc..ce0c8cc 100644 --- a/src/main/java/com/example/tankbattle/Explode.java +++ b/src/main/java/com/example/tankbattle/Explode.java @@ -8,7 +8,6 @@ public class Explode { private int x, y; - private boolean living = true; TankFrame tf = null; private int step = 0; @@ -24,6 +23,7 @@ public class Explode { g.drawImage(ResourceMgr.explodes[step++], x, y, null); if (step >= ResourceMgr.explodes.length) - step = 0; + tf.explodes.remove(this); + } } diff --git a/src/main/java/com/example/tankbattle/ImageUtil.java b/src/main/java/com/example/tankbattle/ImageUtil.java new file mode 100644 index 0000000..639a155 --- /dev/null +++ b/src/main/java/com/example/tankbattle/ImageUtil.java @@ -0,0 +1,24 @@ +package com.example.tankbattle; + +import java.awt.Graphics2D; +import java.awt.RenderingHints; +import java.awt.image.BufferedImage; + +public class ImageUtil { + public static BufferedImage rotateImage(final BufferedImage bufferedimage, + final int degree) { + int w = bufferedimage.getWidth(); + int h = bufferedimage.getHeight(); + int type = bufferedimage.getColorModel().getTransparency(); + BufferedImage img; + Graphics2D graphics2d; + (graphics2d = (img = new BufferedImage(w, h, type)) + .createGraphics()).setRenderingHint( + RenderingHints.KEY_INTERPOLATION, + RenderingHints.VALUE_INTERPOLATION_BILINEAR); + graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2); + graphics2d.drawImage(bufferedimage, 0, 0, null); + graphics2d.dispose(); + return img; + } +} diff --git a/src/main/java/com/example/tankbattle/ResourceMgr.java b/src/main/java/com/example/tankbattle/ResourceMgr.java index 68e3f56..dab015a 100644 --- a/src/main/java/com/example/tankbattle/ResourceMgr.java +++ b/src/main/java/com/example/tankbattle/ResourceMgr.java @@ -11,15 +11,16 @@ public class ResourceMgr { static { try { - tankL = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/tankL.gif")); - tankU = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/tankU.gif")); - tankR = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/tankR.gif")); - tankD = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/tankD.gif")); + tankU = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/BadTank1.png")); + tankL = ImageUtil.rotateImage(tankU, -90); + tankR = ImageUtil.rotateImage(tankU, 90); + tankD = ImageUtil.rotateImage(tankU, 180); + + bulletU = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/bulletU.png")); + bulletL = ImageUtil.rotateImage(bulletU, -90); + bulletR = ImageUtil.rotateImage(bulletU, 90); + bulletD = ImageUtil.rotateImage(bulletU, 180); - bulletL = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/bulletL.gif")); - bulletU = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/bulletU.gif")); - bulletR = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/bulletR.gif")); - bulletD = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/bulletD.gif")); for (int i = 0; i < 16; i++) explodes[i] = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/e" + (i + 1) + ".gif")); } catch (IOException e) { e.printStackTrace(); diff --git a/src/main/java/com/example/tankbattle/Tank.java b/src/main/java/com/example/tankbattle/Tank.java index c292b0f..36e9381 100644 --- a/src/main/java/com/example/tankbattle/Tank.java +++ b/src/main/java/com/example/tankbattle/Tank.java @@ -81,7 +81,15 @@ public class Tank { break; } - if (random.nextInt(10) > 8) this.fire(); + if (this.group == Group.BAD && random.nextInt(100) > 95) this.fire(); + + if (this.group == Group.BAD && random.nextInt(100) > 95) { + randomDir(); + } + } + + private void randomDir() { + this.dir = Dir.values()[random.nextInt(4)]; } public void paint(Graphics g) { diff --git a/src/main/java/com/example/tankbattle/TankFrame.java b/src/main/java/com/example/tankbattle/TankFrame.java index 4c32c47..eebf9d3 100644 --- a/src/main/java/com/example/tankbattle/TankFrame.java +++ b/src/main/java/com/example/tankbattle/TankFrame.java @@ -17,9 +17,10 @@ public class TankFrame extends Frame { List bullets = new ArrayList<>(); List tanks = new ArrayList<>(); - Explode e = new Explode(100, 100, this); + List explodes = new ArrayList<>(); - static final int GAME_WIDTH = 800,GAME_HEIGHT=600; + + static final int GAME_WIDTH = 1080, GAME_HEIGHT = 960; public TankFrame() { setSize(GAME_WIDTH, GAME_HEIGHT); @@ -55,7 +56,8 @@ public class TankFrame extends Frame { Color c = g.getColor(); g.setColor(Color.WHITE); g.drawString("子弹的数量:" + bullets.size(), 10, 60); - g.drawString("敌方坦克的数量:" + tanks.size(), 10, 60); + g.drawString("敌方坦克的数量:" + tanks.size(), 10, 80); + g.drawString("爆炸的数量:" + explodes.size(), 10, 100); g.setColor(c); myTank.paint(g); @@ -67,12 +69,15 @@ public class TankFrame extends Frame { tanks.get(i).paint(g); } + for (int i = 0; i < explodes.size(); i++) { + explodes.get(i).paint(g); + } + for (int i = 0; i < bullets.size(); i++) { for (int j = 0; j < tanks.size(); j++) bullets.get(i).collideWith(tanks.get(j)); } - e.paint(g); // for (Iterator it = bullets.iterator(); it.hasNext()) { // Bullet b = it.next(); // if (!b.live) it.remove(); diff --git a/src/main/resources/images/BadTank1.png b/src/main/resources/images/BadTank1.png new file mode 100644 index 0000000..d661b80 Binary files /dev/null and b/src/main/resources/images/BadTank1.png differ diff --git a/src/main/resources/images/BadTank2.png b/src/main/resources/images/BadTank2.png new file mode 100644 index 0000000..c2a2b01 Binary files /dev/null and b/src/main/resources/images/BadTank2.png differ diff --git a/src/main/resources/images/GoodTank1.png b/src/main/resources/images/GoodTank1.png new file mode 100644 index 0000000..8afb8b0 Binary files /dev/null and b/src/main/resources/images/GoodTank1.png differ diff --git a/src/main/resources/images/GoodTank2.png b/src/main/resources/images/GoodTank2.png new file mode 100644 index 0000000..a1b131a Binary files /dev/null and b/src/main/resources/images/GoodTank2.png differ diff --git a/src/main/resources/images/bulletU.png b/src/main/resources/images/bulletU.png new file mode 100644 index 0000000..f6f7203 Binary files /dev/null and b/src/main/resources/images/bulletU.png differ diff --git a/src/main/resources/images/tank.png b/src/main/resources/images/tank.png new file mode 100644 index 0000000..54c82ba Binary files /dev/null and b/src/main/resources/images/tank.png differ