找传奇、传世资源到传世资源站!
Java 正文

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

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

from clipboard
from clipboardpackage 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毫秒增1if (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毫秒增1if (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(); // 生命减1hero.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(); // 启动执行}}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复