当前位置:文档之家› 飞机大战实验报告

飞机大战实验报告

飞机大战实验报告专业:网络工程132班学号:*********姓名:孙**计算机科学与技术学院二零一六年十二月一、软件运行所需要的软硬件环境本系统是以Windows系统为操作平台,用Java编程语言来实现本系统所需功能的。

本机器的配置如下:处理器:CORE i7主频:1.2Hz以上内存:4G以上硬盘:HHD50G编程语言:Java开发环境:windows7开发软件:Eclipse Mars二、游戏流程1.用户打开游戏,进入开始菜单。

2.用户点击开始游戏按钮,进入游戏界面;3.用户通过触屏方式控制玩家飞机上下左右移动,躲避与子弹相撞;4.游戏失败后,显示本次游戏得分,用的秒数和水平;5.退出游戏三、主要代码1、准备代码设置窗口使用双缓冲使飞机不闪烁Constant设置窗口大小package com.ahut.准备代码;public class Constant {public static final int GAME_WIDTH = 350;public static final int GAME_HEIGHT = 600;}package com.ahut.准备代码;import java.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 (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return bi;}}package com.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);setLocation(100, 100);setVisible(true);new PaintThread().start();addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});}private Image offScreenImage = null;public void update(Graphics g) {if (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);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}}主代码飞机:package com.ahut.plane;import java.awt.Graphics;import java.awt.event.KeyEvent;import com.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 com.ahut.plane;import java.awt.Color;import java.awt.Graphics;import java.awt.Rectangle;import com.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 com.ahut.plane;import 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 = 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 java.util.ArrayList;import java.util.Date;import com.ahut.准备代码.GameUtil;import com.ahut.准备代码.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();Date 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);//检测跟飞机的碰撞boolean peng = b.getRect().intersects(p.getRect());if (peng) {p.setLive(false);break;}}if (!p.isLive()) {int period = ((int)endTime.getTime() - (int)startTime.getTime()) / 1000; //转换成秒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, "入门", 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, "分数: 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);g.drawString(str, x, y);g.setColor(c);}public void launchFrame() {uchFrame();addKeyListener(new KeyMonitor());for (int i = 0; i < 10; i++) {Bullet b = new Bullet();bulletList.add(b);}startTime = new Date();}class KeyMonitor extends KeyAdapter {@Overridepublic void keyPressed(KeyEvent e) {p.addDirection(e);}@Overridepublic void keyReleased(KeyEvent e) {p.minusDirection(e);}}public static void main(String[] args) {new PlaneGameFrame().launchFrame();}}四、游戏实现截图五、实验总结JAVA和EclipsexMars是一款非常好的面向对象开发语言和平台,通过这一段时间的JAVA 程序开发,我感觉到尽管的是不同的语言和平台,开发程序一样需要动脑和努力,每一款软件或者游戏都不是一朝一夕能制作出的,都需要大量的构思和编程,学习好软件工程这门课对日后的编程很有用,最后还有繁琐的调试检查运行时的错误,通过这次接触JAVA我今后会更努力的学习它。

相关主题