飞机大战实验报告_第1页
飞机大战实验报告_第2页
飞机大战实验报告_第3页
飞机大战实验报告_第4页
飞机大战实验报告_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、飞机大战实验报告专业:网络工程132班学号:139074298:仁强计算机科学与技术学院二零一六年十二月一、软件运行所需要的软硬件环境本系统是以Windows系统为操作平台,用Java编程语来实现本系统所需功能的。本机器的配置如下:处理器:CORE i7主频:1.2Hz以上存:4G以上硬盘:HHD 50G编程语言:Java开发环境:windows?开发软件:Ecl ipse Mars二、游戏流程1. 用户打开游戏,进入开始菜单。2. 用户点击开始游戏按钮,进入游戏界面;3. 用户通过触屛方式控制玩家飞机上下左右移动,躲避与子弹相撞;4. 游戏失败后,显示本次游戏得分,用的秒数和水平;5. 退出

2、游戏三、主要代码1、准备代码设置窗口使用双缓冲使飞机不闪烁Constant设置窗口大小packageahut.准备代码;public class Constant public static final int GAME_WIDTH = 350;public static final int GAMEHEIGHT = 600; package . ahut.准备代码;import java.awt 1mage; import jav 日.awt i mage Buffered linage;import java io.IOException;import . URL;publ

3、ic class GameUtil private GameUtil () public static Image get Image(String path) Buffeiedlmage bi = null;try URL u = GameUti1. class. getClassLoader()getResource(path); bi = javax. imageio. 1mneel0. read(u): catch (lOException e) / TODO Auto-generated catch blocke. piintStackTraceO ;return bi;packag

4、e . ahut.准备代码;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 Framepub 1 ic void lauchFrameO setSize(Constant. GAME WIDTH, Constant. GAME HEIGHT); setLocation(100, 100);setVisibl

5、e(true);new PaintThread() start();addWindowListener(new WindowAdapter() Overridepublic void windowC1osing(WindowEvent e) System. exit(0););private Image offScreenImage = null; public void update (Graphics g) if (offScreenlmage = null) offScreenImage = thiscreatelnuie(Constant.GAME WIDTH, Constant. G

6、AME HEIGHT):Graphics gOff = offScreenlmagegetGrsphics();paint(gOff);g. drawImage(offScreenImage, 0. 0. null):class PaintThread extends Thread pub 1 ic void run() while (true) repaint ();try Thread, sleep(40); catch (InterruptedException e) / TODO Auto-generated catch block e. printStackTraceO ;主代码飞机

7、:package .ahut.plane;import java. awt. Graphics; import java awt event KeyEvent;import . ahut.准备代码.GameUtil; public class Plane extends GameObject private boolean left, up, right, down;private boolean live = true;public void draw(Graphics g) if (live) g. drawlmage(img, (int)xt (int)y, null); move ()

8、;public void addDirection(KeyEvent e) switch (e. getKeyCodeO) 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;default: bieak;public void minusDirection(KeyEvent e) switch (e

9、. getKeyCodeO) 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;default: break;public void move() if (left) x 一二 speed;if (right) x += speed; if (up) y -= speed;if (down)

10、 y += speed;public Plane (String imgpath, double x, doub1e y) this img = GameUti1. getImage(imgpath): this .width = imggetWidth(null); this .height = imggetHeight(nul1); this. x = x;this y = y;publ ic Plane () public void setLive(boolean live) this.live = live;publ ic boolean isLiveO return 1ive;pac

11、kage ahutplane; import javaowt.Color; import java. awt. Graphics; import javaawt.Rectangle;import . ahut.准备代码.Constant;public class Bullet extends GameObject double degree;public Bullet() degree = Math, random() * Math. PI * 2;x = Constant. GAMEJVIDTH / 2;y = Constant. GAME HEIGHT / 2;width = 10;hei

12、ght = 10;public Rectangle getRect() return new Rectangle(int)x, (int)y, width, height); public void draw(Graphics g) Color oldColor = ggetColor();g.setColor(Color yellow);g. fillOval(int)x, (int)y, width, height):x += speed * Math, cos(degree);y += speed * Mathsin(degree);if (y Constant. GAMEJIEIGHT

13、 - height | | y 30) degree = -degree;if (x Constant. GAME WIDTH - width) degree = Math PI - degree;g. setCo 1 or (oldColor);游戏对象:package ahutplane;import javaawt.1mage;import javaawt.Rectangle;public class GameObject Image img;double x,y;int speed=5;int width,height;public Rectangle getRect() return

14、 new Rectangle(int)x, (int)y, width, height):public GameObject(Image img. double x, double yt 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;public GameObject() 主线程package ahutp】ane;import javaawt.Color;impo

15、rt javaawt.Font;import javaowt.Graphics;import javaowt Image;import java awt event KeyAdapter;import java awt event KeyEvent;import java .util. ArrayList;import java.uti】Date;import . ahut.准备代码.GameUtil;import . ahut.准备代码.My Frame;public class PlaneGameFrame extends MyFrame Image bg = Gamellti 1. ge

16、 11 mage (n i mage/ped. j pg);Plane p = new P1ane(n image/p1anepng. 50, 50);ArrayList bulletList = new ArrayList();Date startTime;Date endTime; public void paint(Graphics g) g.drawlmage(bg, 0, 0, null):p. draw(g);/在这里画子弹for (int i = 0; i bulletList. size(): i+) Bullet b = (Bullet)bulletList. get(i);

17、b. draw(g);/检测跟飞机的碰撞boolean peng = b. getRect() intersects(pgetRect(); if (peng) p.setLive(false);break;if (!p. isLiveO) intperiod=(int) endTimegetTime()(int) startTime. getTimeO) / 1000;/转换成秒printlnfo(g,时间:” + period + 秒20,115,300,Color white);switch (period / 10) case 0:case 1:printlnfo(g, break;c

18、ase 2: printlnfo(g, break;case 4:printlnfo(g, break;case 5: printlnfo(g, break;”菜鸟S 40.”入丁,40.”精通”,40.”大师 40,115,115,115,115,270. Color, white);270.270.270.Color. yellow);Color. white);Color. white);/print Info (g,分数:100, 10, 501 50, Color, yellow):public void printInfo(Graphics g, String str, int size, int x, int yt Color color) Color c = g.getColor();g setColor(color);Font f = new Font (宋体” Font. BOLD, size):g setFont (f);g. drawString(strt x, y);g se

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论