基於Java制作一個好玩的打飛機遊戲
1.效果圖
2.項目整體構造
3.主類代碼展示
public class MyGameFrame extends Frame { Image planeImg = GameUtil.getImage("images/plane.png"); Image bg = GameUtil.getImage("images/bg.jpg"); Plane plane = new Plane(planeImg,250,250); Shell[] shells = new Shell[50]; Explode bao ; Date startTime = new Date(); Date endTime; int period; //遊戲持續的時間 //@Override public void paint(Graphics g) { //自動被調用。 g相當於一隻畫筆 Color c = g.getColor(); 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; if(bao ==null){ bao = new Explode(plane.x, plane.y); endTime = new Date(); period = (int)((endTime.getTime()-startTime.getTime())/1000); } bao.draw(g); } //計時功能,給出提示 if(!plane.live){ g.setColor(Color.red); Font f = new Font("宋體", Font.BOLD, 50); g.setFont(f); g.drawString("時間:"+period+"秒", (int)plane.x, (int)plane.y); } } g.setColor(c); } //幫助我們反復的重畫窗口! class PaintThread extends Thread { //@Override public void run() { while(true){ repaint(); //重畫 try { Thread.sleep(40); //1s=1000ms } 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(300, 300); this.addWindowListener(new WindowAdapter() { //@Override 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(); } }
4.飛機類代碼展示
public void drawSelf(Graphics g){ if(live){ g.drawImage(img, (int)x,(int) y, null); if(left){ x -=speed; } if(right){ x += speed; } if(up){ y -=speed; //y = y-speed; } if(down){ y += speed; } }else{ } } public Plane(Image img, double x, double y){ this.img = img; this.x = x; this.y = y; this.speed = 3; this.width = img.getWidth(null) ; this.height = img.getHeight(null); } //按下某個鍵,增加相應的方向 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; } }
5.炮彈類代碼展示
public Shell(){ x = 200; y = 200; width=10; height = 10; speed = 3; degree = Math.random()*Math.PI*2; } public void draw(Graphics g){ Color c = g.getColor(); g.setColor(Color.YELLOW); g.fillOval((int)x,(int) y, width, height); //炮彈沿著任意角度去飛 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-height){ degree = - degree; }
6.爆炸類代碼展示
static { for (int i = 0; i < 16; i++) { imgs[i] = GameUtil.getImage("images/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 Explode(double x, double y) { this.x = x; this.y = y; }
到此這篇關於基於Java制作一個好玩的打飛機遊戲的文章就介紹到這瞭,更多相關Java打飛機遊戲內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!