JAVA课程设计报告保卫太空小游戏多线程实现毕业设计论文.docx
《JAVA课程设计报告保卫太空小游戏多线程实现毕业设计论文.docx》由会员分享,可在线阅读,更多相关《JAVA课程设计报告保卫太空小游戏多线程实现毕业设计论文.docx(19页珍藏版)》请在冰豆网上搜索。
![JAVA课程设计报告保卫太空小游戏多线程实现毕业设计论文.docx](https://file1.bdocx.com/fileroot1/2022-12/12/e4aed9a8-19ba-484f-a94b-7ded91e15b2e/e4aed9a8-19ba-484f-a94b-7ded91e15b2e1.gif)
JAVA课程设计报告保卫太空小游戏多线程实现毕业设计论文
一:
程序概述:
该程序主体分为两部分一部分是演示两个航天器的对接。
第二个部分是一个游戏,游戏名为《保卫太空》。
该游戏中,我们的航天器在围绕地球转动,而有一些不怀好意的人想要破坏我们的飞行器,试图用导弹将其打下,我们的任务就是保护航天器,拦截敌人发出的导弹,一共有四关,随着关数升高,敌人炮弹的速度将会越来越快,敌人的大炮也会增多。
二:
程序分析:
(1)程序中主要用到的类:
●Continent类:
该类是大陆线程类,控制大陆的移动,形成地球转动的效果。
●View类:
该类是JPanel类的子类,该程序用于画界面,所有的图形全都画在View类的对象中。
●Aircraft类:
该类是飞行器的抽象父类,提供飞行器的一些共有属性,抽象方法等,继承了线程类Thread。
●ShenEight类:
该类是Aircraft的子类,神州八号类,可以绕地球飞行,并且可以与指定的TianGong类对象完成对接任务。
●TianGong类:
该类是Aircraft的子类,天宫类,可以实现绕地球飞行,并且可以在与指定的ShenEight对象对接后控制ShenEight对象一起运动。
●ParentBarbette类:
该类是大炮的抽象父类,提供大炮的一半属性,以及抽象方法。
●ComBarbette类:
该类是ParentBarbette类的子类,并且实现了Rounnable接口,该类对象由线程控制,控制大炮发炮的速度等,攻击围绕地球运动的飞行器。
●PlayerBarbette类:
该类是ParentBarbette类的子类,用于拦截敌人的炮弹。
实现了KeyListener接口,可以响应键盘的空格以及上下左右方向键。
按下空格键,玩家炮发出炮弹,方向键可以移动炮口,使其瞄准目标。
●CountScore类:
该类是记分类,在游戏中,该类可以通过拦截是否成功或者是敌方炮弹是否攻击到飞行器来进行加减分,从而判断是否过关等等。
三:
程序主要类的实现:
(1)Continent类:
publicclassContinentextendsThread{
Viewearth;
Mainlandland1;
Mainlandland2;
publicContinent(Point2D.Doublecen1,Point2D.Doublecen2,Viewear)
{
earth=ear;
land1=newMainland(cen1,ear);
land2=newMainland(cen2,ear);
}
publicvoidrun()
{
while(true)
{
land2.changePos();//平移中心
land1.changePos();//平移中心
earth.repaint();//窗口重绘
try{
sleep(10);
}catch(InterruptedExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
publicvoiddrawMainland(Graphics2Dg)
{
g.setClip(earth.getEarth());//设置剪贴区
ColoroldColor=g.getColor();
g.setColor(newColor(0,150,0));//设置前景色
g.fill(land1.polygon);
g.fill(land2.polygon);
g.setClip(null);//消除剪贴区
g.setColor(oldColor);
}
}
classMainland
{
staticfinalintN=100;
staticfinalintUserRadius=56;
Viewearth;
Point2D.Doublecenter;
intxPos1[]={/*画图坐标,因太多,故省略*/};
intyPos1[]={/*画图坐标,因太多,故省略*/};
intxPos2[]={/*画图坐标,因太多,故省略*/};
intyPos2[]={/*画图坐标,因太多,故省略*/};
publicGeneralPathpolygon;
publicMainland(Point2D.Doublecen,Viewear)
{
center=newPoint2D.Double();
center.x=cen.x;
center.y=cen.y;
earth=ear;
drawPolygon();
}
privatebooleanchangeCoor(int[]x,int[]y,int[]xPos,int[]yPos)
{
if(x.length!
=xPos.length||y.length!
=yPos.length||xPos.length!
=yPos.length)
returnfalse;
for(inti=0;i!
=x.length;++i)
{
x[i]=xPos[i]-322+(int)center.x;
y[i]=yPos[i]-250+(int)center.y;
}
returntrue;
}
publicvoiddrawPolygon()
{
polygon=newGeneralPath();
int[]tmpX1=newint[xPos1.length];
int[]tmpY1=newint[xPos1.length];
int[]tmpX2=newint[xPos2.length];
int[]tmpY2=newint[xPos2.length];
changeCoor(tmpX1,tmpY1,xPos1,yPos1);
changeCoor(tmpX2,tmpY2,xPos2,yPos2);
polygon.moveTo(tmpX1[0],tmpY1[0]);
for(inti=1;i!
=tmpX1.length;++i)
{
polygon.lineTo(tmpX1[i],tmpY1[i]);
}
polygon.closePath();
polygon.moveTo(tmpX2[0],tmpY2[0]);
for(inti=1;i!
=tmpX2.length;++i)
{
polygon.lineTo(tmpX2[i],tmpY2[i]);
}
polygon.closePath();
}
voidchangePos()
{
center.x+=1;
if(center.x>=earth.center.x+2*earth.radius)
center.x=earth.center.x-2*earth.radius;
drawPolygon();
}
}
(2)Aircraft(飞行器抽象父类):
abstractpublicclassAircraftextendsThread{
publicstaticVectorvecAir=newVector();
protectedbooleanbOpen;//判断飞船是否启动
protectedshortwidth;//飞船船身总宽
protectedshortlength;
protectedshortradiusA;//轨迹半长轴
protectedshortradiusB;//轨迹半短轴
protecteddoubleangle;
protecteddoublek;//轨道切线斜率
protecteddoublespeed=Math.PI/700;
protectedPoint2D.Doublecenter=newPoint2D.Double();//飞船中心坐标
protectedPoint2D.Doublefront=newPoint2D.Double();//飞船前部
protectedPoint2D.Doublerear=newPoint2D.Double();//飞船尾部
protectedViewearth;
publicbooleanisOpen()
{returnbOpen;}
publicvoidclose()
{bOpen=false;}
publicbooleanisDisplay()
{
if(angle>=-Math.PI&&angle<0)
returnfalse;
else
returntrue;
}
//////////判断飞行器是否可见,即是否可被攻击
publicbooleanisVisible()
{
if(!
isDisplay())
{
////////////////计算飞船前部到地球中心的距离
doubledis1=Math.sqrt(
Math.pow(front.x-earth.center.x,2)+Math.pow(front.y-earth.center.y,2));
////////////////计算飞船尾部到地球中心的距离
doubledis2=Math.sqrt(
Math.pow(rear.x-earth.center.x,2)+Math.pow(rear.y-earth.center.y,2));
////////////////若飞船前部和尾部距地球都小于圆心,则飞船设置为不可见,此时飞船不能被攻击
if(dis1<=earth.radius&&dis2<=earth.radius)
returnfalse;
}
returntrue;
}
publicdoublegetLength()
{
returnlength;
}
publicdoublegetWidth()
{
returnwidth;
}
publicPoint2D.DoublegetCenter()
{
returncenter;
}
publicbooleanisBumped(Point2D.Doublep)
{
doublem1=k;
doublem2=-1/m1;
doubledisW=Math.abs(m1*p.x-p.y-m1*center.x+center.y)/Math.sqrt(1+m1*m1);
doubledisL=Math.abs(m2*p.x-p.y-m2*center.x+center.y)/Math.sqrt(1+m2*m2);
if(disW<=width/2&&disL<=length/2)
returntrue;
else
returnfalse;
}
publicabstractvoiddrawAircraft(Graphics2Dg);
}
(3)ParentBarbette(大炮抽象父类):
abstractpublicclassParentBarbette{
protectedshortradius;//炮座的半径
protectedshortlength;//炮管长度
protectedshortwidth;//炮管宽度
protecteddoubleangle;//炮管角度
protecteddoublek;//炮管斜率
protectedPoint2D.Doublecenter=newPoint2D.Double();//炮座的中心
protectedEllipse2D.DoubleElliBase;
protectedGeneralPathpolyPipe;
protectedColorcoBase=Color.darkGray;
protectedColorcoBody=Color.YELLOW;
protectedvoiddrawPolygon()
{
doublem1=length/Math.sqrt(1+k*k);
doublem2=width/Math.sqrt(1+1/(k*k));
doublem3=k*m1;
doublem4=-1/k*m2;
intt1=(int)(Math.cos(angle)/Math.abs(Math.cos(angle)));
intt2=(int)(Math.sin(angle)/Math.abs(Math.sin(angle)));
/////////////////////////////////开始绘制炮管
doublexPos[]=newdouble[10];
doubleyPos[]=newdouble[10];
xPos[0]=center.x+t1*0.05*m1-t2*0.7*m2;yPos[0]=center.y+t1*0.05*m3-t2*0.7*m4;
xPos[9]=center.x+t1*0.05*m1+t2*0.7*m2;yPos[9]=center.y+t1*0.05*m3+t2*0.7*m4;
xPos[1]=center.x+t1*0.1*m1-t2*m2;yPos[1]=center.y+t1*0.1*m3-t2*m4;
xPos[8]=center.x+t1*0.1*m1+t2*m2;yPos[8]=center.y+t1*0.1*m3+t2*m4;
xPos[2]=center.x+t1*0.85*m1-t2*m2;yPos[2]=center.y+t1*0.85*m3-t2*m4;
xPos[7]=center.x+t1*0.85*m1+t2*m2;yPos[7]=center.y+t1*0.85*m3+t2*m4;
xPos[3]=center.x+t1*0.9*m1-t2*1.1*m2;yPos[3]=center.y+t1*0.9*m3-t2*1.1*m4;
xPos[6]=center.x+t1*0.9*m1+t2*1.1*m2;yPos[6]=center.y+t1*0.9*m3+t2*1.1*m4;
xPos[4]=center.x+t1*m1-t2*1.1*m2;yPos[4]=center.y+t1*m3-t2*1.1*m4;
xPos[5]=center.x+t1*m1+t2*1.1*m2;yPos[5]=center.y+t1*m3+t2*1.1*m4;
polyPipe=newGeneralPath();
polyPipe.moveTo(xPos[0],yPos[0]);
for(inti=1;i!
=xPos.length;++i)
{
polyPipe.lineTo(xPos[i],yPos[i]);
}
///////////////////大炮底座
ElliBase=newEllipse2D.Double(center.x-radius,center.y-radius,2*radius,2*radius);
}
publicvoiddrawBarbette(Graphics2Dg)
{
ColoroldColor=g.getColor();
g.setColor(coBase);
g.fill(ElliBase);
g.setColor(coBody);
g.fill(polyPipe);
g.setColor(oldColor);
}
abstractprotectedvoidproBullet();//抽象函数产生炮弹
}
(4)ParentBullet(炮弹抽象父类)
publicabstractclassParentBulletextendsThread{
protectedbooleanb_thread=true;
protectedshortlength;//炮弹长度
protectedshortwidth;//炮弹宽度
protectedshortsleepTime;//线程睡眠时间
protecteddoublek;//炮弹的斜率
protecteddoubleangle;//炮弹的倾角
protectedPoint2D.Doubletarget;//攻击目标位置
protectedPoint2D.Doublecenter;//炮弹的中心
protectedPoint2D.Doublefront=newPoint2D.Double();//弹头坐标
protectedGeneralPathpolyBody;//炮弹身体部分
protectedGeneralPathpolyHead;//炮弹头部
protectedGeneralPathpolyWine;//炮弹尾翼
protectedColorcoBody=newColor(50,200,0);
protectedColorcoHead=newColor(200,0,0);
protectedColorcoWine=newColor(200,180,190);
protectedViewview;
publicParentBullet(Viewv,Point2D.Doublecen,intlen,intwid,doublesita)
{///////////////////////////构造函数中的cen是发射炮塔的旋转中心
view=v;
angle=sita;
width=(short)wid;
length=(short)len;
k=Math.tan(angle);
sleepTime=10;
doublem1=length/Math.sqrt(1+k*k);
doublem3=k*m1;
intt1=(int)(Math.cos(angle)/Math.abs(Math.cos(angle)));
center=newPoint2D.Double();
////////////////////////////////计算炮弹初始位置
center.x=cen.x+t1*0.7*m1;center.y=cen.y+t1*0.7*m3;
movePos(0);
}
publicvoiddrawBullet(Graphics2Dg)//画出炮弹
{
ColoroldColor=g.getColor();
g.setColor(coHead);//弹头颜色
g.fill(polyHead);
g.setColor(coWine);//尾翼颜色
g.fill(polyWine);
g.setColor(coBody);//弹体颜色
g.fill(polyBody);
g.setColor(oldColor);
}
protectedvoidmovePos(doubledis)
{
doublem1=dis/Math.sqrt(1+k*k);
doublem3=k*m1;
intt1=(int)(Math.cos(angle)/Math.abs(Math.cos(angle)));
center.x=center.x+t1*m1;center.y=center.y+t1*m3;
drawPolygon();
}
protectedvoiddrawPolygon()
{
doublem1=length/Math.sqrt(1+k*k);
doublem2=width/Math.sqrt(1+1/(k*k));
doublem3=k*m1;
doublem4=-1/k*m2;
intt1=(int)(Math.cos(angle)/Math.abs(Math.cos(angle)));
intt2=(int)(Math.sin(angle)/Math.abs(Math.sin(angle)));