1. 程式人生 > >教你如何一步一步用Java實現飛機大戰(一)

教你如何一步一步用Java實現飛機大戰(一)

資源交流群

飛機大戰第一天

1.抽象類:飛行物

public abstract class FlyingObject{
protected BufferedImage image;
protected int width;  //寬
protected int height; //高
protected int x; //x座標
protected int y; 
//y座標}

2.介面:敵人

public interface Enemy {
	/** 得分 */
	public int getScore();
}

3.介面:獎勵

/** 獎勵 */
public interface Award {
	public int DOUBLE_FIRE = 0; //火力值
	public int LIFE = 1; //命
	/** 獲取獎勵型別(上面的0或1) */
	public int getType();
}

4.敵機,飛行物,也是敵人

public class Airplane extends FlyingObject implements Enemy {
	private int speed = 2; //移動的速度
	/** 構造方法 */
	public Airplane(){
		image = ShootGame.airplane; //圖片
		width = image.getWidth();   //寬
		height = image.getHeight(); //高
		Random rand = new Random(); //隨機數物件
		x = rand.nextInt(ShootGame.WIDTH-this.width); //x:0到(視窗寬-敵機寬)之間的隨機數
		y = -this.height; //y:負的敵機的高
	}
	
	/** 重寫getScore()得分 */
	public int getScore(){
		return 5; //打掉一個敵機得5分
	}}

5.小蜜蜂:飛行物同時是也是獎勵

/** 小蜜蜂: 是飛行物, */
public class Bee extends FlyingObject implements Award {
	private int xSpeed = 1; //x座標移動速度
	private int ySpeed = 2; //y座標移動速度
	private int awardType;  //獎勵的型別(0或1)
	/** 構造方法 */
	public Bee(){
		image = ShootGame.bee; //圖片
		width = image.getWidth();   //寬
		height = image.getHeight(); //高
		Random rand = new Random(); //隨機數物件
		x = rand.nextInt(ShootGame.WIDTH-this.width); //x:0到(視窗寬-小蜜蜂寬)之間的隨機數
		y = -this.height; //y:負的小蜜蜂的高
		awardType = rand.nextInt(2); //0和1之間的隨機數
	}
	
	/** 獲取獎勵型別 */
	public int getType(){
		return awardType; //返回獎勵型別
	}
}

6.子彈:同樣也是飛行物

/** 子彈: 是飛行物 */
public class Bullet extends FlyingObject {
	private int speed = 3; //移動的速度
	/** 構造方法 子彈的初始座標隨著英雄機定*/
	public Bullet(int x,int y){
		image = ShootGame.bullet; //圖片
		width = image.getWidth();   //寬
		height = image.getHeight(); //高
		this.x = x; //x:隨英雄機
		this.y = y; //y:隨英雄機
	}

7.英雄機:飛行物

/** 英雄機: 是飛行物 */
public class Hero extends FlyingObject {
	private int doubleFire; //火力值
	private int life; //命
	private BufferedImage[] images; //可切換的圖片陣列
	private int index; //協助圖片切換
	/** 構造方法 */
	public Hero(){
		image = ShootGame.hero0; //圖片
		width = image.getWidth();   //寬
		height = image.getHeight(); //高
		x = 150; //x:固定的值
		y = 400; //y:固定的值
		doubleFire = 10000; //預設為0(單倍火力)
		life = 3; //預設3條命
		images = new BufferedImage[]{ShootGame.hero0,ShootGame.hero1}; //兩張圖片切換
		index = 0; //協助圖片切換
	}
}

8.主類:視窗

//主視窗類
public class ShootGame extends JPanel {
	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
	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"));
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	private Hero hero = new Hero(); //一個英雄機
	private FlyingObject[] flyings = {}; //一堆敵人(敵機+小蜜蜂)
	private Bullet[] bullets = {}; //一堆子彈
	
	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 = START; //當前狀態(預設啟動狀態)
public static void main(String[] args) {
		JFrame frame = new JFrame("Fly"); //建立視窗物件
		ShootGame game = new ShootGame(); //建立面板物件
		frame.add(game); //將面板新增到視窗中
		frame.setSize(WIDTH, HEIGHT); //設定視窗大小
		frame.setAlwaysOnTop(true); //設定總是在最上面
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //設定視窗預設關閉操作(關閉視窗時退出程式)
		frame.setLocationRelativeTo(null); //設定居中顯示
		frame.setVisible(true); //1)設定視窗可見  2)儘快呼叫paint()方法
		
		game.action(); //啟動程式的執行
	}
/** 重寫paint() g:畫筆*/
	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); //畫子彈物件
		}
	}
}

9.敵人入場的實現步驟:

1)main(){ game.action(); }
  2)action(){
      ...
      run(){ //10毫秒定時執行
        enteredAction(); //敵人入場
	    repaint();
      }
    }
  3)int index = 0;
    enteredAction(){ //10毫秒
      index++;
      if(index%40==0){  //40*10毫秒
        FlyingObject one = nextOne(); //建立一個敵人物件
        flyings = Arrays.copyOf(flyings,flyings.length+1); //擴容
        flyings[flyings.length-1] = one; //將敵人物件新增到敵人陣列中
      }
    }
  4)nextOne(){
      生成0到19間的隨機數
      為0時return new Bee();
      否則return new Airplane();
    }