坦克大戰(zhàn)java源代碼_第1頁
坦克大戰(zhàn)java源代碼_第2頁
坦克大戰(zhàn)java源代碼_第3頁
坦克大戰(zhàn)java源代碼_第4頁
坦克大戰(zhàn)java源代碼_第5頁
已閱讀5頁,還剩17頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、有些圖片路徑會出錯 要注意package com.tankgame;import java.util.Vector;/坦克類class Tank int x=0; int y=0; int color=0; int speed=1; int direct=0; boolean isLive=true; public Tank(int x,int y) this.x=x; this.y=y; public int getX() return x;public void setX(int x) this.x = x;public int getY() return y;public void se

2、tY(int y) this.y = y;public int getDirect() return direct;public void setDirect(int direct) this.direct = direct;public int getColor() return color;public void setColor(int color) this.color = color;/我的坦克class Hero extends TankShot shot=null;Vector<Shot> shotm=new Vector<Shot>();public H

3、ero(int x,int y)super(x,y);this.color=5;/坦克具有一個打擊敵人的方法public void shotenemy(int x,int y,int 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+30,2);shotm.add(shot

4、);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 movedown()y+=speed;public void moveleft()x-=speed;/敵人的坦克class EnemyTank extends Tank implements RunnableVector<

5、;Shot>ensh=new Vector<Shot>();Vector<EnemyTank>ets=new Vector<EnemyTank>();public EnemyTank(int x, int y)super(x, y);this.setColor(2);this.setDirect(2);/獲取MPanel上的敵人坦克public void setets(Vector<EnemyTank> vv)this.ets=vv;/判斷敵人的坦克是否碰撞public boolean isTouch()boolean b=false;En

6、emyTank et=null;switch(direct)case 0:for(int i=0;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<=et.y+30&&this.y>et.y)return true;if(this.x+20>=et.x&&this.x+20<=et.x+20&&this.y

7、<=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+20&&this.y>et.y)return true;if(this.x+20>=et.x&&this.x+20<=et.x+30&&this.y<=et.y+20&&this.y>et.y)return t

8、rue;break;case 1:for(int i=0;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&&this.y<=et.y+30&&this.y>et.y)return true;if(this.x+30>=et.x&&this.x+30<=et.x+20&&this.y+20<=et.y+3

9、0&&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)return true;if(this.x+30>=et.x&&this.x+30<=et.x+30&&this.y+20<=et.y+20&&this.y+20>et.y)retu

10、rn true;break;case 2:for(int i=0;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<=et.y+30&&this.y+30>et.y)return true;if(this.x+20>=et.x&&this.x+20<=et.x+20&&this.y+30<=et

11、.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)return true;if(this.x+20>=et.x&&this.x+20<=et.x+30&&this.y+30<=et.y+20&&this.y+30>et.y)

