


版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、坦克大戰(zhàn)java源代碼有些圖片路徑會出錯要注意package com.ta nkgame;import java.util.Vector;坦克類class Tankint x=0;int y=0;int color=0;int speed=1;int direct=0;boolea n isLive=true;public Tan k(i nt x,i nt y)this.x=x;this.y=y;public int getX() return x;public void setX(i nt x) this.x = x;public int get Y() return y;public v
2、oid set Y(i nt y) this.y = y;public int getDirect() return direct;public void setDirect(i nt direct) this.direct = direct;public int getColor() retur n color;public void setColor(i nt color) this.color = color;我的坦克class Hero exte nds TankShot shot=n ull;Vector<Shot> shotm=new Vector<Shot>
3、;(); public Hero(i nt x,i nt y)super(x,y);this.color=5;/坦克具有一個打擊敵人的方法public void shote nemy(i nt x,i nt y,i nt direct)switch(direct)case 0:shot=new Shot(this.x+10,this.y,0); shotm.add(shot);break;case 1:shot=new Shot(this.x+30,this.y+10,1); shotm.add(shot);break;case 2:shot=new Shot(this.x+10,this.y
4、+30,2); shotm.add(shot);break;case 3:shot=new Shot(this.x,this.y+10,3); shotm.add(shot);break;Thread th=new Thread(shot); th.start();/調(diào)整速度public void moveup()y-=speed;public void moveright()x+=speed;public void movedow n()y+=speed;public void moveleft()x-=speed;敵人的坦克class EnemyTank extends Tank impl
5、ementsRunn ableVector<Shot>e nsh=new Vector<Shot>();VectorvE nemyTa nk>ets=newVector<E nemyTa nk>();public En emyTa nk(i nt x, i nt y)super(x, y);this.setColor(2);this.setDirect(2);/獲取MPanel上的敵人坦克public void setets(Vector<E nemyTa nk> vv)this.ets=vv;/判斷敵人的坦克是否碰撞public bool
6、ea n isTouch()boolea n b=false;En emyTa nk et=nu II; switch(direct)case 0:for(i nt i=O;i<ets.size();i+)et=ets.get(i);if(et!=this) if(et.direct=0|et.direct=2) if(this.x>=et.x&&this.x<=et.x+20&&this.y<=e t.y+3 0&&this.y>et.y)return true;if(this.x+20>=et.x&
7、&this.x+20<=et.x+20&&t his.yv=et.y+30&&this.y>et.y)return true;if(et.direct=1|et.direct=3)if(this.x>=et.x&&this.x<=et.x+30&&this.y<=et.y+2 0&&this.y>et.y)return true;if(this.x+20>=et.x&&this.x+20<=et.x+30&&this.y<
8、=et.y+20&&this.y>et.y)return true;break;case 1:for(i nt i=O;i<ets.size();i+)et=ets.get(i);if(et!=this) if(et.direct=0|et.direct=2)if(this.x+30>=et.x&&this.x+30<=et.x+20&&t his.yv=et.y+30&&this.y>et.y)if(this.x+30>=et.x&&this.x+30<=et.x+20&
9、amp;&this.y+20<=et.y+30&&this.y+20>et.y)return true;if(et.direct=1|et.direct=3)if(this.x+30>=et.x&&this.x+30<=et.x+30&&this.y<=et.y+20&&this.y>et.y)retur n true;if(this.x+30>=et.x&&this.x+30<=et.x+30&&this.y+20<=et.y+20&a
10、mp;&this.y+20>et.y)break;case 2:for(i nt i=O;i<ets.size();i+)et=ets.get(i);if(et!=this) if(et.direct=0|et.direct=2)if(this.x>=et.x&&this.x<=et.x+20&&this.y+30 v=et.y+30&&this.y+30>et.y)retur n true;if(this.x+20>=et.x&&this.x+20<=et.x+20&&a
11、mp;t his.y+30<=et.y+30&&this.y+30>et.y)return true; if(et.direct=1|et.direct=3)if(this.x>=et.x&&this.x<=et.x+30&&this.y+30 <=et.y+20&&this.y+30>et.y)retur n true;if(this.x+20>=et.x&&this.x+20<=et.x+30&&t his.y+30<=et.y+20&
12、;&this.y+30>et.y)retur n true;break;case 3:for(i nt i=O;i<ets.size();i+)et=ets.get(i);if(et!=this) if(et.direct=0|et.direct=2)if(this.x+30>=et.x&&this.x+30<=et.x+20&&t his.yv=et.y+30&&this.y>et.y) retur n true;if(this.x+30>=et.x&&this.x+30<=et
13、.x+20&&t his.y+20<=et.y+30&&this.y+20>et.y)retur n true; if(et.direct=1|et.direct=3)if(this.x+30>=et.x&&this.x+30<=et.x+30&&t his.yv=et.y+20&&this.y>et.y) retur n true;if(this.x+30>=et.x&&this.x+30<=et.x+30&&t his.y+20<=
14、et.y+20&&this.y+20>et.y)retur n true;break;return b;public void run() while(true)switch(this.direct)case 0:for(int i=0;i<30;i+)if(y> 0&&this.isTouch()=false)y-=this.speed;try Thread.sleep(50); catch (In terruptedExcepti on e) / TODO Auto-generated catch blocke.pri ntStackTra
15、ce();break;case 1:for(int i=0;i<30;i+)if(x<365&&this.isTouch()=false)x+=this.spee d;try Thread.sleep(50); catch (In terruptedExcepti on e) / TODO Auto-generated catch blocke.pri ntStackTrace();break;case 2:for(int i=0;i<30;i+)if(y<270&&this.isTouch()=false)y+=this.spee tr
16、y Thread.sleep(50); catch (In terruptedExcepti on e) / TODO Auto-generated catch blocke.pri ntStackTrace();break;case 3:for(int i=0;i<30;i+)if(x> 0&&this.isTouch()=false)x-=this.speed;try Thread.sleep(50); catch (In terruptedExcepti on e) / TODO Auto-generated catch blocke.pri ntStackT
17、race();break;this.direct=(i nt)(Math.ra ndom()*4);if(this.isLive=false) break;if(ensh.size()<5)Shot es=n ull;switch(this.direct)case 0:es=newShot(this.getX()+10,this.get Y(),0);en sh.add(es);break;case 1:es=newShot(this.getX()+30,this.get Y( )+10,1); en sh.add(es);break;case 2:es=newShot(this.get
18、X()+10,this.get Y( )+30,2); en sh.add(es);break;case 3:es=newShot(this.getX(),this.get Y( )+10,3);en sh.add(es); break;Thread th=new Thread(es); th.start();炸彈類class Bombint x;int y;int lift=9;boolea n isLive=true;public Bomb(i nt x,i nt y) this.x=x;this.y=y;/炸彈的生命值public void liftdow n()if(lift>0
19、)lift-;elseisLive=false;子彈類class Shot impleme nts Runn ableint shotX;int shot Y;int direct;int shotspeed=1;boolea n isLive=true;public Shot(i nt x,i nt y,i nt direct) this.shotX=x; this.shot Y=y; this.direct=direct;public int getShotX() return shotX;public void setShotX(int shotX) this.shotX = shotX
20、;public int getShot Y() return shot Y;public void setShotY(int shotY) this.shotY = shot Y;public int getShotspeed() retur n shotspeed;public void setShotspeed(i nt shotspeed) this.shotspeed = shotspeed;public void run()while(true)try Thread.sleep(50); catch (In terruptedExcepti on e) / TODO Auto-ge
21、nerated catch block e.pri ntStackTrace();switch(direct)case 0:shot Y-=shotspeed;break;case 1:shotX+=shotspeed; break;case 2:shot Y+=shotspeed; break;case 3:shotX-=shotspeed; break;if(shotX<0|shotX>400|shot Y<0|shotY >300)isLive=false; break;*功能:坦克大戰(zhàn)4.0*/ package com.ta nkgame4;import jav
22、a.awt.*;import java.awt.eve nt.Acti on Eve nt;import java.awt.eve nt.Actio nListe ner;import java.awt.eve nt.KeyEve nt;import java.awt.eve nt.KeyListe ner;import java.util.Vector;import java.io.*;import javax.swi ng.*;JFrameimport javax.imageio.*;public class MyTankGame4 extends impleme nts Actio nL
23、iste nerMyPa nel mp=nu II;MyStartPa nel msp=n ull;菜單定義JMe nuBar jmb=nu II;JMe nu jm仁n ull;JMe nultem jmi1= null;public static void main( Stri ng args) / TODO Auto-ge nerated method stubMyTa nkGame4mtg=newMyTa nkGame4();/構(gòu)造函數(shù)public MyTa nkGame4()創(chuàng)建菜單jmb=new JMe nuBar();jm仁new JMe nu("Game(G)&quo
24、t;);jm1.setM nemo nic('G');jmi 仁new JMe nultem("New Game(N)"); jmi1.setM nemo nic('N');jmil.addActio nListe ner(this); jmil.setActio nComma nd("New Game");jml.add(jmil);jmb.add(jml);this.setJMe nuBar(jmb);msp=new MyStartPa nel();Thread st=new Thread(msp);st.start(
25、);this.add(msp);this.setTitle("坦克大戰(zhàn)"); this.setSize(400, 300);this.setDefaultCloseOperati on( JFrame.EXIT _ON_CLOSE);this.setVisible(true);public void actio nPeformed(Actio nEve nt argO) if(arg0.getActio nComma nd().equals("New Game") this.remove(msp); mp=new MyPa nel();Thread mt
26、=new Thread(mp); mt.start();this.add(mp); this.addKeyListe ner(mp); this.setVisible(true);游戲開始面板class MyStartPanel extends JPanel implementsRunn ableint times=O;public void pain t(Graphics g)super.pa in t(g);g.fillRect(0, 0, 400, 300);if(times%2=0)Font myfont=newFon t("行 楷",Fo nt.BOLD,30);
27、g.setF on t(myfo nt);g.setColor(Color.yellow);g.drawString("第一關(guān)", 130, 130);this.repai nt();public void run() while(true)try Thread.sleep(300); times+; catch (In terruptedExcepti on e) / TODO Auto-ge nerated catch block e.pri ntStackTrace();我的面板class MyPanel extends JPanel implements KeyLi
28、ste ner,R unn able/定義一個我的坦克Hero hero=nu II;En emyTa nk et=n ull;Image image仁 null;Image image2=n ull;Image image3=n ull;Image im=n ull;=new=newVector<Bomb>bombsVector<Bomb>();VectorvE nemyTa nk>etmVectorvE nemyTa nk>();int en size=5;public void pain t(Graphics g)super.pa in t(g);g.
29、fillRect(O, 0, 400, 300);g.drawlmage(im, 0, 0, 400, 300,this);畫出自己的坦克if(hero.isLive=true)this.drawTa nk(this.hero.getX(), this.hero.get Y(),g,this.hero.getDirect(),this.hero.getColor();畫出子彈for(i nt i=0;i<hero.shotm.size();i+)Shot myshot=hero.shotm.get(i); if(myshot!=null&&myshot.isLive=tr
30、ue) g.fill3DRect(myshot.getShotX(), myshot.getShot Y(), 2, 2, false);if(myshot.isLive=false)hero.shotm.remove(m yshot);畫出炸彈for(i nt i=0;i<bombs.size();i+)Bomb b=bombs.get(i); if(bift>6)g.drawlmage(image1, b.x, b.y, 30, 30this);else if(bift>3)g.drawImage(image2, b.x, b.y, 30, 30this);elseg.d
31、rawImage(image3, b.x, b.y, 30, 30this);b.liftdow n();if(bift=0)bombs.remove(b);畫出敵人的坦克for(i nt i=O;i<etm.size();i+)et=etm.get(i);if(et!=null&&et.isLive=true)this.drawTa nk(et.getX(), et.get Y(),g, et.getDirect(), et.getColor();/畫出敵人的子彈for(int j=0;j<et.e nsh.size();j+)Shot en shot=et.e
32、nsh.get(j);if(e nshot.isLive=true)g.fill3DRect(e nshot.getShotX(),en shot.getShot Y(), 2, 2, false);/System.out.println("第"+i+"輛坦克的第"+j+"顆子彈的 Y="+enshot.getShotY();elseet.e nsh.remove(e nshot);判斷子彈是否擊中坦克的函數(shù)public void hitTa nk(Shot s,Ta nk t)switch(t.getDirect()case 0:c
33、ase 2:if(s.getShotX()>t.getX ()&&s.getShotX()<t.getX()+20&&s.getShot Y( )>t.get Y()&&s.getShot Y( )<t.get Y()+30)s.isLive=false;t.isLive=false;Bombb=newBomb(t.getX(),t.get丫 ();bombs.add(b);break;case 1:case 3:if(s.getShotX()>t.getX ()&&s.getShotX()<t
34、.getX()+30&&s.getShot Y( )>t.get Y()&&s.getShot Y( )<t.get Y()+20)s.isLive=false;t.isLive=false;Bombb=newBomb(t.getX(),t.getY();bombs.add(b);/畫出坦克的函數(shù)public void drawTank(int xx,int yy,Graphics g,i nt direct,i nt type)判斷什么顏色類型的坦克switch(type)case 0:g.setColor(Color.CYAN);break;ca
35、se 1:g.setColor(Color.pi nk);break;case 2:g.setColor(Color.red);break;case 3:g.setColor(Color.gree n);break;case 4:g.setColor(Color.blue); break;case 5:g.setColor(Color.yellow); break;判斷什么方向的坦克switch(direct)向上case 0:g.fill3DRect(xx, yy, 5, 30, false);g.fill3DRect(xx+15, yy, 5, 30, false);g.fill3DRec
36、t(xx+5, yy+5, 10, 20, false);g.fillOval(xx+5, yy+10, 10, 10);g.drawLine(xx+10, yy+15, xx+10, yy); break;/向右case 1: g.fill3DRect(xx, yy, 30, 5, false);g.fill3DRect(xx, yy+15, 30, 5, false); g.fill3DRect(xx+5, yy+5, 20, 10, false);g.fillOval(xx+10, yy+5, 10, 10);g.drawLi ne(xx+15,yy+10,xx+30,yy+10);br
37、eak;/向下case 2:g.fill3DRect(xx, yy, 5, 30, false); g.fill3DRect(xx+15, yy, 5, 30, false); g.fill3DRect(xx+5, yy+5, 10, 20, false);g.fillOval(xx+5, yy+10, 10, 10);g.drawLi ne(xx+10,yy+15,xx+10,yy+30);break;/向左case 3:g.fill3DRect(xx, yy, 30, 5, false);g.fill3DRect(xx, yy+15, 30, 5, false);g.fill3DRect(
38、xx+5, yy+5, 20, 10, false); g.fillOval(xx+10, yy+5, 10, 10); g.drawLine(xx+15, yy+10, xx, yy+10); break;public MyPanel()hero=new Hero(100,100);im=Toolkit.getDefaultToolkit().getlmage(Pa n el.class.getResourceC728951278.jpg");創(chuàng)建敵人坦克for(i nt i=0;i<e nsize;i+)et=new EnemyTank(i+1)*50,0);Shoten
39、shot=newShot(et.getX()+10,et.get Y( )+30,et.getDirect();Thread eth=new Thread(e nshot); eth.start();et.e nsh.add(e nshot);Thread th=new Thread(et);th.start();etm.add(et);et.setets(etm);/ try / image1=lmagel0.read( newFile("bomb_1.gif");/image2=ImageIO.read( newFile("bomb_2.gif");
40、/image3=ImageIO.read( newFile("bomb_3.gif");/ catch (lOException e) / TODO Auto-ge nerated catch block/e.pri ntStackTrace();/ image1=Toolkit.getDefaultToolkit().getlmage(Pan el.class.getResourceC7bomb_1.gif");image2=Toolkit.getDefaultToolkit().getlmage(Pan el.class.getResourceC7bomb_2
41、.gif");image3=Toolkit.getDefaultToolkit().getlmage(Pan el.class.getResourceC7bomb_3.gif");/控制坦克移動public void keyPressed(KeyEve nt argO) / TODO Auto-ge nerated method stubif(arg0.getKeyCode()=KeyEve nt.VK_DOWN)this.hero.setDirect(2);this.hero.movedow n();elseif(arg0.getKeyCode()=KeyEve nt.VK_UP)this.hero.setDirect
溫馨提示
- 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)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 轉(zhuǎn)讓荔枝園合同協(xié)議書
- 購銷合同調(diào)解協(xié)議書
- 違約合同解約協(xié)議書范本
- 合伙采煤合同協(xié)議書模板
- 慈溪市旭偉電子有限公司介紹企業(yè)發(fā)展分析報告
- 游戲行業(yè)游戲開發(fā)與運營支持策略方案
- 零售行業(yè)數(shù)字化門店運營與數(shù)據(jù)分析方案
- 醫(yī)用中心供氧設(shè)備項目可行性分析報告
- 獸醫(yī)崗位招聘筆試題及解答(某大型國企)
- 學(xué)校教育國際化工作計劃-總結(jié)范文
- 腰椎椎管狹窄的護(hù)理查房
- 頂管定向鉆施工方案
- 創(chuàng)傷失血性休克中國急診專家共識(2023)解讀
- 中廣核研究院熱室設(shè)施建設(shè)項目 環(huán)境影響報告書(建造階段)
- 計算機(jī)教室(微機(jī)室)學(xué)生上機(jī)使用記錄
- 【駱駝祥子思想藝術(shù)特色中的悲劇色彩(論文)】
- 火電機(jī)組運行優(yōu)化指導(dǎo)意見
- 稅務(wù)師-稅法一-專項練習(xí)題-專題一增值稅
- 音樂中的常用速度、力度記號與常用表情術(shù)語
- 簡明疼痛評估量表
- 2023-2024年中國消毒殺毒產(chǎn)品市場分析及發(fā)展趨勢報告
評論
0/150
提交評論