1、坦克大战java源代码有些图片路径会出错 要注意package com.tankgame;import ;/坦克类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 setY
2、(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 Tank Shot shot=null; Vector shotm=new Vector(); public Hero(int x,int y) s
3、uper(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(); /调整速度 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 Ru
5、nnable Vectorensh=new Vector(); Vectorets=new Vector(); public EnemyTank(int x, int y) super(x, y); this.setColor(2); this.setDirect(2); /获取MPanel上的敌人坦克 public void setets(Vector vv) this.ets=vv; /判断敌人的坦克是否碰撞 public boolean isTouch() boolean b=false; EnemyTank et=null; switch(direct) case 0: for(int
6、 i=0;i=et.x&this.x=et.x+20&this.yet.y) return true; if(this.x+20=et.x&this.x+20=et.x+20&this.yet.y) return true; if(et.direct=1|et.direct=3) if(this.x=et.x&this.x=et.x+30&this.yet.y) return true; if(this.x+20=et.x&this.x+20=et.x+30&this.yet.y) return true; break; case 1: for(int i=0;i=et.x&this.x+30
7、=et.x+20&this.yet.y) return true; if(this.x+30=et.x&this.x+30=et.x+20&this.y+20et.y) return true; if(et.direct=1|et.direct=3) if(this.x+30=et.x&this.x+30=et.x+30&this.yet.y) return true; if(this.x+30=et.x&this.x+30=et.x+30&this.y+20et.y) return true; break; case 2: for(int i=0;i=et.x&this.x=et.x+20&
8、this.y+30et.y) return true; if(this.x+20=et.x&this.x+20=et.x+20&this.y+30et.y) return true; if(et.direct=1|et.direct=3) if(this.x=et.x&this.x=et.x+30&this.y+30et.y) return true; if(this.x+20=et.x&this.x+20=et.x+30&this.y+30et.y) return true; break; case 3: for(int i=0;i=et.x&this.x+30=et.x+20&this.y
9、et.y) return true; if(this.x+30=et.x&this.x+30=et.x+20&this.y+20et.y) return true; if(et.direct=1|et.direct=3) if(this.x+30=et.x&this.x+30=et.x+30&this.yet.y) return true; if(this.x+30=et.x&this.x+30=et.x+30&this.y+20et.y) return true; break; return b; public void run() while(true) switch(this.direc
10、t) case 0: for(int i=0;i0&this.isTouch()=false)y-=this.speed; try Thread.sleep(50); catch (InterruptedException e) / TODO Auto-generated catch block e.printStackTrace(); break; case 1: for(int i=0;i30;i+) if(x365&this.isTouch()=false)x+=this.speed; try Thread.sleep(50); catch (InterruptedException e
11、) / TODO Auto-generated catch block e.printStackTrace(); break; case 2: for(int i=0;i30;i+) if(y270&this.isTouch()=false)y+=this.speed; try Thread.sleep(50); catch (InterruptedException e) / TODO Auto-generated catch block e.printStackTrace(); break; case 3: for(int i=0;i0&this.isTouch()=false)x-=th
12、is.speed; try Thread.sleep(50); catch (InterruptedException e) / TODO Auto-generated catch block e.printStackTrace(); break; this.direct=(int)(Math.random()*4); if(this.isLive=false) break; if(ensh.size()0) lift-; else isLive=false; /子弹类class Shot implements Runnable int shotX; int shotY; int direct
13、; 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.shotY = shotY;
14、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 block e.printStackTrace(); switch(direct) case 0: shotY-=shotspeed; break; ca
15、se 1: shotX+=shotspeed; break; case 2: shotY+=shotspeed; break; case 3: shotX-=shotspeed; break; if(shotX400|shotY300) isLive=false; break; /* * 功能:坦克大战4.0 */package com.tankgame4;import java.awt.*;import ;import ;import ;import ;import ;import java.io.*;import javax.swing.*;import javax.imageio.*;p
16、ublic class MyTankGame4 extends JFrame implements ActionListener MyPanel mp=null; MyStartPanel msp=null; /菜单定义 JMenuBar jmb=null; JMenu jm1=null; JMenuItem jmi1=null; public static void main(String args) / TODO Auto-generated method stub MyTankGame4 mtg=new MyTankGame4(); /构造函数 public MyTankGame4()
17、/创建菜单 jmb=new JMenuBar(); jm1=new JMenu(Game(G); jm1.setMnemonic(G); jmi1=new JMenuItem(New Game(N); jmi1.setMnemonic(N); jmi1.addActionListener(this); jmi1.setActionCommand(New Game); jm1.add(jmi1); jmb.add(jm1); this.setJMenuBar(jmb); msp=new MyStartPanel(); Thread st=new Thread(msp); st.start();
18、this.add(msp); this.setTitle(坦克大战); this.setSize(400, 300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.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();
19、this.add(mp); this.addKeyListener(mp); this.setVisible(true); /游戏开始面板class MyStartPanel extends JPanel implements Runnable int 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.y
20、ellow); g.drawString(第一关, 130, 130); this.repaint(); public void run() while(true) try Thread.sleep(300); times+; catch (InterruptedException e) / TODO Auto-generated catch block e.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
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2