




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、Java課程設(shè)計(jì) -多線程實(shí)現(xiàn) 姓名: 學(xué)號:2402100211 班號:24021002 專業(yè):計(jì)算機(jī)科學(xué)與技術(shù) 一:程序概述:該程序主體分為兩部分一部分是演示兩個(gè)航天器的對接。第二個(gè)部分是一個(gè)游戲,游戲名為保衛(wèi)太空。該游戲中,我們的航天器在圍繞地球轉(zhuǎn)動,而有一些不懷好意的人想要破壞我們的飛行器,試圖用導(dǎo)彈將其打下,我們的任務(wù)就是保護(hù)航天器,攔截?cái)橙税l(fā)出的導(dǎo)彈,一共有四關(guān),隨著關(guān)數(shù)升高,敵人炮彈的速度將會越來越快,敵人的大炮也會增多。二:程序分析:(1) 程序中主要用到的類:l Continent類:該類是大陸線程類,控制大陸的移動,形成地球轉(zhuǎn)動的效果。l View類:該類是JPanel類的
2、子類,該程序用于畫界面,所有的圖形全都畫在View類的對象中。l Aircraft類:該類是飛行器的抽象父類,提供飛行器的一些共有屬性,抽象方法等,繼承了線程類Thread。l ShenEight類:該類是Aircraft的子類,神州八號類,可以繞地球飛行,并且可以與指定的TianGong類對象完成對接任務(wù)。l TianGong類:該類是Aircraft的子類,天宮類,可以實(shí)現(xiàn)繞地球飛行,并且可以在與指定的ShenEight對象對接后控制ShenEight對象一起運(yùn)動。l ParentBarbette類:該類是大炮的抽象父類,提供大炮的一半屬性,以及抽象方法。l ComBarbette類:該類
3、是ParentBarbette類的子類,并且實(shí)現(xiàn)了Rounnable接口,該類對象由線程控制,控制大炮發(fā)炮的速度等,攻擊圍繞地球運(yùn)動的飛行器。l PlayerBarbette類:該類是ParentBarbette類的子類,用于攔截?cái)橙说呐趶?。?shí)現(xiàn)了KeyListener接口,可以響應(yīng)鍵盤的空格以及上下左右方向鍵。按下空格鍵,玩家炮發(fā)出炮彈,方向鍵可以移動炮口,使其瞄準(zhǔn)目標(biāo)。l CountScore類: 該類是記分類,在游戲中,該類可以通過攔截是否成功或者是敵方炮彈是否攻擊到飛行器來進(jìn)行加減分,從而判斷是否過關(guān)等等。三:程序主要類的實(shí)現(xiàn):(1)Continent類:public class Co
4、ntinent 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
5、.repaint(); /窗口重繪 try sleep(10); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace(); public void drawMainland( Graphics2D g )g.setClip( earth.getEarth() ); /設(shè)置剪貼區(qū) Color oldColor = g.getColor(); g.setColor( new Color(0, 150, 0) ); /設(shè)置前景色 g.fill(land1.polygon); g.fill(
6、land2.polygon); g.setClip(null); /消除剪貼區(qū) g.setColor(oldColor);class Mainlandstatic final int N = 100;static final int UserRadius = 56;View earth;Point2D.Double center;int xPos1 = /*畫圖坐標(biāo),因太多,故省略*/;int yPos1 = /*畫圖坐標(biāo),因太多,故省略*/;int xPos2 = /*畫圖坐標(biāo),因太多,故省略*/;int yPos2 = /*畫圖坐標(biāo),因太多,故省略*/;public GeneralPath
7、 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 fa
8、lse;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;chang
9、eCoor(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.clo
10、sePath();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< Aircraft > vecAir = new Vector< Aircraft >();protect
11、ed 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(); /飛船中心坐標(biāo) protec
12、ted 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; el
13、se return true; /判斷飛行器是否可見 , 即是否可被攻擊public boolean isVisible()if( !isDisplay() )/計(jì)算飛船前部到地球中心的距離double dis1 = Math.sqrt( Math.pow( front.x - earth.center.x , 2) + Math.pow( front.y - earth.center.y, 2) );/計(jì)算飛船尾部到地球中心的距離double dis2 = Math.sqrt( Math.pow( rear.x - earth.center.x , 2) + Math.pow( rear.y
14、 - earth.center.y, 2) );/若飛船前部和尾部距地球都小于圓心,則飛船設(shè)置為不可見,此時(shí)飛船不能被攻擊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 isBump
15、ed( 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
16、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.D
17、ouble 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
18、) );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 *
19、 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 +
20、 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 = cen
21、ter.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 )pol
22、yPipe.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
23、 protected void proBullet(); /抽象函數(shù)產(chǎn)生炮彈(4)ParentBullet(炮彈抽象父類)public abstract class ParentBullet extends Thread protected boolean b_thread = true;protected short length; /炮彈長度protected short width; /炮彈寬度protected short sleepTime; /線程睡眠時(shí)間protected double k; /炮彈的斜率protected double angle; /炮彈的傾角protecte
24、d Point2D.Double target; /攻擊目標(biāo)位置protected Point2D.Double center; /炮彈的中心protected Point2D.Double front = new Point2D.Double(); /彈頭坐標(biāo) protected GeneralPath polyBody; /炮彈身體部分protected GeneralPath polyHead; /炮彈頭部protected GeneralPath polyWine; /炮彈尾翼 protected Color coBody = new Color(50,200,0);protected
25、 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) /構(gòu)造函數(shù)中的cen是發(fā)射炮塔的旋轉(zhuǎn)中心view = v;angle = sita;width = (short)wid;length = (short)len;k = Math.tan(angle);sleepTime = 10;d
26、ouble 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(); /計(jì)算炮彈初始位置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 =
27、 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.ab
28、s( 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( Mat
29、h.cos(angle) ) );int t2 = (int)( Math.sin(angle)/Math.abs( Math.sin(angle) ) );/存儲彈體坐標(biāo)double xPos1 = new double8;double yPos1 = new double8;/存儲彈頭坐標(biāo)double xPos2 = new double5;double yPos2 = new double5;/存儲尾翼坐標(biāo)double xPos3 = new double8;double yPos3 = new double8;/彈體坐標(biāo)上xPos10 = center.x + t1 * m1/2 -
30、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
31、 = center.y + t1 * 1.1*m3/2 + t2 * m4/2;/記錄頭部坐標(biāo)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;/彈體坐標(biāo)下xPos12 = cen
32、ter.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 + t
33、2 * 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;
34、 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
35、/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;/畫出炮彈各個(gè)部分polyBody = new GeneralPath();polyBody.moveTo(xPos10
36、, 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);for( int i = 1; i != xPos3.lengt
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025-2030年中國汽車用制動項(xiàng)目投資可行性研究分析報(bào)告
- 2024-2030全球快干稀釋劑行業(yè)調(diào)研及趨勢分析報(bào)告
- 軟木及軟木制品項(xiàng)目風(fēng)險(xiǎn)識別與評估綜合報(bào)告
- 2025年體育健身場地租賃合同
- 2025年服裝店長雇傭合同樣本
- 2025年住宅銷售場所裝飾裝修合同樣本
- 2025年企業(yè)雇傭合同與員工權(quán)益
- 2025年期內(nèi)汽車租賃調(diào)度合同
- 2025年個(gè)體工商戶貸款知識產(chǎn)權(quán)抵押合同樣本
- 從動手做中體驗(yàn)增強(qiáng)孩子的操作實(shí)踐能力研究報(bào)告
- GB/T 4706.13-2024家用和類似用途電器的安全第13部分:制冷器具、冰淇淋機(jī)和制冰機(jī)的特殊要求
- 人教版(2024年新教材)九年級上冊化學(xué)第一單元達(dá)標(biāo)測試卷
- AQ 1044-2007 礦井密閉防滅火技術(shù)規(guī)范(正式版)
- 光伏項(xiàng)目施工總進(jìn)度計(jì)劃表(含三級)
- 施工現(xiàn)場建筑垃圾減量化專項(xiàng)方案
- 《平面向量的坐標(biāo)運(yùn)算(平行與垂直)》專題精講課件
- 陶土瓦屋面施工施工方法及工藝要求
- 第三課 多彩的鉛筆 教案 五下信息科技河南大學(xué)版
- 18《文言文二則:鐵杵成針》(教學(xué)設(shè)計(jì))2023-2024學(xué)年統(tǒng)編版語文四年級下冊
- 河南省創(chuàng)新發(fā)展聯(lián)盟2023-2024學(xué)年高一下學(xué)期3月月考化學(xué)試題(解析版)
- 機(jī)械錨栓檢測報(bào)告
評論
0/150
提交評論