java飛機遊戲

Java飛機小遊戲

純屬自己娛樂,不足之處,請大佬指點,參考於尚學堂高淇300集。
裏面還附帶了圖片
由於還是萌新,所以變量名的定義都很。。。。簡單易懂,註釋也寫的算是很全面了
對於萌新就很友好

這個是遊戲父類

public class GameObject {
	 Image img;
	 double x,y;
	 int sudu;
	 int kuan,gao;
	public void drawSelf(Graphics g) {
		g.drawImage(img, (int)x,(int) y, null);
	}
	public GameObject(Image img, double x, double y, int sudu, int kuan, int gao) {
		super();
		this.img = img;
		this.x = x;
		this.y = y;
		this.sudu = sudu;
		this.kuan = kuan;
		this.gao = gao;
	}
	public GameObject(Image img, double x, double y) {
		super();
		this.img = img;
		this.x = x;
		this.y = y;
	}
	
	public GameObject() {
	}
	/*
	 * 返回物體所在的矩形,便於後續的碰撞檢測
	 */
	public Rectangle getPeng() {
		return new Rectangle((int)x,(int) y, kuan, gao);
	}
}

這個是飛機類

public class plane extends GameObject{
	
	
	 boolean shang,xia,zuo,you;
	 
	 boolean die = true ;
	public void drawSelf(Graphics g) {
		
		if(die) {
		g.drawImage(img, (int)x,(int) y, null);
		
		if(zuo) {
			x-= sudu;
		}
		if(you) {
			x+=sudu;
		}
		if(shang) {
			 y-= sudu;
		}
		if(xia) {
			y+= sudu;
		}
	}else {
		
	}
}
	public plane(Image img,double x,double y) {
		this.img = img;
		this.x = x;
		this .y = y;
		this.sudu = 10;
		this.kuan = img .getWidth(null);
		this.gao = img .getHeight(null);
	}
	
	//按下某個鍵,增加相應的方向
	public void addDirection(KeyEvent e) {
		switch (e.getKeyCode()) {
		case KeyEvent.VK_LEFT:
			zuo = true;
			break;
		case KeyEvent.VK_UP:
			shang = true;
			break;
		case KeyEvent.VK_RIGHT:
			you = true;
			break;
		case KeyEvent.VK_DOWN:
			xia = true;
			break;

		}
	}
	//按下某個鍵,取消相應的方向
	public void minusDirection(KeyEvent e) {
		switch (e.getKeyCode()) {
		case KeyEvent.VK_LEFT:
			zuo = false;
			break;
		case KeyEvent.VK_UP:
			shang = false; 
			break;
		case KeyEvent.VK_RIGHT:
			you = false;
			break;
		case KeyEvent.VK_DOWN:
			xia = false;
			break;

		}
	}
	
}

這個是炮彈類

public class paodan extends GameObject {
	
	double degree;    //弧度

	public paodan() {
		x=500;
		y= 400;
		kuan = 10;
		gao = 10;
		sudu = 3;
		degree = Math.random()*Math.PI*2;  //0到2PI的隨機數
	}
	public void draw(Graphics g) {
		Color c  = g.getColor();
		g.setColor(Color.red);
	
		g.fillOval((int)x, (int)y, kuan, gao);
		
		//炮彈沿着任意角度飛
		x += sudu*Math.cos(degree);
		y += sudu*Math.sin(degree);
		
		if(x<0||x>Constant.GAME_KUAN-kuan) {
			degree = Math.PI-degree;
		}
		
		if(y<30||y>Constant.GAME_GAO-gao) {
			degree = -degree;
		}
		
		
		g.setColor(c);
	
	}
	
}

炮彈類和飛機類都是繼承於遊戲父類
然後窗口的高和寬專門弄了的常量

public class Constant {
	public static final int GAME_GAO = 1200;
	public static final int GAME_KUAN = 900;
}

然後是這個工具類,用來加載圖片的

public class GameUtil {
    // 工具類最好將構造器私有化。
    private GameUtil() {
     
    } 
 /*
  * 返回指定路徑文件的圖片對象
  */
    public static Image getImage(String path) {
        BufferedImage bi = null;
        try {
            URL u = GameUtil.class.getClassLoader().getResource(path);
            bi = ImageIO.read(u);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bi;
    }
}

這是遊戲主窗口類,由於我的遊戲父類定義了碰撞檢測,所以後續的飛機和子彈相撞,他們本身其實是兩個矩形,只要兩個矩形相交,就會被檢測到,然後就會被判斷出飛機犧牲,繼而加載爆炸動畫,然後我還提供了計時,從飛機對象創建開始,到飛機與炮彈碰撞結束,便於讓用戶清楚自己的飛機存活了多久,
剛開始會發現窗口在不停的閃爍,體驗巨差,在實際開發中,繪製圖形是非常複雜的,繪圖可能需要幾秒甚至更長時間,也經常發生閃爍現象, 所以添加了雙緩衝(假如窗口類繼承的是Java.swing.JFrame則本身已解決了閃爍問題,但是並不完美,還是會有些許閃爍,後期還是需要改回到雙緩衝)

public class MyGame extends Frame{
	Image fj = GameUtil.getImage("tu/fj.png");//將圖片定義爲成員變量
	Image bj = GameUtil.getImage("tu/bj.jpg");
	
