基本信息
源码名称:java 飞机大战游戏源码下载(修改版)
源码大小:10.51M
文件格式:.zip
开发语言:Java
更新时间:2016-05-06
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 3 元 
   源码介绍
学习java入门知识点集合


package com.tarena.shoot;
/**扩展 (完整版)
 * 1.子弹与敌人撞上后,子弹消失
 * 2.大飞机----打3次才死,死后得40分
 * 3.大黄蜂----打5次才死,死后得60分,并且获得奖励:life 1,doubleFire
 * */
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.IOException;
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 = 1L;
	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;
	public static BufferedImage hero1;
	public static BufferedImage bigyellowbee;
	public static BufferedImage bossairplane;
	
	Hero hero = new Hero();
	FlyingObject[] flyings = {};
	Bullet[] bullets = {};
	
	private int state = 0;
	public static final int START = 0;
	public static final int PAUSE = 1;
	public static final int RUNNING = 2;
	public static final int GAMEOVER = 3;
	
	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("hero.png"));
			hero1 = ImageIO.read(ShootGame.class.getResource("hero.png"));
			bigyellowbee = ImageIO.read(ShootGame.class.getResource("bigyellowbee.png"));
			bossairplane = ImageIO.read(ShootGame.class.getResource("bossairplane.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public FlyingObject nextOne(){
		Random ran = new Random();
		int type = ran.nextInt(20);
		if(type<=1){
			return new Bee();
		}else if(type == 2){
			return new BigYellowBee();
		}else if(type == 3){
			return new BossAirplane();
		}else{
			return new Airplane();
		}
			
	}
	
	public void stepAction(){
		for(int i = 0;i<flyings.length;i  ){
			flyings[i].step();
		}
		for(int i = 0;i<bullets.length;i  ){
			bullets[i].step();
		}
		hero.step();
	}
	
	int flyEnteredIndex = 0;
	public void enteredAction(){
		flyEnteredIndex  ;
		if(flyEnteredIndex%50==0){
			FlyingObject f = nextOne();
			flyings = Arrays.copyOf(flyings,flyings.length 1);
			flyings[flyings.length-1] = f;
		}
	}
	
	int shootIndex = 0;
	public void shootAction(){
		shootIndex   ;
		if(shootIndex%30==0){
			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;
				index   ;
			}
		}
		flyings = Arrays.copyOf(flyingLives,index);
		
		index = 0;
		Bullet[] bulletLives = new Bullet[bullets.length];
		for(int i = 0;i<bullets.length;i  ){
			Bullet b = bullets[i];
			if(!b.outOfBounds()){
				bulletLives[index] = b;
				index   ;
			}
		}
		bullets = Arrays.copyOf(bulletLives,index);
	}
	
	public void bangAction(){
		int index = -1;//被撞的子弹的下标
		for(int i = 0;i<bullets.length;i  ){
			if(bang(bullets[i])){
				index = i;
			}
			if(index != -1){//删除被撞的子弹
				Bullet temp = bullets[index];
				bullets[index] = bullets[bullets.length-1];
				bullets[bullets.length-1] = temp;
				bullets = Arrays.copyOf(bullets,bullets.length-1);
			}
		}
	}
	
	int score = 0;
	public boolean bang(Bullet b){
		boolean flag = false;
		int index = -1;
		for(int i = 0;i<flyings.length;i  ){
			FlyingObject f = flyings[i];
			if(f instanceof Airplane || f instanceof Bee){//判断撞上的是敌机还是小蜜蜂
				if(f.shootBy(b)){
				index = i;
				flag = true;
				break;
			   }
			}
			if(f instanceof BossAirplane){//撞上的是Boss机
				if(f.shootBy(b)){
					BossAirplane ba = (BossAirplane)f;
					ba.substractLife();
					if(ba.getLife()==0){
						index = i;
						break;
					}
					flag = true;
				}
			}
			if(f instanceof BigYellowBee){//撞上的是大黄蜂
				if(f.shootBy(b)){
					BigYellowBee byb = (BigYellowBee)f;
					byb.substractLife();
					if(byb.getLife()==0){
						index = i;
						break;
					}
					flag = true;
				}
			}
			
		}
		if(index!=-1){
			FlyingObject one = flyings[index];
			if(one instanceof BigYellowBee){
				BigYellowBee byb = (BigYellowBee)one;
				score  = byb.getScore();
				hero.addDoubleFire();
				hero.addLife();
			}
			if(one instanceof BossAirplane){
				BossAirplane ba = (BossAirplane)one;
				score  = ba.getScore();
			}
			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;
				}
			}
			FlyingObject temp = flyings[index];
			flyings[index] = flyings[flyings.length-1];
			flyings[flyings.length-1] = temp;
			flyings = Arrays.copyOf(flyings,flyings.length-1);
		}
		
		return flag;
	}
	

	
	public void checkGameOverAction(){
		if(isGameOver()){
			state = GAMEOVER;
		}
	}
	public boolean isGameOver(){
		for(int i = 0;i<flyings.length;i  ){
			FlyingObject f = flyings[i];
			if(hero.hit(f)){
				hero.clearDoubleFire();
				hero.substractLife();
				FlyingObject temp = flyings[i];
				flyings[i] = flyings[flyings.length-1];
				flyings[flyings.length-1] = temp;
				flyings = Arrays.copyOf(flyings,flyings.length-1);
			}
		}
		return hero.getLife()<=0;
	}
	
	public void action(){
		MouseAdapter ma = new MouseAdapter(){
			public void mouseMoved(MouseEvent e){
				if(state == RUNNING){
					int x = e.getX();
					int y = e.getY();
					hero.moveTo(x, y);
				}
			}
			
			public void mouseEntered(MouseEvent e){
				if(state == PAUSE){
					state = RUNNING;
				}
			}
			
            public void mouseExited(MouseEvent e){
				if(state == RUNNING){
					state = PAUSE;
				}
			}
            
            public void mouseClicked(MouseEvent e){
				switch(state){
				case START:
					state = RUNNING;
					break;
				case GAMEOVER:
					state = START;
					hero = new Hero();
					bullets = new Bullet[0];
					flyings = new FlyingObject[0];
					score = 0;
					break;
				}
			}
		};
		this.addMouseListener(ma);
		this.addMouseMotionListener(ma);
		
		Timer timer = new Timer();
		int intevel = 10;
		timer.schedule(new TimerTask(){
			@Override
			public void run() {
				if(state == RUNNING){
					stepAction();
					enteredAction();
					shootAction();
					outOfBoundsAction();
					bangAction();
					checkGameOverAction();
				}
				repaint();
			}
		}, intevel,intevel);
	}
	
	public void paint(Graphics g){
		g.drawImage(background,0,0,null);
		paintHero(g);
		paintFlyingObjects(g);
		paintBullets(g);
		paintScoreAndLife(g);
		paintState(g);
	}
	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	paintScoreAndLife(Graphics g){
    	g.setColor(new Color(0xFF0000));
    	g.setFont(new Font(Font.SANS_SERIF,Font.BOLD,20));
    	g.drawString("SCORE:" score,15,25);
    	g.drawString("LIFE:" hero.getLife(),15,45);
    }
	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 GAMEOVER:
			g.drawImage(gameover,0,0,null);
			break;
		}	
	}
	
    
	@SuppressWarnings("static-access")
	public static void main(String[] args) {
		JFrame jf = new JFrame("shoot");
		ShootGame game = new ShootGame();
		jf.add(game);
		jf.setSize(WIDTH,HEIGHT);
		jf.setAlwaysOnTop(true);
		jf.setLocationRelativeTo(null);
		jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
		jf.setVisible(true);
		game.action();
	}

}