在好例子网,分享、交流、成长!
您当前所在位置:首页Java 开发实例Java语言基础 → java飞机大战3.0版本(源码)

java飞机大战3.0版本(源码)

Java语言基础

下载此实例
  • 开发语言:Java
  • 实例大小:3.98M
  • 下载次数:48
  • 浏览次数:402
  • 发布时间:2019-06-17
  • 实例类别:Java语言基础
  • 发 布 人:usernamedong
  • 文件格式:.zip
  • 所需积分:2
 相关标签: java 飞机 飞机大战

实例介绍

【实例简介】

【实例截图】

from clipboard


from clipboard

【核心代码】

package main;

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

//主程序测试类
public class ShootGame extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 9158805858745581422L;
	public static final int WIDTH = 400; // 窗口的宽
	public static final int HEIGHT = 654; // 窗口的高
	// 静态资源
	public static BufferedImage background; // 背景图
	public static BufferedImage start; // 开始图
	public static BufferedImage pause; // 暂停图
	public static BufferedImage gameover; // 游戏结束图
	public static BufferedImage airplane; // 敌机图
	public static BufferedImage bee; // 蜜蜂图
	public static BufferedImage bullet; // 子弹图
	public static BufferedImage hero0; // 英雄机0图
	public static BufferedImage hero1; // 英雄机1图
	
	public static AudioClip music;

	public static final int START = 0;
	public static final int RUNNING = 1;
	public static final int PAUSE = 2;
	public static final int GAME_OVER = 3;
	private int state = 0; // 当前状态

	private Hero hero = new Hero(); // 英雄机
	private Bullet[] bullets = {}; // 子弹数组
	private FlyingObject[] flyings = {}; // 敌人数组

	private Timer timer;
	private int intervel = 10; // 间隔时间:单位--毫秒

	// 静态块
	static {
		try {
			background = ImageIO.read(ShootGame.class.getResource("background.png"));
			start = ImageIO.read(ShootGame.class.getResource("start.png"));
			pause = ImageIO.read(ShootGame.class.getResource("pause.png"));
			gameover = ImageIO.read(ShootGame.class.getResource("gameover.png"));
			airplane = ImageIO.read(ShootGame.class.getResource("airplane.png"));
			bee = ImageIO.read(ShootGame.class.getResource("bee.png"));
			bullet = ImageIO.read(ShootGame.class.getResource("bullet.png"));
			hero0 = ImageIO.read(ShootGame.class.getResource("hero0.png"));
			hero1 = ImageIO.read(ShootGame.class.getResource("hero1.png"));
			
			URL musicPath = ShootGame.class.getResource("game_music.wav");
			music = Applet.newAudioClip(musicPath);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static FlyingObject nextOne() {
		Random rand = new Random();
		int type = rand.nextInt(20); // 生成0到19的随机数
		if (type == 0) { // 随机数为0,返回bee;否则返回敌机
			return new Bee();
		} else {
			return new Airplane();
		}
	}

	int flyEnteredIndex = 0;

	// 敌人登场
	public void enterAction() {// 10毫秒走一次
		flyEnteredIndex  ; // 每10毫秒增1
		if (flyEnteredIndex % 40 == 0) {
			FlyingObject obj = nextOne();
			flyings = Arrays.copyOf(flyings, flyings.length   1);
			flyings[flyings.length - 1] = obj;// 将敌人赋值给flyings数组的最后一个元素
		}
	}

	public void stepAction() { // 10毫秒走一次
		hero.step(); // 英雄机走一步
		int num = time1 / 15;
		for (int i = 0; i < flyings.length; i  ) {
			for(int j = 0; j <= num; j  ) {
				flyings[i].step(); // 敌人走一步
			}
		}
		for (int i = 0; i < bullets.length; i  ) {
			for(int j = 0; j <= num/2; j  ) {
				bullets[i].step(); // 子弹走一步
			}
		}
	}

	int shootIndex = 0;

	public void shootAction() { // 10毫秒走一次
		shootIndex  ; // 每10毫秒增1
		if (shootIndex % 30 == 0) { // 300毫秒发射一次子弹
			Bullet[] bs = hero.shoot();// 获取子弹对象
			bullets = Arrays.copyOf(bullets, bullets.length   bs.length);
			System.arraycopy(bs, 0, bullets, bullets.length - bs.length, bs.length);
		}
	}

	// 删除越界飞行物
	public void outOfBoundsAction() {
		int index = 0;
		FlyingObject[] flyingLives = new FlyingObject[flyings.length];
		for (int i = 0; i < flyings.length; i  ) {
			FlyingObject f = flyings[i];
			if (!f.outOfBounds()) {
				flyingLives[index] = f;// 不越界,将其装入flyingLives[]数组中
				index  ;
			}
		}
		flyings = Arrays.copyOf(flyingLives, index);

		index = 0;
		Bullet[] bulletsLives = new Bullet[bullets.length];
		for (int i = 0; i < bullets.length; i  ) {
			Bullet bs = bullets[i];
			if (!bs.outOfBounds()) {
				bulletsLives[index] = bs;// 不越界,将其装入flyingLives[]数组中
				index  ;
			}
		}
		bullets = Arrays.copyOf(bulletsLives, index);

	}

	int score = 0; // 得分
	static double time = 0.00;//时间
	static int time1 = 0;
	// 所有子弹与所有敌人撞

	public void bangAction() {
		for (int i = 0; i < bullets.length; i  ) {
			bang(bullets[i]);
		}
	}

	// 一个子弹与所有敌人撞
	public void bang(Bullet b) {
		int index = -1;// 被撞敌人的索引
		for (int i = 0; i < flyings.length; i  ) {// 遍历所有的敌人
			if (flyings[i].shootBy(b)) {// 判断是否撞上
				index = i; // 记录被撞敌人的索引
				break;
			}
		}
		if (index != -1) {// 撞上了
			FlyingObject one = flyings[index];
			if (one instanceof Enemy) {
				Enemy e = (Enemy) one;
				score  = e.getScore();
			}
			if (one instanceof Award) {
				Award a = (Award) one;
				int type = a.getType();
				switch (type) {
				case Award.DOUBLE_FIRE: // 奖励活力值
					hero.addDoubleFire(); // 英雄机增加火力
					break;
				case Award.LIFE: // 奖励命
					hero.addLife(); // 英雄机增命
					break;
				}
			}
			// 被撞敌人与flyings数组中的最后一个元素交换
			FlyingObject t = flyings[index];
			flyings[index] = flyings[flyings.length - 1];
			flyings[flyings.length - 1] = t;
			// 缩容,删除随后一个元素---即被撞的对象
			flyings = Arrays.copyOf(flyings, flyings.length - 1);
		}
	}

	public void checkGameOverAction() {
		if (isGameOver()) { // 结束游戏
			state = GAME_OVER;
			time1 = 0;
			time = 0;
		}
	}

	public boolean isGameOver() {
		for (int i = 0; i < flyings.length; i  ) { // 撞上了,
			if (hero.hit(flyings[i])) {
				hero.subtractLife(); // 生命减1
				hero.setDoubleFire(0); // 火力值清零

				// 相撞之后,交换缩容
				FlyingObject t = flyings[i];
				flyings[i] = flyings[flyings.length - 1];
				flyings[flyings.length - 1] = t;
				flyings = Arrays.copyOf(flyings, flyings.length - 1);
			}
		}
		return hero.getLife() <= 0; // 英雄机的命<=0,游戏结束
	}

	// 启动执行代码
	public void action() {
		
		MouseAdapter l = new MouseAdapter() {
			public void mouseMoved(MouseEvent e) {
				if (state == RUNNING) { // 运行状态下执行
					int x = e.getX(); // 鼠标Y坐标
					int y = e.getY(); // 鼠标X坐标
					hero.moveTo(x, y); // 英雄机随着鼠标移动而移动
				}
			}

			
			// 鼠标的点击事件
			public void mouseClicked(MouseEvent e) {
				switch (state) {
				case PAUSE:
					state = RUNNING;
					music.stop();
					music.loop();
					break;
				case RUNNING:
					state = PAUSE;
					music.stop();
					break;
				case START:
					state = RUNNING;
					music.stop();
					music.loop();
					break;
				case GAME_OVER:
					hero = new Hero();// 清理现场
					flyings = new FlyingObject[0];
					bullets = new Bullet[0];
					score = 0;
					state = START;
					music.stop();
					break;
				}
			}

			public void mouseEntered(MouseEvent e) {
				if (state == PAUSE) {
					state = RUNNING;
				}
			}

			public void mouseExited(MouseEvent e) {
				if (state == RUNNING) {
					state = PAUSE;
				}
			}
		};
		this.addMouseListener(l); // 处理鼠标操作事件
		this.addMouseMotionListener(l);// 处理鼠标移动事件

		timer = new Timer(); // 创建定时器对象
		timer.schedule(new TimerTask() {
			public void run() { // 定时干的那个事--10毫秒走一次
				if (state == RUNNING) { // 运行状态下执行
					enterAction();
					stepAction();// 飞行物走一步
					shootAction();// 子弹入场
					outOfBoundsAction();// 删除越界飞行物
					bangAction(); // 子弹与敌人撞
					time = time  0.01;
					time1 = (int)time;
					checkGameOverAction();
				}
				repaint(); // 重画,调用paint()
			}
		}, intervel, intervel);
	}


	// 重写paint()方法 g:表示画笔
	public void paint(Graphics g) {
		g.drawImage(background, 0, 0, null); // 画背景图
		paintHero(g);
		paintFlyingObjects(g);
		paintBullets(g);
		paintScore(g); // 画分,画名
		paintState(g);
	}

	// 画状态
	public void paintState(Graphics g) {
		switch (state) {
		case START: // 启动状态画启动图
			g.drawImage(start, 0, 0, null);
			break;
		case PAUSE: // 暂停状态画暂停图
			g.drawImage(pause, 0, 0, null);
			break;
		case GAME_OVER: // 结束状态画结束图
			g.drawImage(gameover, 0, 0, null);
			break;

		}
	}

	public void paintHero(Graphics g) {
		g.drawImage(hero.image, hero.x, hero.y, null); // 画英雄机对象
	}

	public void paintFlyingObjects(Graphics g) {
		for (int i = 0; i < flyings.length; i  ) {
			FlyingObject f = flyings[i];
			g.drawImage(f.image, f.x, f.y, null);
		}
	}

	public void paintBullets(Graphics g) {
		for (int i = 0; i < bullets.length; i  ) {
			Bullet b = bullets[i];
			g.drawImage(b.image, b.x, b.y, null);
		}

	}

	public void paintScore(Graphics g) { // 画分,画命
		g.setColor(new Color(0xFF0000));
		g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
		g.drawString("SCORE: "   score, 20, 25);
		g.drawString("LIFE: "   hero.getLife(), 20, 45);
		g.drawString("TIME:"  time1,20,65);
	}

	public static void main(String[] args) {
		
		JFrame frame = new JFrame("飞机大战"); // 窗口对象
		ShootGame game = new ShootGame(); // 面板
		frame.add(game); // 将面板添加到窗口中
    	BufferedImage image;
		try {
			image = ImageIO.read(ShootGame.class.getResource("icon.jpg"));
			frame.setIconImage(image);
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
		}  
		
		frame.setSize(WIDTH, HEIGHT); // 设置窗口的宽和高
		frame.setAlwaysOnTop(true); // 设置一直在最上面
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置默认关闭的操作:窗口关闭时退出程序
		frame.setLocationRelativeTo(null); // 设置窗口初始位置(居中)
		frame.setVisible(true); // 设置窗体可见

		game.action(); // 启动执行
	}

}

实例下载地址

java飞机大战3.0版本(源码)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警