	plane pl =new plane(fj,250,20);

	 ArrayList<paodan>  paodans= new ArrayList<paodan>();//容器對象存儲多發炮彈
	
	baozha bao;//創建爆炸對象
	Date playTime = new Date();//創建開始時間
	Date dieTime;
	int time;//遊戲持續的時間
	
	@Override
	public void paint(Graphics g) {//paint的作用是畫出整個窗口及內部內容。被系統自動調用。g相當於畫筆
		Color c = g.getColor();
		
		g.drawImage(bj, 0, 0, null);
		pl.drawSelf(g);//畫飛機
		
		//畫出容器中所有的炮彈
		for(int i=0;i<paodans.size();i++) {
			paodan p = paodans.get(i);
			p.draw(g);
			
			//飛機與炮彈的碰撞檢測
		boolean peng =  p.getPeng().intersects(pl.getPeng());
		
		if(peng) {
			pl.die   = false ;
		
			if(bao ==null) {
			bao = new baozha(pl.x, pl.y);
			
			dieTime = new Date();
			time = (int)((dieTime.getTime()-playTime.getTime())/1000);			
			}
			bao.draw(g);			
			}
		if(!pl.die) {
			g.setColor(Color.blue);
			Font f = new Font("黑體", Font.BOLD, 60);
			g.setFont(f);
			g.drawString("你個菜雞存活了:"+time+"秒", 300, 300);
		}
		}
		g.setColor(c);
	}
	
	class xiancheng extends Thread{//這個線程可以幫我們反覆的重畫窗口
		@Override
		public void run() {
		while(true) {
			repaint();//重畫,不可以調上邊的paint,因爲他是自動被系統調的,我不能調
			
			try {
				Thread.sleep(40);//1s = 1000ms	40ms畫一次
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			}
		}
	}
	//鍵盤監聽的內部類
	class KeyMonitor extends KeyAdapter{

		@Override
		public void keyPressed(KeyEvent e) {
			pl.addDirection(e);
		}

		@Override
		public void keyReleased(KeyEvent e) {
			pl.minusDirection(e);
		}
		
	}
	
	/**
	 * 初始化窗口
	 */
	public void chuang() {
		this.setTitle("作者:小陳");//窗口標題
		this.setVisible(true);//窗口原本默認是不可見的,爲了讓窗口可見
		this.setSize(Constant.GAME_GAO,Constant.GAME_KUAN);// 窗口的大小
		this.setLocation(100, 10);//窗口的出現位置
		this.setVisible(true);//窗口原本默認是不可見的,爲了讓窗口可見
        //增加關閉窗口監聽,這樣用戶點擊右上角關閉圖標,可以關閉遊戲程序
		this.addWindowListener/*表示增加窗口監聽事件*/(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);//表示應用正常結束
			}
		});
		new xiancheng().start();//啓動線程
		addKeyListener(new KeyMonitor());//給窗口增加鍵盤的監聽
		
		//給炮彈初始化
		for(int i = 0; i<50;i++) {
			paodan p= new paodan();
			paodans.add(p);
			}
	}
	public static void main(String[] args) {
		MyGame zz = new MyGame();//創建對象
		zz.chuang();//調用上面的初始化窗口
	}
	
	private Image offScreenImage = null;
	 
	public void update(Graphics g) {
	    if(offScreenImage == null)
	        offScreenImage = this.createImage(Constant.GAME_GAO,Constant.GAME_KUAN);//這是遊戲窗口的寬度和高度
	     
	    Graphics gOff = offScreenImage.getGraphics();
	    paint(gOff);
	    g.drawImage(offScreenImage, 0, 0, null);
	}  
	
}

這是爆炸類,是一組16張圖片組成的動畫,形成的爆炸效果

public class baozha {
	 double x,y;
	 
	    static Image[] imgs = new Image[16];
	    static {
	        for(int i=0;i<16;i++){
	            imgs[i] = GameUtil.getImage("tu/explode/e"+(i+1)+".gif");
	            imgs[i].getWidth(null);
	        }
	    }
	     
	    int count;
	     
	    public void draw(Graphics g){
	        if(count<=15){
	            g.drawImage(imgs[count], (int)x, (int)y, null);
	            count++;
	        }
	    }
	     
	    public baozha(double x,double y){
	        this.x = x;
	        this.y = y;
	    }
}

圖片奉上
這是背景

這是一組爆炸動畫圖片
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章