飞机大战实验报告

 飞机大战实验报告 专业: 网络工程 1 32班 学号:

 13 9074 298

 ﻩ姓名:孙

 仁 强 计算机科学与技术学院 二零一六年 十二月 一、软件运行所需要得软硬件环境

 本系统就是以Windows 系统为操作平台,用 Java 编程语言来实现本系统所需功能得。

 本机器得配置如下:

 处理器:CORE i7

 主频:1、2Hz以上

 内存:4G 以上

 硬盘:HHD 50G 编程语言:Java

 开发环境:windows7

 开发软件:Eclipse Mars 二、 游戏流程 1. 用户打开游戏,进入开始菜单。

 2. 用户点击开始游戏按钮,进入游戏界面;

 3. 用户通过触屏方式控制玩家飞机上下左右移动,躲避与子弹相撞;

 4. 游戏失败后,显示本次游戏得分,用得秒数与水平;

 5. 退出游戏 ﻩ

 ﻩ

 三、主要代码 1 、准备代码 设置窗口 使用双缓冲使飞机不闪烁

 ﻩ

 小大口窗置设 tnatsnoCﻩpac kag e 、ahut、准备代码; pub li c c lass Constant { pu blic s ta ti c fi nal i nt G A M E _WID T H = 350; public s tatic final int GA ME _H E IGH T

 = 600; } package 、ahut、准备代码; import java、awt、Image; 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 {

  oseRteg、)(redaoLssalCteg、ssalc、litUemaG = u LRUﻩurce(path);

 ;)u(daer、OIegamI、oiegami、xavaj = ibﻩ }

  { )e noitpecxEOI( hctacﻩ //

  kcolb hctac detareneg-otuA ODOTﻩ

  e、printStackTrace();

 }

 ﻩ

  return bi;

 }

 ﻩ} package 、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 Frame{

 public void lauchFrame() {

 setSize(Constant、GAME_WIDTH, Constant、GAME_HEIGHT);

  ;)001 ,001(noitacoLtesﻩ

  ;)eurt(elbisiVtesﻩ

  ;)(trats、)(daerhTtniaP wenﻩ

  { )(retpadAwodniW wen(renetsiLwodniWddaﻩ

 ﻩ

 edirrevOﻩ

 { )e tnevEwodniW(gnisolCwodniw diov cilbupﻩ

  System、exit(0);

 }

 ﻩ

  });

 }

  ;llun = egamIneercSffo egamI etavirpﻩ

 public void update(Graphics g)

 {

 ﻩ

 if (offScreenImage == null)

 ﻩ

  offScreenImage = this、createImage(Constant、GAME_WIDTH, Constant、GAME_HEIGHT);

 ﻩ

  Graphics gOff = offScreenImage、getGraphics();

  ﻩ

 paint(gOff);

  ﻩ

 ;)llun ,0 ,0 ,egamIneercSffo(egamIward、gﻩ ﻩ

  }

 ﻩ

  { daerhT sdnetxe daerhTtniaP ssalcﻩ

 { )(nur diov cilbupﻩ

 ﻩ

 { )eurt( elihwﻩ

 ﻩ

  repaint();

  ﻩ

  try {

 ﻩ ﻩ

 ﻩ

 ;)04(peels、daerhTﻩ

 ﻩ

 ﻩ

 ﻩ

 }

 { )e noitpecxEdetpurretnI( hctacﻩ

  ﻩ ﻩ ﻩ

 //

 kcolb hctac detareneg-otuA ODOTﻩ

  ﻩ

  ;)(ecarTkcatStnirp、eﻩ ﻩ

  ﻩ

 ﻩ

 }

  }

  ﻩ }

  ﻩ ﻩ } ﻩ} 主代码

 飞机: 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、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 KeyEvent、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;

 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;

 }

 public Plane()

 {}

 public void setLive(boolean live)

 {

 this、live = live;

 }

 public boolean isLive() {

 return live;

 }

 } 子弹: package 、ahut、plane; import java、awt、Color; import java、awt、Graphics; import java、awt、Rectangle; import 、ahut、准备代码、Constant; public class Bullet extends GameObject {

 double degree;

 public Bullet() {

 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、fillOval((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);

 }

 } 游戏对象: package 、ahut、plane; import java、awt、Image; import java、awt、Rectangle; public class GameObject {

  ;gmi egamIﻩ

 ;y,x elbuodﻩ int speed=5;

 int width,height;

  { )(tceRteg elgnatceR cilbupﻩ

  ﻩ

 ;)thgieh ,htdiw ,y)tni( ,x)tni((elgnatceR wen nruterﻩ

  }

 public GameObject(Image img, double x, double y, int speed, int width,

 int height)

 {

  ﻩ

 ;)(repusﻩ

  ﻩ

 ;gmi = gmi、sihtﻩ

  ﻩ

 ;x = x、sihtﻩ

  this、y = y;

 ﻩ

 ;deeps = deeps、sihtﻩ

  this、width = width;

 ;thgieh = thgieh、sihtﻩ

 }

  ﻩ ﻩ

 public GameObject()

 {}

 } 主线程 package 、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 java、util、ArrayList; import java、util、Date; import 、ahut、准备代码、GameUtil; import 、ahut、准备代码、MyFrame; public class PlaneGameFrame extends MyFrame {

 ;)”gpj、dep/egami”(egamIteg、litUemaG = gb egamIﻩ

  ;)05 ,05 ,”gnp、enalp/egami”(enalP wen = p enalPﻩ

  ArrayList bulletList = new ArrayList();

 ;emiTtrats etaDﻩ

  ;emiTdne etaDﻩ

  public void paint(Graphics g) {

 g、drawImage(bg, 0, 0, null);

 ;)g(ward、pﻩ//

  弹子画里这在ﻩ

  { )++i ;)(ezis、tsiLtellub < i ;0 = i tni( rofﻩ

  ;)i(teg、tsiLtellub)telluB( = b telluBﻩ

  b、draw(g); //

  撞碰得机飞跟测检ﻩ

  ;))(tceRteg、p(stcesretni、)(tceRteg、b = gnep naeloobﻩ

  { )gnep( fiﻩ

 ;)eslaf(eviLtes、pﻩ

  ;kaerbﻩ

  }

 }

 ﻩ

  if (!p、isLive())

 {

  ats)tni( - )(emiTteg、emiTdne)tni(( = doirep tniﻩrtTime、getTime())

 / 1000;

 //转换成秒

 printInfo(g, ”时间: ” + period + ”秒”, 20, 115, 300, Color、white);

 switch (period / 10)

 {

 :0 esacﻩ

  :1 esacﻩ

  ;)etihw、roloC ,072 ,511 ,04 ,"鸟菜” ,g(ofnItnirpﻩ

  ;kaerbﻩ

  case 2:

 printInfo(g, "入门", 40, 115, 270, Color、yellow);

 break;

 case 4:

 ;)etihw、roloC ,072 ,511 ,04 ,"通精" ,g(ofnItnirpﻩ

  break;

 :5 esacﻩ

  ;)etihw、roloC ,072 ,511 ,04 ,"师大” ,g(ofnItnirpﻩ

  break;

  }

 }

 ﻩ

  //

 printInfo(g, "分数:

 100”, 10, 50, 50, Color、yellow);

 }

 ,x tni ,ezis tni ,rts gnirtS ,g scihparG(ofnItnirp diov cilbupﻩint y, Color color)

 {

 Color c = g、getColor();

 ;)roloc(roloCtes、gﻩ

  ;)ezis ,DLOB、tnoF ,”体宋”(tnoF wen = f tnoFﻩ

  ;)f(tnoFtes、gﻩ

  g、drawString(str, x, y);

 g、setColor(c);

 }

 ﻩ

  { )(emarFhcnual diov cilbupﻩ

  ;)(emarFhcual、repusﻩ

  addKeyListener(new KeyMonitor());

 for (int i = 0; i 〈 10; i++) {

 Bullet b = new Bullet();

 ;)b(dda、tsiLtellubﻩ

  }

  ;)(etaD wen = emiTtratsﻩ }

  ﻩ

 class KeyMonitor extends KeyAdapter {

  Override

  { )e tnevEyeK(desserPyek diov cilbupﻩ

 ;)e(noitceriDdda、pﻩ }

  ﻩ

 Override

  { )e tnevEyeK(desaeleRyek diov cilbupﻩ

 ;)e(noitceriDsunim、pﻩ }

  ﻩ

 }

  public static void main(String[] args) {

  new PlaneGameFrame()、launchFrame();

  }

 } ﻩ

 四、游戏实现截图

 五 、实验总结 JAVA与 EclipsexMars 就是一款非常好得面向对象开发语言与平台,通过这一段时间得JAVA 程序开发,我感觉到尽管得就是不同得语言与平台,开发程序一样需要动脑与努力,每一款软件或者游戏都不就是一朝一夕能制作出得,都需要大量得构思与编程,学习好软件工程这门课对日后得编程很有用,最后还有繁琐得调试检查运行时得错误,通过这次接触 JAVA我今后会更努力得学习它。