12、return true;break;case 3:for(int i=0;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&&this.y<=et.y+30&&this.y>et.y)return true;if(this.x+30>=et.x&&this.x+30<=et.x+20&&this.y+20<

13、;=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)return true;if(this.x+30>=et.x&&this.x+30<=et.x+30&&this.y+20<=et.y+20&&this.y+20>e

14、t.y)return 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 (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();break;case 1:for(int i=0;i<3

15、0;i+)if(x<365&&this.isTouch()=false)x+=this.speed;try Thread.sleep(50); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();break;case 2:for(int i=0;i<30;i+)if(y<270&&this.isTouch()=false)y+=this.speed;try Thread.sleep(50); catch (InterruptedE

16、xception e) / TODO Auto-generated catch blocke.printStackTrace();break;case 3:for(int i=0;i<30;i+)if(x>0&&this.isTouch()=false)x-=this.speed;try Thread.sleep(50); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();break;this.direct=(int)(Math.random()*4

17、);if(this.isLive=false)break;if(ensh.size()<5)Shot es=null;switch(this.direct)case 0:es=new Shot(this.getX()+10,this.getY(),0);ensh.add(es);break;case 1:es=new Shot(this.getX()+30,this.getY()+10,1);ensh.add(es);break;case 2:es=new Shot(this.getX()+10,this.getY()+30,2);ensh.add(es);break;case 3:es

18、=new Shot(this.getX(),this.getY()+10,3);ensh.add(es);break;Thread th=new Thread(es);th.start();/炸彈類 class Bomb int x;int y;int lift=9;boolean isLive=true;public Bomb(int x,int y)this.x=x;this.y=y;/炸彈的生命值public void liftdown()if(lift>0)lift-;elseisLive=false; /子彈類class Shot implements Runnableint

19、shotX;int shotY;int direct;int shotspeed=1;boolean isLive=true;public Shot(int x,int y,int direct)this.shotX=x;this.shotY=y;this.direct=direct;public int getShotX() return shotX;public void setShotX(int shotX) this.shotX = shotX;public int getShotY() return shotY;public void setShotY(int shotY) this

20、.shotY = shotY;public int getShotspeed() return shotspeed;public void setShotspeed(int shotspeed) this.shotspeed = shotspeed;public void run()while(true)try Thread.sleep(50); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();switch(direct)case 0:shotY-=shotspeed;bre

21、ak;case 1:shotX+=shotspeed;break;case 2:shotY+=shotspeed;break;case 3:shotX-=shotspeed;break;if(shotX<0|shotX>400|shotY<0|shotY>300)isLive=false;break;/* * 功能:坦克大戰(zhàn)4.0 */package com.tankgame4;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import j

22、ava.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.util.Vector;import java.io.*;import javax.swing.*;import javax.imageio.*;public class MyTankGame4 extends JFrame implements ActionListenerMyPanel mp=null;MyStartPanel msp=null;/菜單定義JMenuBar jmb=null;JMenu jm1=null;JMenuItem jmi1=nu

23、ll;public static void main(String args) / TODO Auto-generated method stubMyTankGame4 mtg=new MyTankGame4();/構(gòu)造函數(shù)public MyTankGame4()/創(chuàng)建菜單jmb=new JMenuBar();jm1=new JMenu("Game(G)");jm1.setMnemonic('G');jmi1=new JMenuItem("New Game(N)");jmi1.setMnemonic('N');jmi1.a

24、ddActionListener(this);jmi1.setActionCommand("New Game");jm1.add(jmi1);jmb.add(jm1);this.setJMenuBar(jmb);msp=new MyStartPanel();Thread st=new Thread(msp);st.start();this.add(msp);this.setTitle("坦克大戰(zhàn)");this.setSize(400, 300);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);thi

