坦克大战-基础版03

坦克大战03

给游戏加上背景音效

new Thread(() -> new Audio("audios/war1.wav").loop()).start();

在resourceManager中加入坦克和子弹的图片

初始化ImageUtil类,对图片进行操作

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

增加Audio类,控制声音

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

}

添加坦克移动 子弹发射 坦克爆炸的音效

在tf中添加爆炸list

List<Explode> explodes = new ArrayList<>();//爆炸

paint

g.drawString("爆炸数量" + explodes.size(), 10, 100);
for (int i = 0; i < explodes.size(); i++) {
            explodes.get(i).paint(g);
        }

bullet的fire中

tf.explodes.add(new Explode(x,y,tf));

敌人坦克随机移动

private void randomDir() {

        this.dir = Dir.values()[random.nextInt(4)];
 }

调整爆炸位置

if (rectBu.intersects(rectTa)) {
            tank.die();
            this.die();
            tf.explodes.add(new Explode(x,y,tf));
            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));
        }

边界检测

 private void boundCheck() {
        if (this.x < 0) x = 0;
        if (this.y < 28) y = 28;
        if (this.x > TankFrame.GAME_WIDTH - Tank.WIDTH - 2) x = TankFrame.GAME_WIDTH - Tank.WIDTH - 2;
        if (this.y > TankFrame.GAME_HEIGHT - Tank.HEIGHT - 2) y = TankFrame.GAME_HEIGHT - Tank.HEIGHT - 2;

    }

修复碰撞时产生很多rect的问题,在子弹和坦克中维护一个rect,随着移动进行更新

bullet

 Rectangle rect = new Rectangle();
public class Bullet {
        this.dir = dir;
        this.group = group;
        this.tf = tf;

        rect.x = this.x;
        rect.y = this.y;
        rect.width = WIDTH;
        rect.height = HEIGHT;
}

坦克行走声音,坦克爆炸声音

if(this.group == Group.GOOD) new Thread(()->new Audio("audios/tank_fire.wav").play()).start();
  new Thread(() -> new Audio("audios/explode.wav").play()).start();

使用配置文件

config

#tanks count at initialization
initTankCount=1

propertyManager

public class PropertyManager {
    static Properties props = new Properties();

    static {
        try {
            props.load(PropertyManager.class.getClassLoader().getResourceAsStream("config"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Object get(String key) {
        if (props == null) return null;
        return props.get(key);
    }
}
int initTankCount = Integer.parseInt((String) PropertyManager.get("initTankCount"));
for (int i = 0; i < initTankCount; i++) {
            tf.tanks.add(new Tank(50 + i * 80, 200,Dir.DOWN, Group.BAD, tf));
        }

修改一些固定参数到配置文件,添加getInt

    private static final int SPEED = PropertyManager.getInt("bulletSpeed");
    public static final int WIDTH = ResourceManager.bulletD.getWidth();
    public static final int HEIGHT = ResourceManager.bulletD.getHeight();

    public static int getInt(String key){
        if (props == null) return 0;
        return Integer.parseInt((String)props.get(key));
    }
#tanks count at initialization
initTankCount=10
initTankCount=10
tankSpeed=5
bulletSpeed=10
gameWidth=1080
gameHeight=720
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值