基于Java制作一個(gè)好玩的打飛機(jī)游戲
1.效果圖
2.項(xiàng)目整體構(gòu)造
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; ? //游戲持續(xù)的時(shí)間 ?? ? ?? ?//@Override ?? ?public void paint(Graphics g) {?? ??? ?//自動(dòng)被調(diào)用。 ?g相當(dāng)于一只畫筆 ?? ??? ?Color ? c = ?g.getColor(); ?? ??? ?g.drawImage(bg, 0, 0, null); ?? ??? ? ?? ??? ?plane.drawSelf(g); ?//畫飛機(jī) ?? ??? ? ?? ??? ?//畫出所有的炮彈 ?? ??? ?for(int i=0;i<shells.length;i++){ ?? ??? ??? ?shells[i].draw(g); ?? ??? ??? ? ?? ??? ??? ?//飛機(jī)和炮彈的碰撞檢測(cè)?。?! ?? ??? ??? ?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); ?? ??? ??? ?} ?? ??? ??? ? ?? ??? ??? ?//計(jì)時(shí)功能,給出提示 ?? ??? ??? ?if(!plane.live){ ?? ??? ??? ??? ?g.setColor(Color.red); ?? ??? ??? ??? ?Font ? f ?= ?new Font("宋體", Font.BOLD, 50); ?? ??? ??? ??? ?g.setFont(f); ?? ??? ??? ??? ?g.drawString("時(shí)間:"+period+"秒", (int)plane.x, (int)plane.y); ?? ??? ??? ?} ?? ??? ??? ? ?? ??? ?} ?? ??? ? ?? ??? ?g.setColor(c); ?? ?} ?? ? ?? ? ?? ?//幫助我們反復(fù)的重畫窗口! ?? ?class ?PaintThread ?extends ?Thread ?{ ?? ??? ?//@Override ?? ??? ?public void run() { ?? ??? ??? ?while(true){ ?? ??? ??? ??? ?repaint();?? ??? ?//重畫 ?? ??? ??? ??? ? ?? ??? ??? ??? ?try { ?? ??? ??? ??? ??? ?Thread.sleep(40); ? ?? ?//1s=1000ms ?? ??? ??? ??? ?} catch (InterruptedException e) { ?? ??? ??? ??? ??? ?e.printStackTrace(); ?? ??? ??? ??? ?}?? ??? ? ?? ??? ??? ?} ?? ??? ?} ?? ??? ? ?? ?} ?? ? ?? ?//定義鍵盤監(jiān)聽(tīng)的內(nèi)部類 ?? ?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("飛機(jī)大戰(zhàn)"); ?? ??? ?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();?? ?//啟動(dòng)重畫窗口的線程 ?? ??? ?addKeyListener(new KeyMonitor()); ? //給窗口增加鍵盤的監(jiān)聽(tīng) ?? ??? ? ?? ??? ? ?? ??? ?//初始化50個(gè)炮彈 ?? ??? ?for(int i=0;i<shells.length;i++){ ?? ??? ??? ?shells[i] = new Shell(); ?? ??? ?} ?? ??? ? ?? ?}
4.飛機(jī)類代碼展示
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); ?? ??? ? ?? ?} ?? ? ?? ?//按下某個(gè)鍵,增加相應(yīng)的方向 ?? ?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; ?? ?}
到此這篇關(guān)于基于Java制作一個(gè)好玩的打飛機(jī)游戲的文章就介紹到這了,更多相關(guān)Java打飛機(jī)游戲內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot整合mqtt實(shí)現(xiàn)消息訂閱和推送功能
mica-mqtt-client-spring-boot-starter是一個(gè)方便、高效、可靠的MQTT客戶端啟動(dòng)器,適用于需要使用MQTT協(xié)議進(jìn)行消息通信的Spring Boot應(yīng)用程序,這篇文章主要介紹了springboot整合mqtt實(shí)現(xiàn)消息訂閱和推送功能,需要的朋友可以參考下2024-02-02SpringSecurity退出功能實(shí)現(xiàn)的正確方式(推薦)
本文將介紹在Spring Security框架下如何實(shí)現(xiàn)用戶的"退出"logout的功能。本文通過(guò)實(shí)例代碼講解的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2019-11-11Java使用for循環(huán)解決經(jīng)典的雞兔同籠問(wèn)題示例
這篇文章主要介紹了Java使用for循環(huán)解決經(jīng)典的雞兔同籠問(wèn)題,結(jié)合實(shí)例形式分析了Java巧妙使用流程控制語(yǔ)句for循環(huán)解決雞兔同籠問(wèn)題相關(guān)操作技巧,需要的朋友可以參考下2018-05-05淺談Java自定義類加載器及JVM自帶的類加載器之間的交互關(guān)系
這篇文章主要介紹了淺談Java自定義類加載器及JVM自帶的類加載器之間的交互關(guān)系,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02springboot驗(yàn)證碼生成以及驗(yàn)證功能舉例詳解
登錄注冊(cè)是大部分系統(tǒng)需要實(shí)現(xiàn)的基本功能,同時(shí)也會(huì)對(duì)登錄驗(yàn)證增加需求,下面這篇文章主要給大家介紹了關(guān)于springboot驗(yàn)證碼生成以及驗(yàn)證功能的相關(guān)資料,需要的朋友可以參考下2023-04-04MyBatis-Plus中最簡(jiǎn)單的查詢操作教程(Lambda)
這篇文章主要給大家介紹了關(guān)于MyBatis-Plus中最簡(jiǎn)單的查詢操作的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-03-03