飛機(jī)大戰(zhàn)實驗報告參考模板_第1頁
飛機(jī)大戰(zhàn)實驗報告參考模板_第2頁
飛機(jī)大戰(zhàn)實驗報告參考模板_第3頁
飛機(jī)大戰(zhàn)實驗報告參考模板_第4頁
飛機(jī)大戰(zhàn)實驗報告參考模板_第5頁
已閱讀5頁,還剩7頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、飛機(jī)大戰(zhàn)實驗報告專業(yè):網(wǎng)絡(luò)工程132班學(xué)號:139074298姓名:孫 仁 強(qiáng)計算機(jī)科學(xué)與技術(shù)學(xué)院二零一六年十二月1 / 12一、軟件運行所需要的軟硬件環(huán)境 本系統(tǒng)是以Windows系統(tǒng)為操作平臺,用Java編程語言來實現(xiàn)本系統(tǒng)所需功能的。 本機(jī)器的配置如下: 處理器:CORE i7 主頻:1.2Hz以上 內(nèi)存:4G以上 硬盤:HHD 50G編程語言:Java 開發(fā)環(huán)境:windows7 開發(fā)軟件:Eclipse Mars二、游戲流程1. 用戶打開游戲,進(jìn)入開始菜單。 2. 用戶點擊開

2、始游戲按鈕,進(jìn)入游戲界面; 3. 用戶通過觸屏方式控制玩家飛機(jī)上下左右移動,躲避與子彈相撞; 4. 游戲失敗后,顯示本次游戲得分,用的秒數(shù)和水平; 5. 退出游戲三、主要代碼1、準(zhǔn)備代碼 設(shè)置窗口 使用雙緩沖使飛機(jī)不閃爍Constant設(shè)置窗口大小package com.ahut.準(zhǔn)備代碼;public class Constant public static final int GAME_WIDTH = 350;public static final int GAME_HEIGHT = 600;package com.ahut.準(zhǔn)備代碼;import java.

3、awt.Image;import java.awt.image.BufferedImage;import java.io.IOException;import .URL;public class GameUtil private GameUtil () public static Image getImage(String path) BufferedImage bi = null; try URL u = GameUtil.class.getClassLoader().getResource(path); bi = javax.imageio.ImageIO.read(u); catch (

4、IOException e) / TODO Auto-generated catch block e.printStackTrace(); return bi; package com.ahut.準(zhǔn)備代碼;import java.awt.Frame;import java.awt.Graphics; import java.awt.Image; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent;public class MyFrame extends Framepublic void lauchFram

5、e() setSize(Constant.GAME_WIDTH, Constant.GAME_HEIGHT); setLocation(100, 100); setVisible(true); new PaintThread().start(); addWindowListener(new WindowAdapter() Override public void windowClosing(WindowEvent e) System.exit(0); ); private Image offScreenImage = null; public void update(Graphics g) i

6、f (offScreenImage = null) offScreenImage = this.createImage(Constant.GAME_WIDTH, Constant.GAME_HEIGHT); Graphics gOff = offScreenImage.getGraphics(); paint(gOff); g.drawImage(offScreenImage, 0, 0, null); class PaintThread extends Thread public void run() while (true) repaint(); try Thread.sleep(40);

7、 catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace(); 主代碼飛機(jī):package com.ahut.plane;import java.awt.Graphics;import java.awt.event.KeyEvent;import com.ahut.準(zhǔn)備代碼.GameUtil;public class Plane extends GameObject private boolean left, up, right, down; private boolean live =

8、 true; public void draw(Graphics g) if (live) g.drawImage(img, (int)x, (int)y, null); move(); 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 KeyEv

9、ent.VK_DOWN: /下 down = true; break; default: 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;

10、default: break; public void move() if (left) x -= speed; if (right) x += speed; if (up) y -= speed; if (down) y += speed; public Plane(String imgpath, double x, double y) this.img = GameUtil.getImage(imgpath); this.width = img.getWidth(null); this.height = img.getHeight(null); this.x = x; this.y = y

11、; public Plane() public void setLive(boolean live) this.live = live; public boolean isLive() return live; 子彈:package com.ahut.plane;import java.awt.Color;import java.awt.Graphics;import java.awt.Rectangle;import com.ahut.準(zhǔn)備代碼.Constant;public class Bullet extends GameObject double degree; public Bull

12、et() degree = Math.random() * Math.PI * 2; x = Constant.GAME_WIDTH / 2; y = Constant.GAME_HEIGHT / 2; width = 10; height = 10; public Rectangle getRect() return new Rectangle(int)x, (int)y, width, height); public void draw(Graphics g) Color oldColor = g.getColor(); g.setColor(Color.yellow); g.fillOv

13、al(int)x, (int)y, width, height); x += speed * Math.cos(degree); y += speed * Math.sin(degree); if (y > Constant.GAME_HEIGHT - height | y < 30) degree = -degree; if (x < 0 | x > Constant.GAME_WIDTH - width) degree = Math.PI - degree; g.setColor(oldColor); 游戲?qū)ο螅簆ackage com.ahut.plane;impo

14、rt java.awt.Image;import java.awt.Rectangle;public class GameObject Image img;double x,y;int speed=5;int width,height;public Rectangle getRect() return new Rectangle(int)x, (int)y, width, height); public GameObject(Image img, double x, double y, int speed, int width, int height) super(); this.img =

15、img; this.x = x; this.y = y; this.speed = speed; this.width = width; this.height = height; public GameObject() 主線程package com.ahut.plane;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Image;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import ja

16、va.util.ArrayList;import java.util.Date;import com.ahut.準(zhǔn)備代碼.GameUtil;import com.ahut.準(zhǔn)備代碼.MyFrame;public class PlaneGameFrame extends MyFrame Image bg = GameUtil.getImage("image/ped.jpg"); Plane p = new Plane("image/plane.png", 50, 50); ArrayList bulletList = new ArrayList(); Da

17、te startTime; Date endTime; public void paint(Graphics g) g.drawImage(bg, 0, 0, null); p.draw(g); /在這里畫子彈 for (int i = 0; i < bulletList.size(); i+) Bullet b = (Bullet)bulletList.get(i); b.draw(g); /檢測跟飛機(jī)的碰撞 boolean peng = b.getRect().intersects(p.getRect(); if (peng) p.setLive(false); break; if

18、(!p.isLive() int period = (int)endTime.getTime() - (int)startTime.getTime() / 1000; /轉(zhuǎn)換成秒 printInfo(g, "時間: " + period + "秒", 20, 115, 300, Color.white); switch (period / 10) case 0: case 1: printInfo(g, "菜鳥", 40, 115, 270, Color.white); break; case 2: printInfo(g, &quo

19、t;入門", 40, 115, 270, Color.yellow); break; case 4: printInfo(g, "精通", 40, 115, 270, Color.white); break; case 5: printInfo(g, "大師", 40, 115, 270, Color.white); break; / printInfo(g, "分?jǐn)?shù): 100", 10, 50, 50, Color.yellow); public void printInfo(Graphics g, String str, int size, int x, int y, Color color) Color c = g.getColor(); g.setColor(color); Font f = new Font("宋體", Font.BOLD, size); g.setFont(f);

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論