25、s.setVisible(true);public void actionPerformed(ActionEvent arg0) if(arg0.getActionCommand().equals("New Game")this.remove(msp);mp=new MyPanel();Thread mt=new Thread(mp);mt.start();this.add(mp);this.addKeyListener(mp);this.setVisible(true);/游戲開始面板class MyStartPanel extends JPanel implements

26、 Runnableint times=0;public void paint(Graphics g)super.paint(g);g.fillRect(0, 0, 400, 300);if(times%2=0)Font myfont=new Font("行楷",Font.BOLD,30);g.setFont(myfont);g.setColor(Color.yellow);g.drawString("第一關(guān)", 130, 130);this.repaint();public void run() while(true)try Thread.sleep(3

27、00);times+; catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();/我的面板class MyPanel extends JPanel implements KeyListener,Runnable/定義一個我的坦克Hero hero=null;EnemyTank et=null;Image image1=null;Image image2=null;Image image3=null;Image im=null;Vector<Bomb> bombs =new

28、 Vector<Bomb>();Vector<EnemyTank> etm =new Vector<EnemyTank>();int ensize=5;public void paint(Graphics g)super.paint(g);g.fillRect(0, 0, 400, 300);g.drawImage(im, 0, 0, 400, 300,this);/畫出自己的坦克if(hero.isLive=true)this.drawTank(this.hero.getX(), this.hero.getY(), g, this.hero.getDire

29、ct(),this.hero.getColor() );/畫出子彈for(int i=0;i<hero.shotm.size();i+)Shot myshot=hero.shotm.get(i);if(myshot!=null&&myshot.isLive=true)g.fill3DRect(myshot.getShotX(),myshot.getShotY(), 2, 2, false);if(myshot.isLive=false)hero.shotm.remove(myshot);/畫出炸彈for(int i=0;i<bombs.size();i+)Bomb

30、b=bombs.get(i);if(b.lift>6)g.drawImage(image1, b.x, b.y, 30, 30, this);else if(b.lift>3)g.drawImage(image2, b.x, b.y, 30, 30, this);elseg.drawImage(image3, b.x, b.y, 30, 30, this);b.liftdown();if(b.lift=0)bombs.remove(b);/畫出敵人的坦克for(int i=0;i<etm.size();i+)et=etm.get(i);if(et!=null&&

31、;et.isLive=true)this.drawTank(et.getX(), et.getY(),g, et.getDirect(), et.getColor();/畫出敵人的子彈for(int j=0;j<et.ensh.size();j+)Shot enshot=et.ensh.get(j);if(enshot.isLive=true)g.fill3DRect(enshot.getShotX(),enshot.getShotY(), 2, 2, false);/System.out.println("第"+i+"輛坦克的第"+j+"

32、;顆子彈的Y="+enshot.getShotY();elseet.ensh.remove(enshot);/判斷子彈是否擊中坦克的函數(shù) public void hitTank(Shot s,Tank t) switch(t.getDirect() case 0: case 2: if(s.getShotX()>t.getX()&&s.getShotX()<t.getX()+20&& s.getShotY()>t.getY()&&s.getShotY()<t.getY()+30) s.isLive=false;

33、t.isLive=false; Bomb b=new Bomb(t.getX(),t.getY(); bombs.add(b); break; case 1: case 3: if(s.getShotX()>t.getX()&&s.getShotX()<t.getX()+30&& s.getShotY()>t.getY()&&s.getShotY()<t.getY()+20) s.isLive=false; t.isLive=false; Bomb b=new Bomb(t.getX(),t.getY(); bombs.a

34、dd(b); /畫出坦克的函數(shù)public void drawTank(int xx,int yy,Graphics g,int direct,int type)/判斷什么顏色類型的坦克switch(type)case 0:g.setColor(Color.CYAN);break;case 1:g.setColor(Color.pink);break;case 2:g.setColor(Color.red);break;case 3:g.setColor(Color.green);break;case 4:g.setColor(Color.blue);break;case 5:g.setCol

35、or(Color.yellow);break;/判斷什么方向的坦克switch(direct) /向上case 0: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.drawLine(xx+10, yy+15, xx+10, yy);break;/向右case 1:g.fill3DRect(xx, yy, 30, 5, false);g.fill3DRe

36、ct(xx, yy+15, 30, 5, false);g.fill3DRect(xx+5, yy+5, 20, 10, false);g.fillOval(xx+10, yy+5, 10, 10);g.drawLine(xx+15, yy+10, xx+30, yy+10);break;/向下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);

37、g.drawLine(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(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.getDefau

38、ltToolkit().getImage(Panel.class.getResource("/28951278.jpg");/創(chuàng)建敵人坦克for(int i=0;i<ensize;i+) et=new EnemyTank(i+1)*50,0);Shot enshot=new Shot(et.getX()+10,et.getY()+30,et.getDirect();Thread eth=new Thread(enshot);eth.start();et.ensh.add(enshot);Thread th=new Thread(et);th.start();etm.a

39、dd(et);et.setets(etm);/try /image1=ImageIO.read(new File("bomb_1.gif");/image2=ImageIO.read(new File("bomb_2.gif");/image3=ImageIO.read(new File("bomb_3.gif");/ catch (IOException e) / TODO Auto-generated catch block/e.printStackTrace();/image1=Toolkit.getDefaultToolkit

40、().getImage(Panel.class.getResource("/bomb_1.gif");image2=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_2.gif");image3=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_3.gif");/控制坦克移動public void keyPressed(KeyEvent arg0) / TODO Auto-generated method stubif(arg0.getKeyCode()=KeyEvent.VK_DOWN)this.hero.setDirect(2);this.hero.movedown();else if(arg0.getKeyCode()=KeyEvent.VK_UP

溫馨提示

  • 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論