Java飛機躲避炮彈碰撞小遊戲
如果你是入門Java,那麼這個飛機躲避炮彈碰撞小遊戲將會是和不錯的入門項目選擇,可以讓你在學習基礎的情況下感受到代碼帶給你的樂趣。總代碼附在文章底下百度網盤中。
ps:如報錯,需根據自己的飛機圖片和背景路徑位置自行調整!!!
首先我們來看一下效果圖:
這是我們程序的思維導圖:
************************************************************************************************************
我將程序各功能的函數進行了封裝,下面的代碼爲各封裝類的代碼:
1、MyGameFrame遊戲的主函數包(繪製圖像、雙緩衝技術)
package cn.ws.game; import java.awt.Color; import java.awt.Font; import java.awt.Frame; import java.awt.Graphics; import java.awt.Image; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; public class MyGameFrame extends Frame { Image bg=GameUtil.getImage("images/bg.jpg"); //引入改圖片 Image planeImg=GameUtil.getImage("images/plane.png"); Plane plane = new Plane(planeImg,250,250); Shell[] shells = new Shell[50]; //創建50個炮彈數組 @Override public void paint(Graphics g) { //相當於畫筆工具,g就是畫筆 super.paint(g); //必須加的 g.drawImage(bg,0, 0, null); //加載圖片 plane.drawSelf(g); //畫飛機 //畫出所有炮彈 for(int i=0;i<shells.length;i++) { shells[i].draw(g); //是否飛機矩形框與小球矩形框相撞(碰撞檢測) boolean peng = shells[i].getRect().intersects(plane.getRect()); if(peng) { plane.live = false; } } } //窗口重畫 class PaintThread extends Thread{ public void run() { while(true) { repaint(); //重畫窗口 try { Thread.sleep(40); }catch(InterruptedException e) { e.printStackTrace(); } } } } //定義鍵盤監聽的內部類 class KeyMonitor extends KeyAdapter{ @Override public void keyPressed(KeyEvent e) { //按下 plane.addDirection(e); } @Override public void keyReleased(KeyEvent e) { //擡起 plane.minusDirection(e); } } //初始化窗口 public void launchFrame() { //主窗口啓動 this.setTitle("王爍製作的小灰機"); //主窗口標題 this.setVisible(true); //主窗口可見 this.setSize(Constant.GAME_WIDTH, Constant.GAME_HEIGHT); //主窗口尺寸 this.setLocation(400,100); //主窗口位置 this.addWindowListener(new WindowAdapter() { //主窗口關閉結束 public void windowClosing(WindowEvent e) { System.exit(0); } }); new PaintThread().start(); //啓動重畫窗口 addKeyListener(new KeyMonitor()); //增加鍵盤監聽 //初始化50個炮彈 for(int i=0;i<shells.length;i++) { shells[i] = new Shell(); } } public static void main(String[] args) { MyGameFrame f=new MyGameFrame(); f.launchFrame(); } //雙緩衝,屏幕不閃爍 private Image offScreenImage = null; public void update(Graphics g) { if(offScreenImage == null) offScreenImage = this.createImage(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);//這是遊戲窗口的寬度和高度 Graphics gOff = offScreenImage.getGraphics(); paint(gOff); g.drawImage(offScreenImage, 0, 0, null); } }
2、Constant設定窗口的大小包
package cn.ws.game; public class Constant { public static final int GAME_WIDTH = 500; public static final int GAME_HEIGHT = 500; }
3、GameObject初始化飛機大小以及返回所在矩形包(便於後續封裝檢測)
package cn.ws.game; import java.awt.Graphics; import java.awt.Image; import java.awt.Rectangle; public class GameObject { Image img; double x,y; int speed=3; int width; int heigth; public void drawSelf(Graphics g) { g.drawImage(img,(int)x,(int)y,null); } public GameObject(Image img, double x, double y, int speed, int width, int heigth) { super(); this.img = img; this.x = x; this.y = y; this.speed = speed; this.width = width; this.heigth = heigth; } public GameObject(Image img, double x, double y) { super(); this.img = img; this.x = x; this.y = y; } //返回物體所在矩形。便於後續碰撞 public Rectangle getRect() { return new Rectangle((int)x,(int)y,width,heigth); } public GameObject() { } }
4、GameUtil圖片讀取之BufferedImage得到像素矩陣包
package cn.ws.game; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.IOException; import javax.imageio.ImageIO; import javax.print.DocFlavor.URL; public class GameUtil { private GameUtil() { } public static Image getImage(String path) { BufferedImage bi= null; try { java.net.URL u = GameUtil.class.getClassLoader().getResource(path); bi = ImageIO.read(u); }catch(IOException e) { e.printStackTrace(); } return bi; } }
5、Plane初始化飛機和飛機控制和飛機矩形大小設定包
package cn.ws.game; import java.awt.Graphics; import java.awt.Image; import java.awt.event.KeyEvent; public class Plane extends GameObject{ boolean left,up,right,down; boolean live = true; public void drawSelf(Graphics g) { if(live) { g.drawImage(img,(int)x,(int)y,null); if(left) { x-=speed; } if(up) { y-=speed; } if(right) { x+=speed; } if(down) { y+=speed; } } } public Plane(Image img,double x,double y) { this.img=img; this.x=x; this.y=y; this.speed=3; this.width=3; //img.getWidth(null) 獲得圖片寬度 this.heigth=3; } //按下某個鍵,增加相應方向 public void addDirection(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_LEFT: left=true; break; case KeyEvent.VK_UP: up=true; break; case KeyEvent.VK_RIGHT: right=true; break; case KeyEvent.VK_DOWN: down=true; break; } } //按下某個鍵,減相應方向 public void minusDirection(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_LEFT: left=false; break; case KeyEvent.VK_UP: up=false; break; case KeyEvent.VK_RIGHT: right=false; break; case KeyEvent.VK_DOWN: down=false; break; } } }
6、Shell初始化子彈和子彈反彈包
package cn.ws.game; import java.awt.Color; import java.awt.Graphics; /** * 炮彈類 * @author 王爍 * */ public class Shell extends GameObject { double degree; public Shell() { x=200; y=200; width=10; heigth=10; speed=2; degree=Math.random()*Math.PI*2; //degree弧度,隨機生產(0-2PI) } public void draw(Graphics g) { Color c= g.getColor(); //先保存現在的顏色 g.setColor(Color.green); g.fillOval((int)x, (int)y, width, heigth); //炮彈沿着任意角度飛 x+=speed*Math.cos(degree); y+=speed*Math.sin(degree); if(x<0||x>Constant.GAME_WIDTH-width) { degree = Math.PI-degree; } if(y<30||y>Constant.GAME_HEIGHT-heigth) { degree = -degree; } g.setColor(c); //用完再換回去 } }
百度網盤提取源碼和所用到的圖片:https://pan.baidu.com/s/1V_Wa79P1-c0mZiDCP6X3kA
提取碼:1qhm
************************************************************************************************************