版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、坦克大战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();/调整速度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;*功能:坦克大战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();/构造函数public MyTa nkGame4()创建菜单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("坦克大战"); 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("第一关", 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);判断子弹是否击中坦克的函数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);/画出坦克的函数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");创建敌人坦克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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论