Compare commits

...

3 Commits

Author SHA1 Message Date
kn5886348135 f60ecaf688 好坦克和坏坦克的图片
3 years ago
kn5886348135 4122bfa034 坦克随机移动、调整爆炸位置
3 years ago
kn5886348135 5334cb5391 渲染爆炸
3 years ago

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -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();
}
}

@ -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));
}
}

@ -0,0 +1,29 @@
package com.example.tankbattle;
import java.awt.*;
public class Explode {
public static int WIDTH = ResourceMgr.explodes[0].getWidth();
public static int HEIGHT = ResourceMgr.explodes[0].getHeight();
private int x, y;
TankFrame tf = null;
private int step = 0;
public Explode(int x, int y, TankFrame tf) {
this.x = x;
this.y = y;
this.tf = tf;
}
public void paint(Graphics g) {
g.drawImage(ResourceMgr.explodes[step++], x, y, null);
if (step >= ResourceMgr.explodes.length)
tf.explodes.remove(this);
}
}

@ -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;
}
}

@ -5,20 +5,29 @@ import java.io.IOException;
import javax.imageio.ImageIO;
public class ResourceMgr {
public static BufferedImage tankL, tankU, tankR, tankD;
public static BufferedImage GoodTankL, GoodTankU, GoodTankR, GoodTankD;
public static BufferedImage BadTankL, BadTankU, BadTankR, BadTankD;
public static BufferedImage bulletL, bulletU,bulletR, bulletD;
public static BufferedImage[] explodes = new BufferedImage[16];
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"));
GoodTankU = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/GoodTank1.png"));
GoodTankL = ImageUtil.rotateImage(GoodTankU, -90);
GoodTankR = ImageUtil.rotateImage(GoodTankU, 90);
GoodTankD = ImageUtil.rotateImage(GoodTankU, 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"));
BadTankU = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/BadTank1.png"));
BadTankL = ImageUtil.rotateImage(BadTankU, -90);
BadTankR = ImageUtil.rotateImage(BadTankU, 90);
BadTankD = ImageUtil.rotateImage(BadTankU, 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);
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();
}

@ -6,8 +6,8 @@ import java.util.Random;
public class Tank {
private static final int SPEED = 1;
public static int WIDTH = ResourceMgr.tankD.getWidth();
public static int HEIGHT = ResourceMgr.tankD.getHeight();
public static int WIDTH = ResourceMgr.GoodTankD.getWidth();
public static int HEIGHT = ResourceMgr.GoodTankD.getHeight();
private Random random = new Random();
@ -81,23 +81,31 @@ public class Tank {
break;
}
if (random.nextInt(10) > 5) 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) {
if (!living) tf.tanks.remove(this);
switch (dir) {
case LEFT:
g.drawImage(ResourceMgr.tankL, x, y, null);
g.drawImage(this.group == Group.GOOD ? ResourceMgr.GoodTankL : ResourceMgr.BadTankL, x, y, null);
break;
case UP:
g.drawImage(ResourceMgr.tankU, x, y, null);
g.drawImage(this.group == Group.GOOD ? ResourceMgr.GoodTankU : ResourceMgr.BadTankU, x, y, null);
break;
case RIGHT:
g.drawImage(ResourceMgr.tankR, x, y, null);
g.drawImage(this.group == Group.GOOD ? ResourceMgr.GoodTankR : ResourceMgr.BadTankR, x, y, null);
break;
case DOWN:
g.drawImage(ResourceMgr.tankD, x, y, null);
g.drawImage(this.group == Group.GOOD ? ResourceMgr.GoodTankD : ResourceMgr.BadTankD, x, y, null);
break;
default:
break;

@ -17,7 +17,10 @@ public class TankFrame extends Frame {
List<Bullet> bullets = new ArrayList<>();
List<Tank> tanks = new ArrayList<>();
static final int GAME_WIDTH = 800,GAME_HEIGHT=600;
List<Explode> explodes = new ArrayList<>();
static final int GAME_WIDTH = 1080, GAME_HEIGHT = 960;
public TankFrame() {
setSize(GAME_WIDTH, GAME_HEIGHT);
@ -53,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);
@ -65,10 +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));
}
// for (Iterator<Bullet> it = bullets.iterator(); it.hasNext()) {
// Bullet b = it.next();
// if (!b.live) it.remove();

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 847 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 271 KiB

Loading…
Cancel
Save