JAVA课程设计报告-保卫太空小游戏.doc_第1页
JAVA课程设计报告-保卫太空小游戏.doc_第2页
JAVA课程设计报告-保卫太空小游戏.doc_第3页
JAVA课程设计报告-保卫太空小游戏.doc_第4页
JAVA课程设计报告-保卫太空小游戏.doc_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

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

文档简介

Java课程设计 -多线程实现 姓名: 学号:2402100211 班号:24021002 专业:计算机科学与技术 一:程序概述:该程序主体分为两部分一部分是演示两个航天器的对接。第二个部分是一个游戏,游戏名为保卫太空。该游戏中,我们的航天器在围绕地球转动,而有一些不怀好意的人想要破坏我们的飞行器,试图用导弹将其打下,我们的任务就是保护航天器,拦截敌人发出的导弹,一共有四关,随着关数升高,敌人炮弹的速度将会越来越快,敌人的大炮也会增多。二:程序分析:(1) 程序中主要用到的类:l Continent类:该类是大陆线程类,控制大陆的移动,形成地球转动的效果。l View类:该类是JPanel类的子类,该程序用于画界面,所有的图形全都画在View类的对象中。l Aircraft类:该类是飞行器的抽象父类,提供飞行器的一些共有属性,抽象方法等,继承了线程类Thread。l ShenEight类:该类是Aircraft的子类,神州八号类,可以绕地球飞行,并且可以与指定的TianGong类对象完成对接任务。l TianGong类:该类是Aircraft的子类,天宫类,可以实现绕地球飞行,并且可以在与指定的ShenEight对象对接后控制ShenEight对象一起运动。l ParentBarbette类:该类是大炮的抽象父类,提供大炮的一半属性,以及抽象方法。l ComBarbette类:该类是ParentBarbette类的子类,并且实现了Rounnable接口,该类对象由线程控制,控制大炮发炮的速度等,攻击围绕地球运动的飞行器。l PlayerBarbette类:该类是ParentBarbette类的子类,用于拦截敌人的炮弹。实现了KeyListener接口,可以响应键盘的空格以及上下左右方向键。按下空格键,玩家炮发出炮弹,方向键可以移动炮口,使其瞄准目标。l CountScore类: 该类是记分类,在游戏中,该类可以通过拦截是否成功或者是敌方炮弹是否攻击到飞行器来进行加减分,从而判断是否过关等等。三:程序主要类的实现:(1)Continent类:public class Continent extends Thread View earth;Mainland land1; Mainland land2;public Continent( Point2D.Double cen1, Point2D.Double cen2 , View ear )earth = ear;land1 = new Mainland( cen1, ear );land2 = new Mainland( cen2, ear );public void run()while(true) land2.changePos(); /平移中心 land1.changePos(); /平移中心 earth.repaint(); /窗口重绘 try sleep(10); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace(); public void drawMainland( Graphics2D g )g.setClip( earth.getEarth() ); /设置剪贴区 Color oldColor = g.getColor(); g.setColor( new Color(0, 150, 0) ); /设置前景色 g.fill(land1.polygon); g.fill(land2.polygon); g.setClip(null); /消除剪贴区 g.setColor(oldColor);class Mainlandstatic final int N = 100;static final int UserRadius = 56;View earth;Point2D.Double center;int xPos1 = /*画图坐标,因太多,故省略*/;int yPos1 = /*画图坐标,因太多,故省略*/;int xPos2 = /*画图坐标,因太多,故省略*/;int yPos2 = /*画图坐标,因太多,故省略*/;public GeneralPath polygon;public Mainland( Point2D.Double cen , View ear )center = new Point2D.Double();center.x = cen.x;center.y = cen.y;earth = ear;drawPolygon();private boolean changeCoor( int x, int y ,int xPos, int yPos)if( x.length != xPos.length | y.length != yPos.length | xPos.length != yPos.length)return false;for(int i=0; i!= x.length; +i) xi = xPosi - 322 + (int)center.x; yi = yPosi - 250 + (int)center.y;return true;public void drawPolygon()polygon = new GeneralPath();int tmpX1 = new intxPos1.length;int tmpY1 = new intxPos1.length;int tmpX2 = new intxPos2.length;int tmpY2 = new intxPos2.length;changeCoor(tmpX1, tmpY1, xPos1, yPos1);changeCoor(tmpX2, tmpY2, xPos2, yPos2);polygon.moveTo(tmpX10, tmpY10);for(int i = 1; i!= tmpX1.length; +i)polygon.lineTo(tmpX1i, tmpY1i);polygon.closePath();polygon.moveTo(tmpX20, tmpY20);for(int i = 1; i!= tmpX2.length; +i)polygon.lineTo(tmpX2i, tmpY2i);polygon.closePath();void changePos()center.x += 1;if( center.x = earth.center.x + 2*earth.radius )center.x = earth.center.x - 2*earth.radius;drawPolygon();(2)Aircraft(飞行器抽象父类):abstract public class Aircraft extends Threadpublic static Vector vecAir = new Vector();protected boolean bOpen; /判断飞船是否启动protected short width; /飞船船身总宽protected short length;protected short radiusA; /轨迹半长轴protected short radiusB; /轨迹半短轴protected double angle;protected double k; /轨道切线斜率 protected double speed = Math.PI/700;protected Point2D.Double center = new Point2D.Double(); /飞船中心坐标 protected Point2D.Double front = new Point2D.Double(); /飞船前部protected Point2D.Double rear = new Point2D.Double(); /飞船尾部protected View earth;public boolean isOpen() return bOpen; public void close() bOpen = false; public boolean isDisplay() if( angle= -Math.PI & angle 0 ) return false; else return true; /判断飞行器是否可见 , 即是否可被攻击public boolean isVisible()if( !isDisplay() )/计算飞船前部到地球中心的距离double dis1 = Math.sqrt( Math.pow( front.x - earth.center.x , 2) + Math.pow( front.y - earth.center.y, 2) );/计算飞船尾部到地球中心的距离double dis2 = 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 )return false;return true;public double getLength() return length; public double getWidth() return width; public Point2D.Double getCenter() return center; public boolean isBumped( Point2D.Double p ) double m1 = k; double m2 = -1/m1; double disW = Math.abs( m1*p.x - p.y - m1*center.x + center.y ) / Math.sqrt( 1 + m1*m1 ); double disL = Math.abs( m2*p.x - p.y - m2*center.x + center.y ) / Math.sqrt( 1 + m2*m2 ); if( disW = width/2 & disL = length/2 ) return true; else return false; public abstract void drawAircraft( Graphics2D g );(3)ParentBarbette(大炮抽象父类):abstract public class ParentBarbette protected short radius; /炮座的半径protected short length; /炮管长度 protected short width; /炮管宽度protected double angle; /炮管角度 protected double k; /炮管斜率protected Point2D.Double center = new Point2D.Double(); /炮座的中心protected Ellipse2D.Double ElliBase; protected GeneralPath polyPipe;protected Color coBase = Color.darkGray;protected Color coBody = Color.YELLOW;protected void drawPolygon()double m1 = length / Math.sqrt( 1 + k*k );double m2 = width / Math.sqrt( 1 + 1/(k*k) );double m3 = k * m1;double m4 = -1/k * m2;int t1 = (int)( Math.cos(angle)/Math.abs( Math.cos(angle) ) );int t2 = (int)( Math.sin(angle)/Math.abs( Math.sin(angle) ) );/开始绘制炮管double xPos = new double10;double yPos = new double10;xPos0 = center.x + t1 * 0.05*m1 - t2 * 0.7*m2; yPos0 = center.y + t1 * 0.05*m3 - t2 * 0.7*m4;xPos9 = center.x + t1 * 0.05*m1 + t2 * 0.7*m2; yPos9 = center.y + t1 * 0.05*m3 + t2 * 0.7*m4;xPos1 = center.x + t1 * 0.1*m1 - t2 * m2; yPos1 = center.y + t1 * 0.1*m3 - t2 * m4;xPos8 = center.x + t1 * 0.1*m1 + t2 * m2; yPos8 = center.y + t1 * 0.1*m3 + t2 * m4;xPos2 = center.x + t1 * 0.85*m1 - t2 * m2; yPos2 = center.y + t1 * 0.85*m3 - t2 * m4;xPos7 = center.x + t1 * 0.85*m1 + t2 * m2; yPos7 = center.y + t1 * 0.85*m3 + t2 * m4;xPos3 = center.x + t1 * 0.9*m1 - t2 * 1.1*m2; yPos3 = center.y + t1 * 0.9*m3 - t2 * 1.1*m4;xPos6 = center.x + t1 * 0.9*m1 + t2 * 1.1*m2; yPos6 = center.y + t1 * 0.9*m3 + t2 * 1.1*m4;xPos4 = center.x + t1 * m1 - t2 * 1.1*m2; yPos4 = center.y + t1 * m3 - t2 * 1.1*m4;xPos5 = center.x + t1 * m1 + t2 * 1.1*m2; yPos5 = center.y + t1 * m3 + t2 * 1.1*m4;polyPipe = new GeneralPath();polyPipe.moveTo(xPos0, yPos0);for( int i = 1; i != xPos.length; +i )polyPipe.lineTo(xPosi, yPosi);/大炮底座 ElliBase = new Ellipse2D.Double( center.x - radius, center.y - radius, 2*radius, 2*radius );public void drawBarbette( Graphics2D g )Color oldColor = g.getColor();g.setColor( coBase );g.fill(ElliBase);g.setColor( coBody );g.fill(polyPipe);g.setColor(oldColor);abstract protected void proBullet(); /抽象函数产生炮弹(4)ParentBullet(炮弹抽象父类)public abstract class ParentBullet extends Thread protected boolean b_thread = true;protected short length; /炮弹长度protected short width; /炮弹宽度protected short sleepTime; /线程睡眠时间protected double k; /炮弹的斜率protected double angle; /炮弹的倾角protected Point2D.Double target; /攻击目标位置protected Point2D.Double center; /炮弹的中心protected Point2D.Double front = new Point2D.Double(); /弹头坐标 protected GeneralPath polyBody; /炮弹身体部分protected GeneralPath polyHead; /炮弹头部protected GeneralPath polyWine; /炮弹尾翼 protected Color coBody = new Color(50,200,0);protected Color coHead = new Color(200,0,0);protected Color coWine = new Color(200,180,190);protected View view;public ParentBullet( View v, Point2D.Double cen, int len, int wid, double sita) /构造函数中的cen是发射炮塔的旋转中心view = v;angle = sita;width = (short)wid;length = (short)len;k = Math.tan(angle);sleepTime = 10;double m1 = length / Math.sqrt( 1 + k*k );double m3 = k * m1;int t1 = (int)( Math.cos(angle)/Math.abs( Math.cos(angle) ) );center = new Point2D.Double(); /计算炮弹初始位置center.x = cen.x + t1 * 0.7*m1; center.y = cen.y + t1 * 0.7*m3; movePos( 0 );public void drawBullet( Graphics2D g ) /画出炮弹 Color oldColor = g.getColor();g.setColor( coHead ); /弹头颜色g.fill(polyHead);g.setColor( coWine ); /尾翼颜色g.fill(polyWine);g.setColor( coBody ); /弹体颜色g.fill(polyBody);g.setColor(oldColor);protected void movePos( double dis )double m1 = dis / Math.sqrt( 1 + k*k );double m3 = k * m1;int t1 = (int)( Math.cos(angle)/Math.abs( Math.cos(angle) ) );center.x = center.x + t1 * m1; center.y = center.y + t1 * m3; drawPolygon();protected void drawPolygon()double m1 = length / Math.sqrt( 1 + k*k );double m2 = width / Math.sqrt( 1 + 1/(k*k) );double m3 = k * m1;double m4 = -1/k * m2;int t1 = (int)( Math.cos(angle)/Math.abs( Math.cos(angle) ) );int t2 = (int)( Math.sin(angle)/Math.abs( Math.sin(angle) ) );/存储弹体坐标double xPos1 = new double8;double yPos1 = new double8;/存储弹头坐标double xPos2 = new double5;double yPos2 = new double5;/存储尾翼坐标double xPos3 = new double8;double yPos3 = new double8;/弹体坐标上xPos10 = center.x + t1 * m1/2 - t2 * 0.6*m2/2; yPos10 = center.y + t1 * m3/2 - t2 * 0.6*m4/2;xPos17 = center.x + t1 * m1/2 + t2 * 0.6*m2/2; yPos17 = center.y + t1 * m3/2 + t2 * 0.6*m4/2;xPos11 = center.x + t1 * 1.1*m1/2 - t2 * m2/2; yPos11 = center.y + t1 * 1.1*m3/2 - t2 * m4/2;xPos16 = center.x + t1 * 1.1*m1/2 + t2 * m2/2; yPos16 = center.y + t1 * 1.1*m3/2 + t2 * m4/2;/记录头部坐标xPos20 = xPos11; yPos20 = yPos11;xPos21 = xPos10; yPos21 = yPos10;xPos22 = xPos17; yPos22 = yPos17;xPos23 = xPos16; yPos23 = yPos16;xPos24 = center.x + t1 * 1.5*m1/2; yPos24 = center.y + t1 * 1.5*m3/2;front.x = xPos24;front.y = yPos24;/弹体坐标下xPos12 = center.x - t1 * 0.9*m1/2 - t2 * m2/2; yPos12 = center.y - t1 * 0.9*m3/2 - t2 * m4/2;xPos15 = center.x - t1 * 0.9*m1/2 + t2 * m2/2; yPos15 = center.y - t1 * 0.9*m3/2 + t2 * m4/2;xPos13 = center.x - t1 * m1/2 - t2 * 0.6*m2/2; yPos13 = center.y - t1 * m3/2 - t2 * 0.6*m4/2;xPos14 = center.x - t1 * m1/2 + t2 * 0.6*m2/2; yPos14 = center.y - t1 * m3/2 + t2 * 0.6*m4/2;/画尾翼xPos30 = center.x - t1 * 0.2*m1/2 - t2 * m2/2; yPos30 = center.y - t1 * 0.2*m3/2 - t2 * m4/2;xPos34 = center.x - t1 * 0.2*m1/2 + t2 * m2/2; yPos34 = center.y - t1 * 0.2*m3/2 + t2 * m4/2;xPos31 = center.x - t1 * 0.3*m1/2 - t2 * 1.8*m2/2; yPos31 = center.y - t1 * 0.3*m3/2 - t2 * 1.8*m4/2;xPos35 = center.x - t1 * 0.3*m1/2 + t2 * 1.8*m2/2; yPos35 = center.y - t1 * 0.3*m3/2 + t2 * 1.8*m4/2;xPos32 = center.x - t1 * 0.9*m1/2 - t2 * 1.8*m2/2; yPos32 = center.y - t1 * 0.9*m3/2 - t2 * 1.8*m4/2;xPos36 = center.x - t1 * 0.9*m1/2 + t2 * 1.8*m2/2; yPos36 = center.y - t1 * 0.9*m3/2 + t2 * 1.8*m4/2;xPos33 = center.x - t1 * 0.9*m1/2 - t2 * m2/2; yPos33 = center.y - t1 * 0.9*m3/2 - t2 * m4/2;xPos37 = center.x - t1 * 0.9*m1/2 + t2 * m2/2; yPos37 = center.y - t1 * 0.9*m3/2 + t2 * m4/2;/画出炮弹各个部分polyBody = new GeneralPath();polyBody.moveTo(xPos10, yPos10);for( int i = 1; i != xPos1.length; +i )polyBody.lineTo(xPos1i, yPos1i);polyHead = new GeneralPath();polyHead.moveTo(xPos20, yPos20);for( int i = 1; i != xPos2.length; +i )polyHead.lineTo(xPos2i, yPos2i);polyWine = new GeneralPath();polyWine.moveTo(xPos30, yPos30);f

温馨提示

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

评论

0/150

提交评论