飞机大战实验报告.docx
《飞机大战实验报告.docx》由会员分享,可在线阅读,更多相关《飞机大战实验报告.docx(16页珍藏版)》请在冰豆网上搜索。
![飞机大战实验报告.docx](https://file1.bdocx.com/fileroot1/2023-2/24/75a27be2-498f-436e-a73d-0936090ea1c9/75a27be2-498f-436e-a73d-0936090ea1c91.gif)
飞机大战实验报告
飞机大战实验报告
专业:
网络工程132班
学号:
139074298
:
仁强
计算机科学与技术学院
二零一六年十二月
一、软件运行所需要的软硬件环境
本系统是以Windows系统为操作平台,用Java编程语言来实现本系统所需功能的。
本机器的配置如下:
处理器:
COREi7
主频:
1.2Hz以上
内存:
4G以上
硬盘:
HHD 50G
编程语言:
Java
开发环境:
windows7
开发软件:
Eclipse Mars
二、游戏流程
1.用户打开游戏,进入开始菜单。
2.用户点击开始游戏按钮,进入游戏界面;
3.用户通过触屏方式控制玩家飞机上下左右移动,躲避与子弹相撞;
4.游戏失败后,显示本次游戏得分,用的秒数和水平;
5.退出游戏
三、主要代码
1、准备代码设置窗口使用双缓冲使飞机不闪烁
Constant设置窗口大小
package.ahut.准备代码;
publicclassConstant{
publicstaticfinalintGAME_WIDTH=350;
publicstaticfinalintGAME_HEIGHT=600;
}
package.ahut.准备代码;
importjava.awt.Image;
importjava.awt.image.BufferedImage;
importjava.io.IOException;
import.URL;
publicclassGameUtil{
privateGameUtil(){}
publicstaticImagegetImage(Stringpath){
BufferedImagebi=null;
try{
URLu=GameUtil.class.getClassLoader().getResource(path);
bi=javax.imageio.ImageIO.read(u);
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
returnbi;
}
}
package.ahut.准备代码;
importjava.awt.Frame;
importjava.awt.Graphics;
importjava.awt.Image;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
publicclassMyFrameextendsFrame{
publicvoidlauchFrame(){
setSize(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);
setLocation(100,100);
setVisible(true);
newPaintThread().start();
addWindowListener(newWindowAdapter(){
Override
publicvoidwindowClosing(WindowEvente){
System.exit(0);
}
});
}
privateImageoffScreenImage=null;
publicvoidupdate(Graphicsg){
if(offScreenImage==null)
offScreenImage=this.createImage(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);
GraphicsgOff=offScreenImage.getGraphics();
paint(gOff);
g.drawImage(offScreenImage,0,0,null);
}
classPaintThreadextendsThread{
publicvoidrun(){
while(true){
repaint();
try{
Thread.sleep(40);
}catch(InterruptedExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
}
}
主代码
飞机:
package.ahut.plane;
importjava.awt.Graphics;
importjava.awt.event.KeyEvent;
import.ahut.准备代码.GameUtil;
publicclassPlaneextendsGameObject{
privatebooleanleft,up,right,down;
privatebooleanlive=true;
publicvoiddraw(Graphicsg){
if(live){
g.drawImage(img,(int)x,(int)y,null);
move();
}
}
publicvoidaddDirection(KeyEvente){
switch(e.getKeyCode()){
caseKeyEvent.VK_LEFT:
//左
left=true;break;
caseKeyEvent.VK_UP:
//上
up=true;break;
caseKeyEvent.VK_RIGHT:
//右
right=true;break;
caseKeyEvent.VK_DOWN:
//下
down=true;break;
default:
break;
}
}
publicvoidminusDirection(KeyEvente){
switch(e.getKeyCode()){
caseKeyEvent.VK_LEFT:
//左
left=false;break;
caseKeyEvent.VK_UP:
//上
up=false;break;
caseKeyEvent.VK_RIGHT:
//右
right=false;break;
caseKeyEvent.VK_DOWN:
//下
down=false;break;
default:
break;
}
}
publicvoidmove(){
if(left){
x-=speed;
}
if(right){
x+=speed;
}
if(up){
y-=speed;
}
if(down){
y+=speed;
}
}
publicPlane(Stringimgpath,doublex,doubley){
this.img=GameUtil.getImage(imgpath);
this.width=img.getWidth(null);
this.height=img.getHeight(null);
this.x=x;
this.y=y;
}
publicPlane(){}
publicvoidsetLive(booleanlive){
this.live=live;
}
publicbooleanisLive(){
returnlive;
}
}
子弹:
package.ahut.plane;
importjava.awt.Color;
importjava.awt.Graphics;
importjava.awt.Rectangle;
import.ahut.准备代码.Constant;
publicclassBulletextendsGameObject{
doubledegree;
publicBullet(){
degree=Math.random()*Math.PI*2;
x=Constant.GAME_WIDTH/2;
y=Constant.GAME_HEIGHT/2;
width=10;
height=10;
}
publicRectanglegetRect(){
returnnewRectangle((int)x,(int)y,width,height);
}
publicvoiddraw(Graphicsg){
ColoroldColor=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;
importjava.awt.Image;
importjava.awt.Rectangle;
publicclassGameObject{
Imageimg;
doublex,y;
intspeed=5;
intwidth,height;
publicRectanglegetRect(){
returnnewRectangle((int)x,(int)y,width,height);
}
publicGameObject(Imageimg,doublex,doubley,intspeed,intwidth,
intheight){
super();
this.img=img;
this.x=x;
this.y=y;
this.speed=speed;
this.width=width;
this.height=height;
}
publicGameObject(){}
}
主线程
package.ahut.plane;
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics;
importjava.awt.Image;
importjava.awt.event.KeyAdapter;
importjava.awt.event.KeyEvent;
importjava.util.ArrayList;
importjava.util.Date;
import.ahut.准备代码.GameUtil;
import.ahut.准备代码.MyFrame;
publicclassPlaneGameFrameextendsMyFrame{
Imagebg=GameUtil.getImage("image/ped.jpg");
Planep=newPlane("image/plane.png",50,50);
ArrayListbulletList=newArrayList();
DatestartTime;
DateendTime;
publicvoidpaint(Graphicsg){
g.drawImage(bg,0,0,null);
p.draw(g);
//在这里画子弹
for(inti=0;iBulletb=(Bullet)bulletList.get(i);
b.draw(g);
//检测跟飞机的碰撞
booleanpeng=b.getRect().intersects(p.getRect());
if(peng){
p.setLive(false);
break;
}
}
if(!
p.isLive()){
intperiod=((int)endTime.getTime()-(int)startTime.getTime())/1000;//转换成秒
printInfo(g,"时间:
"+period+"秒",20,115,300,Color.white);
switch(period/10){
case0:
case1:
printInfo(g,"菜鸟",40,115,270,Color.white);
break;
case2:
printInfo(g,"入门",40,115,270,Color.yellow);
break;
case4:
printInfo(g,"精通",40,115,270,Color.white);
break;
case5:
printInfo(g,"大师",40,115,270,Color.white);
break;
}
}
//printInfo(g,"分数:
100",10,50,50,Color.yellow);
}
publicvoidprintInfo(Graphicsg,Stringstr,intsize,intx,inty,Colorcolor){
Colorc=g.getColor();
g.setColor(color);
Fontf=newFont("宋体",Font.BOLD,size);
g.setFont(f);
g.drawString(str,x,y);
g.setColor(c);
}
publicvoidlaunchFrame(){
super.lauchFrame();
addKeyListener(newKeyMonitor());
for(inti=0;i<10;i++){
Bulletb=newBullet();
bulletList.add(b);
}
startTime=newDate();
}
classKeyMonitorextendsKeyAdapter{
Override
publicvoidkeyPressed(KeyEvente){
p.addDirection(e);
}
Override
publicvoidkeyReleased(KeyEvente){
p.minusDirection(e);
}
}
publicstaticvoidmain(String[]args){
newPlaneGameFrame().launchFrame();
}
}
四、游戏实现截图
五、实验总结
JAVA和EclipsexMars是一款非常好的面向对象开发语言和平台,通过这一段时间的JAVA程序开发,我感觉到尽管的是不同的语言和平台,开发程序一样需要动脑和努力,每一款软件或者游戏都不是一朝一夕能制作出的,都需要大量的构思和编程,学习好软件工程这门课对日后的编程很有用,最后还有繁琐的调试检查运行时的错误,通过这次接触JAVA我今后会更努力的学习它。