有些图片路径会出错要注意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 setY(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<Shot> shotm=new V ector<Shot>();public Hero(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);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 Runnable {Vector<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;EnemyTank 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<=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 true;}}}}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+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>et.y){return 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.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){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<=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>et.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<30;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 (InterruptedException 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);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=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--;}else{isLive=false;}}}//子弹类class Shot implements Runnable{int 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.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;break;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;}}}}/*** 功能:坦克大战4.0*/package com.tankgame4;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.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 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 stubMyTankGame4 mtg=new MyTankGame4();}//构造函数public MyTankGame4(){//创建菜单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();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();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.yellow);g.drawString("第一关", 130, 130);}this.repaint();}public void run() {while(true){try {Thread.sleep(300);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 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.getDirect(),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 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);}else{g.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&&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+"颗子弹的Y="+enshot.getShotY());}else{et.ensh.remove(enshot);}}}}//判断子弹是否击中坦克的函数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;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.add(b);}}}//画出坦克的函数public void drawTank(int xx,int yy,Graphics g,int direct,int type){//判断什么颜色类型的坦克switch(type){case 0:g.setColor(Color.CY AN);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.setColor(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.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+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);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.getDefaultToolkit().getImage(Panel.class.getResource("/28951278.jpg"));//创建敌人坦克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.add(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().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){this.hero.setDirect(0);this.hero.moveup();}else if(arg0.getKeyCode()==KeyEvent.VK_LEFT){this.hero.setDirect(3);this.hero.moveleft();}else if(arg0.getKeyCode()==KeyEvent.VK_RIGHT){this.hero.setDirect(1);this.hero.moveright();}else if(arg0.getKeyCode()==KeyEvent.VK_P){hero.speed=0;et.speed=0;}//发射子弹(连发5枚子弹)if(hero.shotm.size()<=4){if(arg0.getKeyCode()==KeyEvent.VK_SPACE){hero.shotenemy(hero.getX(),hero.getY(),hero.getDirect());}}//控制坦克不能跑出边界if(this.hero.getX()<0)this.hero.setX(0);if(this.hero.getY()<0)this.hero.setY(0);if(this.hero.getX()>365)this.hero.setX(365);if(this.hero.getY()>270)this.hero.setY(270);//面板重绘this.repaint();}public void keyReleased(KeyEvent arg0) {// TODO Auto-generated method stub}public void keyTyped(KeyEvent arg0) {// TODO Auto-generated method stub}public void run() {while(true){try {Thread.sleep(50);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}//不停地调用敌人是否打中自己坦克函数for(int i=0;i<etm.size();i++){et=etm.get(i);for(int j=0;j<et.ensh.size();j++){Shot enshot=et.ensh.get(j);if(enshot.isLive==true&&hero.isLive==true){this.hitTank(enshot, hero);}}}//不停地调用是否打中敌人坦克函数for(int i=0;i<hero.shotm.size();i++){Shot s=hero.shotm.get(i);if(s!=null&&s.isLive==true){for(int j=0;j<etm.size();j++){EnemyTank et=etm.get(j);if(et!=null&&et.isLive==true){this.hitTank(s, et);}}}}//面板重绘this.repaint();}}}。