Java實現(xiàn)飛機小游戲
本文實例為大家分享了Java實現(xiàn)飛機小游戲的具體代碼,供大家參考,具體內(nèi)容如下
該小游戲使用java語言實現(xiàn),使用工具idea。
共寫9個類
Constant;Explode;GameObject;GameUtil;Plane;Shell;image;images;Plan;
本文實例為大家分享了vue + element ui實現(xiàn)錨點定位的具體代碼,供大家參考,具體內(nèi)容如下
1,Constant;專門放常量
package com.game2; //專門放常量的一個類 public class Constant { ? ? public static final int GAME_WIDTH=500;//窗口的寬度 ? ? public static final int GAME_HEIDHT=500;//窗口的高度 ? ? public static final int GAME_X=300; ? ? public static final int GAME_Y=300; }
2,GameObject;工具父類
package com.game2; //寫工具父類 import java.awt.*; public class GameObject { ? ? //聲明6個變量 ? ? Image img; ? ? double x,y; ? ? int speed; ? ? int width,height; ? ? //重寫構(gòu)造方法 ? ? public void drawSelf(Graphics g){ ? ? ? ? g.drawImage(img,(int)x,(int)y,null); ? ? } ? ? //6個變量的構(gòu)造方法 ? ? public GameObject(Image img, double x, double y, int speed, int width, int height) { ? ? ? ? super(); ? ? ? ? this.img = img; ? ? ? ? this.x = x; ? ? ? ? this.y = y; ? ? ? ? this.speed = speed; ? ? ? ? this.width = width; ? ? ? ? this.height = height; ? ? } ? ? //重寫構(gòu)造方法 ? ? public GameObject(Image img, double x, double y) { ? ? ? ? super(); ? ? ? ? this.img = img; ? ? ? ? this.x = x; ? ? ? ? this.y = y; ? ? } ? ? //寫空的方法 ? ? public GameObject(){ ? ? } ? ? /* ? ? 返回物體所在的矩形,便于后續(xù)的碰撞檢測 ? ? ?*/ ? ? public Rectangle getRect(){ ? ? ? ? return new Rectangle((int)x,(int)y,width,height); ? ? } }
3,GameUtil;工具類
package com.game2; //工具類 import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.URL; 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; ? ? } }
4,Explode;爆炸
package com.game2; //爆炸類 import java.awt.*; public class Explode { ? ? //爆炸位置 ? ? double x,y; ? ? public Explode(double x,double y){ ? ? ? ? this.x = x; ? ? ? ? this.y = y; ? ? } ? ? //設(shè)置對象數(shù)組 ? ? static Image[] imgs =new Image[12]; ? ? static { ? ? ? ? //圖片循環(huán) ? ? ? ? for(int i= 0;i<12;i++){ ? ? ? ? ? ? imgs[i] = GameUtil.getImage("com/game2/images/a"+(i+1)+".png"); ? ? ? ? ? ? imgs[i].getWidth(null); ? ? ? ? } ? ? } ? ? //圖片計數(shù)輪播 ? ? int count; ? ? public void draw(Graphics g){ ? ? ? ? if(count <= 11){ ? ? ? ? ? ? g.drawImage(imgs[count],(int)x,(int)y,null ); ? ? ? ? ? ? count++; ? ? ? ? } ? ? } }
5,Plane;飛機類
package com.game2; //飛機類 import java.awt.*; 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(right) { ? ? ? ? ? ? ? ? x +=speed; ? ? ? ? ? ? } ? ? ? ? ? ? if(up){ ? ? ? ? ? ? ? ? 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 =5; ? ? ? ? this.width =30; ? ? ? ? this.height =40; ? ? } ? ? //按下鍵盤的某個鍵,增加相應(yīng)的方向 ? ? public void addDirection(KeyEvent e){ ? ? ? ? switch (e.getKeyCode()){ ? ? ? ? ? ? case KeyEvent.VK_UP: ? ? ? ? ? ? ? ? up = true; ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case KeyEvent.VK_DOWN: ? ? ? ? ? ? ? ? down = true; ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case KeyEvent.VK_LEFT: ? ? ? ? ? ? ? ? left = true; ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case KeyEvent.VK_RIGHT: ? ? ? ? ? ? ? ? right = true; ? ? ? ? ? ? ? ? break; ? ? ? ? } ? ? } ? ? //按下鍵盤的某個鍵,取消相應(yīng)的方向 ? ? public void minusDirection(KeyEvent e){ ? ? ? ? switch (e.getKeyCode()){ ? ? ? ? ? ? case KeyEvent.VK_UP: ? ? ? ? ? ? ? ? up = false; ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case KeyEvent.VK_DOWN: ? ? ? ? ? ? ? ? down = false; ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case KeyEvent.VK_LEFT: ? ? ? ? ? ? ? ? left = false; ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case KeyEvent.VK_RIGHT: ? ? ? ? ? ? ? ? right = false; ? ? ? ? ? ? ? ? break; ? ? ? ? } ? ? } }
6,Shell;炮彈類
package com.game2; //炮彈類 import java.awt.*; public class Shell extends GameObject { ? ? double degree; ? ? public Shell(){ ? ? ? ? x=200; ? ? ? ? y=200; ? ? ? ? width = 5; ? ? ? ? height = 5; ? ? ? ? speed = 2; ? ? ? ? degree = Math.random()*Math.PI *2; ? ? } ? ? public void draw(Graphics g){ ? ? ? ? Color c = g.getColor(); ? ? ? ? g.setColor(Color.green); ? ? ? ? g.fillOval((int)x,(int)y,width,height); ? ? ? ? //炮彈沿著任意角度飛 ? ? ? ? x+= speed*Math.cos(degree); ? ? ? ? y+= speed*Math.sin(degree); ? ? ? ? //讓炮彈在窗口內(nèi)反彈 ? ? ? ? if(x<5||x>Constant.GAME_WIDTH-width-5){ ? ? ? ? ? ? degree = Math.PI-degree; ? ? ? ? } ? ? ? ? if(y<30||y>Constant.GAME_HEIDHT-height-5){ ? ? ? ? ? ? degree= -degree; ? ? ? ? } ? ? ? ? g.setColor(c); ? ? } }
7,image;圖片包
8,images;爆炸圖片包
9,Plan;主類
package com.game2; //運行類 import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.Date; public class Plan extends Frame { ? ? //圖片路徑 ? ? static Image FeiJ = GameUtil.getImage("com\\game2\\image\\11.png"); ? ? static Image BeiJ = GameUtil.getImage("com\\game2\\image\\Xk.jpg"); ? ? static Image PaoD = GameUtil.getImage("com\\game2\\image\\00.png"); ? ? //定義飛機位置 ? ? Plane pl = new Plane(FeiJ,250,250);//飛機對象初始化 ? ? Shell[] shells = new Shell[50];//多個炮彈數(shù)組 ? ? Explode bao;// ? ? Date startTime = new Date();//開始時間 ? ? Date endTime;//結(jié)束時間 ? ? int period;//游戲持續(xù)的時間 ? ? //窗口內(nèi)繪制,自動調(diào)用 ? ? @Override ? ? public void paint(Graphics g) { ? ? ? ? Color c= g.getColor();//保存原字體顏色 ? ? ? ? g.drawImage(BeiJ,0,0,null); ? ? ? ? pl.drawSelf(g);//畫飛機 ? ? ? ? //畫出所有炮彈for循環(huán) ? ? ? ? for(int i= 0;i<shells.length;i++) { ? ? ? ? ? ? shells[i].draw(g); ? ? ? ? ? ? //判斷炮彈是否碰撞 ? ? ? ? ? ? boolean peng =shells[i].getRect().intersects(pl.getRect()); ? ? ? ? ? ? if (peng){ ? ? ? ? ? ? ? ? pl .live = false;//如果碰撞飛機死亡 ? ? ? ? ? ? ? ? if(bao==null){ ? ? ? ? ? ? ? ? ? ? bao = new Explode(pl.x, pl.y); ? ? ? ? ? ? ? ? ? ? //計算結(jié)束時間 ? ? ? ? ? ? ? ? ? ? endTime ?=new Date(); ? ? ? ? ? ? ? ? ? ? period= (int)((endTime.getTime()-startTime.getTime())/1000); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? bao.draw(g);//畫出爆炸 ? ? ? ? ? ? } ? ? ? ? ? ? //游戲時間計算 ? ? ? ? ? ? //如果飛機死亡 ? ? ? ? ? ? if(!pl.live){ ? ? ? ? ? ? ? ? //改顏色 ? ? ? ? ? ? ? ? g.setColor(Color.CYAN); ? ? ? ? ? ? ? ? //改字體 ? ? ? ? ? ? ? ? Font f = new Font("楷體",Font.BOLD,50); ? ? ? ? ? ? ? ? g.setFont(f); ? ? ? ? ? ? ? ? //時間計算 ? ? ? ? ? ? ? ? g.drawString("時間:"+period+"秒",(int)pl.x,(int)pl.y); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? g.setColor(c);//改回原來字體顏色 ? ? } ? ? //多線程 ? ? class PaintThread extends Thread{ ? ? ? ? @Override ? ? ? ? public void run() { ? ? ? ? ? ? //反復(fù)畫窗口 ? ? ? ? ? ? while(true){ ? ? ? ? ? ? ? ? repaint(); ? ? ? ? ? ? ? ? //時間間隔 ? ? ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? ? ? Thread.sleep(40); ? ? ? ? ? ? ? ? } catch (InterruptedException e) { ? ? ? ? ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? //定義鍵盤監(jiān)聽內(nèi)部類 ? ? class KeyMonitor extends KeyAdapter{ ? ? ? ? @Override ? ? ? ? public void keyPressed(KeyEvent e) { ? ? ? ? ? ? pl.addDirection(e); ? ? ? ? }//按下 ? ? ? ? @Override ? ? ? ? public void keyReleased(KeyEvent e) { ? ? ? ? ? ? pl.minusDirection(e); ? ? ? ? }//釋放 ? ? } ? ? //加入雙緩沖,使窗口頁面不閃爍 ? ? private Image offScreenImage = null; ? ? public void update(Graphics g){ ? ? ? ? if(offScreenImage == null) ? ? ? ? ? ? offScreenImage = this.createImage(Constant.GAME_WIDTH,Constant.GAME_HEIDHT);//游戲?qū)挾雀叨? ? ? ? ? Graphics gOff = offScreenImage.getGraphics(); ? ? ? ? paint(gOff); ? ? ? ? g.drawImage(offScreenImage,0,0,null); ? ? } ? ? /* ? ? 初始化窗口 ? ? ?*/ ? ? public void launchFrame(){ ? ? ? ? this.setTitle("飛機大戰(zhàn)");//窗口標題 ? ? ? ? this.setVisible(true);//窗口顯示可見 ? ? ? ? this.setSize(Constant.GAME_WIDTH,Constant.GAME_HEIDHT);//設(shè)置窗口大小 ? ? ? ? this.setLocation(Constant.GAME_X,Constant.GAME_Y);//設(shè)置窗口左上點在電腦屏幕上的位置 ? ? ? ? this.addWindowListener(new WindowAdapter() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void windowClosing(WindowEvent e) { ? ? ? ? ? ? ? ? System.exit(0); ? ? ? ? ? ? } ? ? ? ? });//關(guān)閉窗口進程 ? ? ? ? new PaintThread().start();//啟動重畫窗口的線程 ? ? ? ? addKeyListener(new KeyMonitor());//給窗口增加鍵盤的監(jiān)聽 ? ? ? ? //初始化生成50個炮彈 ? ? ? ? for(int i=0;i<shells.length;i++){ ? ? ? ? ? ? shells[i]= new Shell(); ? ? ? ? } ? ? } ? ? public static void main(String [] args){ ? ? ? ? Plan p = new Plan(); ? ? ? ? p.launchFrame(); ? ? } }
運行即可
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java畢業(yè)設(shè)計實戰(zhàn)之健身俱樂部管理系統(tǒng)的實現(xiàn)
這是一個使用了java+SSM+Mysql+Jsp開發(fā)的健身俱樂部管理系統(tǒng),是一個畢業(yè)設(shè)計的實戰(zhàn)練習,具有俱樂部管理該有的所有功能,感興趣的朋友快來看看吧2022-02-02NameNode?重啟恢復(fù)數(shù)據(jù)的流程詳解
這篇文章主要為大家介紹了NameNode?重啟恢復(fù)數(shù)據(jù)的流程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02Java?Servlet響應(yīng)httpServletResponse過程詳解
HttpServletResponse是處理http響應(yīng)的對象,調(diào)用該對象的方法,設(shè)置到對象屬性的內(nèi)容,tomcat最終會組織為http響應(yīng)報文2022-02-02SpringBoot集成FastDFS+Nginx整合基于Token的防盜鏈的方法
這篇文章主要介紹了SpringBoot集成FastDFS+Nginx整合基于Token的防盜鏈的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04