Java坦克大战

只是用来做个人的学习笔记,也希望大神多指导,指导 微笑
/*作者:Tim
 *功能:坦克大战
 *   1.  画出坦克;
 *   2. 让坦克跑起来;
 *   3. 让坦克发出子弹;
 *   4. 当我的坦克击中敌人坦克时就消失爆炸;
 *   5.防止敌人坦克重合;
 *     5.1:决定把判断是否碰撞的函数写到EnemyTank类
 *   6.可以分关;
 *     6.1:做一个开始的panle,它是一个空的;
 *     6.2:闪烁效果;
 *   7.可以暂停和继续
 *     7.1暂停时,子弹的速度和坦克为零,方向不变
 *   8.记录成绩
 *    8.1用文件流
 *    8.2单写一个记录类,完成对玩家的记录
 *    8.3先完成保存共击中多少量敌人坦克的功能
 *    8.4存盘退出,记录当时坦克的坐标,并恢复
 *   9.加入音频
 *
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.imageio.ImageIO;
import java.io.*;


public class Mytankgame extends JFrame implements ActionListener{
    
    MyPanel my=null;
    
    //定义一个开始面板
    MyStartPanel msp=null;
    
    //作出我需要的菜单
    JMenuBar jmb=null;
    //开始游戏
    JMenu jm1=null;
    JMenuItem jmi1=null;
    //退出系统
    JMenuItem jmi2=null;
    //存盘退出
    JMenuItem jmi3=null;
    //继续上局游戏
    JMenuItem jmi4=null;
    
   public static void main(String []args){
        Mytankgame mg=new Mytankgame();
    }
    public Mytankgame(){
        // my=new MyPanel();
         
        // this.add(my);
         //启动线程
         //Thread t=new Thread(my);
         //t.start();
         //添加机制
         //this.addKeyListener(my);
         
         //创建菜单及菜单选项
         jmb=new JMenuBar();
         jm1=new JMenu("游戏(G)");
         //设置快捷方式
         jm1.setMnemonic('G');
         jmi1=new JMenuItem("开始新游戏(N)");
         jmi2=new JMenuItem("退出(E)");
         jmi3=new JMenuItem("退出保存(C)");
         jmi4=new JMenuItem("继续上局游戏(S)");
         
         //注册监听
         jmi3.addActionListener(this);
         jmi3.setActionCommand("conGame");
         jmi3.setMnemonic('S');
         //注册监听
         jmi3.addActionListener(this);
         jmi3.setActionCommand("saveExit");
         jmi3.setMnemonic('C');
         
         jmi2.addActionListener(this);
         jmi2.setActionCommand("exit");
         jmi2.setMnemonic('E');
         
         //对jim1的响应
         jmi1.addActionListener(this);
         jmi1.setActionCommand("newgame");
         jm1.add(jmi1);
         jm1.add(jmi2);
         jm1.add(jmi3);
         jm1.add(jmi4);
         jmb.add(jm1);
         
         
         msp=new MyStartPanel();
         Thread t=new Thread(msp);
         t.start();
         
         this.setJMenuBar(jmb);
         this.add(msp);
         this.setSize(800,700);
         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         this.setVisible(true);
        
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        
        //对用户不同的点击作出不同处理
        if(ae.getActionCommand().equals("newgame")){
        
            //创建战场面板   
             my=new MyPanel("newGame");
         
             
             //启动my线程
             Thread t=new Thread(my);
             t.start();
             //先删除旧的开始面板
             this.remove(msp);
             this.add(my);
             //添加监听机制
             this.addKeyListener(my);
             //显示,刷新JFrame
             this.setVisible(true);
        }else if(ae.getActionCommand().equals("exit")){
            //用户点击了退出系统的菜单
            //保存击毁敌人的数量
            Recorder.keepRecording();
            
            System.exit(0);
        }//存盘退出
        else if(ae.getActionCommand().equals("saveExit")){
            
             //工作
             Recorder rd= new Recorder();
             rd.setEts(my.ets);
            //保存击毁敌人的坦克数量
             rd.keepRecAndEnemyTank();
             System.exit(0);
        }else if(ae.getActionCommand().equals("conGame")){
             
             //创建战场面板   
             my=new MyPanel("con");
             
           //启动my线程
             Thread t=new Thread(my);
             t.start();
             //先删除旧的开始面板
             this.remove(msp);
             this.add(my);
             //添加监听机制
             this.addKeyListener(my);
             //显示,刷新JFrame
             this.setVisible(true);
        }
        
    }

}

//做一个提示作用
class MyStartPanel extends JPanel implements Runnable
{
    
    int times=0;
    public void paint(Graphics g){
        super.paint(g);
        g.fillRect(0,0,600,500);
        //提示信息
        
        if(times%2==0){
        g.setColor(Color.yellow);
        //开关信息的字体
        Font myFont=new Font("华文新魏",Font.BOLD,50);
        g.setFont(myFont);
        g.drawString("stage: 1", 200, 200);
        }
    }

    @Override
    public void run() {
        
        while(true){
            try{
                
                Thread.sleep(100);
            }catch(Exception e){
                e.printStackTrace();
            }
            
            times++;
            //重画
            this.repaint();
        }
        
    }
            
    
}

//我的面板
class MyPanel extends JPanel implements KeyListener,Runnable{
     //定义一个坦克   
    Hero hero=null;
    
  
    //定义敌人的坦克组
    Vector<EnemyTank> ets=new Vector<EnemyTank>();
    Vector<Node> nodes=new Vector<Node>();
    
    //定义一个炸弹集合
    Vector<Bomb> bombs=new Vector<Bomb>();
    int enSize=3;
    
    //定义三张图片
    Image image1=null;
    Image image2=null;
    Image image3=null;
  
    //构造函数
    public MyPanel(String flag){
        
        //恢复记录
      Recorder.getRecoring();
      hero=new Hero(100,100);
      
      if(flag.equals("newGame")){
        //初始化敌人的坦克
         for(int i=0;i<enSize;i++){
             EnemyTank et=new EnemyTank((i+1)*50,0);
            et.setColor(0);
            et.setDirect(2);
            //将mypanel的敌人坦克向量交给该敌人坦克
            et.setEts(ets);
            //启动敌人坦克
            Thread t=new Thread(et);
            t.start();
            //给敌人坦克添加一颗子弹
            Shot s=new Shot(et.x+10,et.y+30,2);
            //加入给敌人坦克
            et.ss.add(s);
            Thread t2=new Thread(s);
            t2.start();
             //加入
            ets.add(et);
         }
      }else{
          
          nodes=new Recorder().getNodesAndEnNums();
          //初始化敌人的坦克
         for(int i=0;i<nodes.size();i++){
             
             Node node=nodes.get(i);
             EnemyTank et=new EnemyTank(node.x,node.y);
            et.setColor(0);
            et.setDirect(node.direct);
            //将mypanel的敌人坦克向量交给该敌人坦克
            et.setEts(ets);
            //启动敌人坦克
            Thread t=new Thread(et);
            t.start();
            //给敌人坦克添加一颗子弹
            Shot s=new Shot(et.x+10,et.y+30,2);
            //加入给敌人坦克
            et.ss.add(s);
            Thread t2=new Thread(s);
            t2.start();
             //加入
            ets.add(et);
         }
      }
//      try{
//          image1=ImageIO.read(new File("tank1.gif"));
//          image2=ImageIO.read(new File("tank2.gif"));
//          image3=ImageIO.read(new File("tank3.gif"));
//      }catch(Exception e){
//          e.printStackTrace();
//      }
//      
     //初始化图片
          image1=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/tank1.gif"));
          image2=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/tank2.gif"));
          image3=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/tank3.gif"));
      
    }
    
    //画出提示信息
    public void showInfo(Graphics g){
        
        //画出提示信息的坦克(该坦克不参加战斗);
        this.drawTank(100, 550, g, 0, 0);
        g.setColor(Color.black);
        g.drawString(Recorder.getEnNum()+"", 130, 570);
        this.drawTank(150,550,g,0,1);
        g.setColor(Color.black);
        g.drawString(Recorder.getMyLife()+"",185 , 570);
        
        //画出玩家的总成绩
        g.setColor(Color.black);
        Font f=new Font("宋体",Font.BOLD,20);
        g.setFont(f);
        g.drawString("您的总成绩",620,30);
        
        this.drawTank(650,70,g,0,0);
        
        g.setColor(Color.black);
        g.drawString(Recorder.getAllEnNum()+"", 650, 60);
    }
    

  //重写paint
    public void paint(Graphics g){
        super.paint(g);
       
        g.fillRect(0,0,600,500);
        
        //提示信息
        this.showInfo(g);
        
        //画出自己的坦克
        if(hero.isLive){
        this.drawTank(hero.getX(),hero.getY() ,g, this.hero.direct, 1);
        }
        
        //从ss,中取出每颗子弹
      for(int i=0;i<hero.ss.size();i++){
          
          Shot myshot=hero.ss.get(i);
      
          //画出子弹,一颗子弹;
        if(myshot!=null&&myshot.isLive==true){
            g.draw3DRect(myshot.x, myshot.y, 1, 1, false);
        }
        if(myshot.isLive==false){
            //从ss中删除掉子弹
            hero.ss.remove(myshot);
        }
       }
      
      //画出炸弹
      for(int i=0;i<bombs.size();i++){
          //取出炸弹
          Bomb b=bombs.get(i);
          
          if(b.life>6){
              g.drawImage(image1, b.x, b.y, 30, 30, this);
          }else if(b.life>3){
              g.drawImage(image2, b.x, b.y, 30, 30, this);
          }else{
              g.drawImage(image3, b.x, b.y, 30, 30, this);
          }
          
          //让b的生命值减小
          b.lifeDown();
          //如果炸弹生命值为0,就把该炸弹从bombs向量去掉
          if(b.life==0){
              bombs.remove(b);
          }
      }
        //画出敌人的坦克
        for(int i=0;i<ets.size();i++){
            
            EnemyTank et=ets.get(i);
            
            if(et.isLive){
            this.drawTank(et.getX(),et.getY(), g, et.getDirect(), 0);
            //再画敌人的子弹
            for(int j=0;j<et.ss.size();j++){
                //取出子弹
                Shot enemyShot=et.ss.get(j);
                if(enemyShot.isLive){
                    g.draw3DRect(enemyShot.x, enemyShot.y, 1, 1, false);
                }else{
                    //如果敌人的坦克死亡就从Vector去掉
                    et.ss.remove(enemyShot);
                }
            }
            }
        }
    }
    
  //敌人的子弹是否击中我
    public void hitMe(){
        
       //取出每一个敌人的坦克
       for(int i=0;i<this.ets.size();i++){
           
           //取出坦克
           EnemyTank et=ets.get(i);
           
           //取出每一颗子弹
           for(int j=0;j<et.ss.size();j++){
               //取出子弹
               Shot enemyShot=et.ss.get(j);
               if(hero.isLive){
               if(this.hitTank(enemyShot,hero)){
                   
                    //敌人坦克减少
                    Recorder.reduceMyLife();
                    
               }
              }
           }
       }
        
    }
  //判断我的坦克是否击中别人的坦克
    public void hitEmenyTank(){
              //判断是否击中敌人的坦克
           for(int i=0;i<hero.ss.size();i++){
               
               //取出子弹
               Shot myshot=hero.ss.get(i);
               //判断子弹是否有效
               if(myshot.isLive){
                   
                   //取出每个坦克,与它判断
                   for(int j=0;j<ets.size();j++){
                       
                       //取出坦克
                       EnemyTank et=ets.get(j);
                       
                       if(et.isLive){
                           if(this.hitTank(myshot, et)){
                                  //敌人坦克减少
                                 Recorder.reduceEnNun();
                                 //增加我的战绩
                                 Recorder.reduceallEnNum();
                           }
                       }
                   }
               }
           }
    }
    //写一个函数专门判断子弹是否击中敌人的坦克
    public boolean hitTank(Shot s,Tank et){
        
         boolean b2=false;
        //判断该坦克的方向
        switch(et.direct){
            //向上或者向下
            case 0:
            case 2:
                if(s.x>et.x&&s.x<et.x+20&&s.y>et.y&&s.y<et.y+30){
                    //击中
                    
                    //子弹死亡
                    s.isLive=false;
                    //敌人坦克死亡
                    et.isLive=false;
                    b2=true;
                    //创建一颗炸弹,放入Vector
                    Bomb b=new Bomb(et.x,et.y);
                    //放入Vector
                    bombs.add(b);
                }
                break;
            case 1:
            case 3:
                if(s.x>et.x&&s.x<et.x+30&&s.y>et.y&&s.y<et.y+20){
                    //击中
                    
                    //子弹死亡
                    s.isLive=false;
                    //敌人坦克死亡
                    et.isLive=false;
                    b2=true;
                    //创建一颗炸弹,放入Vector
                    Bomb b=new Bomb(et.x,et.y);
                    //放入Vector
                    bombs.add(b);
                    
                }
                
        }
        return b2;
    }
  //画出坦克的函数
   public void drawTank(int x,int y,Graphics g,int direct,int type){
        //判断是什么类型的坦克
        switch(type)
        {
            case 0:
                g.setColor(Color.cyan);
                break;
            case 1:
                g.setColor(Color.yellow);
                break;                                                                                                                                                      
        }
        
        //判断方向
        switch(direct)
        {
            case 0:
                //画出我的坦克
                //画出左边的矩形
                g.fill3DRect(x, y, 5, 30, false);
                //画出右边矩形
                g.fill3DRect(x+15, y, 5, 30, false);
                //画出中间矩形
                g.fill3DRect(x+5, y+5, 10, 20, false);
                //画出圆形
                g.fillOval(x+5, y+10, 10, 10);
                //画出线
                g.drawLine(x+10, y+15, x+10, y);
                break;
            case 1:
                //炮筒向右
                //画出上边的矩形
                g.fill3DRect(x, y, 30, 5, false);
                //画出下边矩形
                g.fill3DRect(x, y+15, 30, 5, false);
                //画出中间矩形
                g.fill3DRect(x+5, y+5, 20, 10, false);
                //画出圆形
                g.fillOval(x+10, y+5, 10, 10);
                //画出线
                g.drawLine(x+15, y+10, x+30, y+10);
                break;
            case 2:
                //向下
                //画出左边的矩形
                g.fill3DRect(x, y, 5, 30, false);
                //画出右边矩形
                g.fill3DRect(x+15, y, 5, 30, false);
                //画出中间矩形
                g.fill3DRect(x+5, y+5, 10, 20, false);
                //画出圆形
                g.fillOval(x+5, y+10, 10, 10);
                //画出线
                g.drawLine(x+10, y+15, x+10, y+30);
                break;
            case 3:
                //向左
                //画出上边的矩形
                g.fill3DRect(x, y, 30, 5, false);
                //画出下边矩形
                g.fill3DRect(x, y+15, 30, 5, false);
                //画出中间矩形
                g.fill3DRect(x+5, y+5, 20, 10, false);
                //画出圆形
                g.fillOval(x+10, y+5, 10, 10);
                //画出线
                g.drawLine(x+15, y+10, x, y+10);
                break;
        }
    }

    @Override
    public void keyTyped(KeyEvent ke) {
   
    }

    //键的表示:W 向上 S 向下 A 向左 D 向右
    public void keyPressed(KeyEvent ke) {
        
        if(ke.getKeyCode()==KeyEvent.VK_W) {
            //设置坦克的方向
            this.hero.setDirect(0);
            this.hero.moveUp();
        } else if(ke.getKeyCode()==KeyEvent.VK_D){
            //向右
            this.hero.setDirect(1);
            this.hero.moveRight();
        }else if(ke.getKeyCode()==KeyEvent.VK_S){
            //向下
            this.hero.setDirect(2);
            this.hero.moveDown();
        }else if(ke.getKeyCode()==KeyEvent.VK_A){
            //向左
            this.hero.setDirect(3);
            this.hero.moveLeft();
        }
        
        if(ke.getKeyCode()==KeyEvent.VK_J){
            //判断玩家是否按下J
            
            //开火
            this.hero.shotEnemy();
        }
        //重写窗口
        this.repaint();
    
    }

    @Override
    public void keyReleased(KeyEvent ke) {
   
    }

    @Override
    public void run() {
       
        //每隔100毫秒去重绘
        while(true){
           try{
               Thread.sleep(100);
           }catch(Exception e){
               e.printStackTrace();
           }
           
     
  
              this.hitEmenyTank();
              this.hitMe();
              //重绘
            this.repaint();
        }
    }
}


import java.util.Vector;
import java.io.*;
/**
 *
 * @author Mr
 */
//点
class Node{
    int x;
    int y;
    int direct;
    public Node(int x,int y,int direct){
      
       this.x=x;
       this.y=y;
       this.direct=direct;        
    }
}

//记录类,同时保存玩家的设置
class Recorder{
    
    //记录每关的敌人
    private static int enNum=20;
    //设置我有多少可以用的人
    private static int myLife=3;
    //记录总共消灭的敌人
    private static int allEnNum=0;
    //从文件中恢复记录点
    Vector<Node> nodes=new Vector<Node>();
    
    private static FileWriter fw=null;
    private static BufferedWriter bw=null;
    private static FileReader fr=null;
    private static BufferedReader br=null;
    
    private Vector<EnemyTank> ets=new Vector<EnemyTank>();
    
    //完成读取任务
    public Vector<Node> getNodesAndEnNums(){
         try{
            fr=new FileReader("e:\\myRecording.txt");
            br=new BufferedReader(fr);
            String n="";
            n=br.readLine();
            allEnNum=Integer.parseInt(n);
            while((n=br.readLine())!=null){
                String []xyz=n.split("");
                
                Node node=new Node(Integer.parseInt(xyz[0]),Integer.parseInt(xyz[0]),Integer.parseInt(xyz[1])+Integer.parseInt(xyz[2]));
                nodes.add(node);
            }
            
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try{
                br.close();
                fr.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        return nodes;
    }
    

    public Vector<EnemyTank> getEts() {
        return ets;
    }

    public void setEts(Vector<EnemyTank> ets) {
        this.ets = ets;
    }
    
    //保存击毁敌人的数量和敌人坦克坐标,方向
    public  void  keepRecAndEnemyTank(){
        try{
            
            fw=new FileWriter("e:/myRecording.txt");
            bw=new BufferedWriter(fw);
            bw.write(allEnNum+"\r\n");
            
            //保存当前活的敌人坦克的坐标和方向
            for(int i=0;i<ets.size();i++){
                
                //取出第一个坦克
                EnemyTank et=ets.get(i);
                
                if(et.isLive){
                    //活的就保存
                    String x=et.x+" ";
                    String y=et.y+" ";
                    String d=et.direct+"";
                    
                    //写入
                    bw.write(x+"\r\n");
                    bw.write(y+"\r\n");
                    bw.write(d+"\r\n");
                }
            }
            
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            //关闭流
            try{
                //先后关闭
                bw.close();
                fw.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
    //从文件中读取,记录
    public static void getRecoring(){
        
        try{
            fr=new FileReader("e:\\myRecording.txt");
            br=new BufferedReader(fr);
            String n=br.readLine();
            allEnNum=Integer.parseInt(n);
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try{
                br.close();
                fr.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
    
    //那玩家击毁敌人坦克保存到文件
    public static void keepRecording(){
        
        try{
            fw=new FileWriter("e:/myRecording.txt");
            bw=new BufferedWriter(fw);
            bw.write(allEnNum+"\r\n");
            
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            //关闭流
            try{
                //先后关闭
                bw.close();
                fw.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }

    public static int getAllEnNum() {
        return allEnNum;
    }

    public static void setAllEnNum(int allEnNum) {
        Recorder.allEnNum = allEnNum;
    }

    public static int getMyLife() {
        return myLife;
    }

    public static void setMyLife(int myLife) {
        Recorder.myLife = myLife;
    }
    public static int getEnNum(){
        return enNum;
    }
    public static void setEnNum(int enNum){
        Recorder.enNum=enNum;
    }
    
    //减少敌人数量
    public static void reduceEnNun(){
        enNum--;
    }
    
    //我的数量减少
    public static void reduceMyLife(){
        myLife--;
    }
    //记录总共的数量
    public static void reduceallEnNum(){
        allEnNum++;
    }
}
//炸弹类
class Bomb{
    //定义炸弹的坐标
    int x,y;
    //炸弹的生命
    int life=9;
    boolean isLive=true;
    public  Bomb(int x,int y){
        this.x=x;
        this.y=y;
    }
    
    //减少生命值
    public void lifeDown(){
        if(life>0){
            life--;
        }else{
            this.isLive=false;
        }
    }
}
//子弹类
class Shot implements Runnable{
   int x;
   int y;
   int direct;
   int speed=1;
   boolean isLive=true;
   public Shot(int x,int y,int direct){
       this.x=x;
       this.y=y;
       this.direct=direct;
   }
   
    public void run() {
       while(true){
           
           try{
               Thread.sleep(50);
           }catch(Exception e){
               
           }
           switch(direct){
               case 0:
                   //上
                   y-=speed;
                   break;
               case 1:
                   x+=speed;
                   break;
               case 2:
                   y+=speed;
                   break;
               case 3:
                   x-=speed;
                   break;
                 
           }
           //判断子弹是否碰到边缘
           if(x<0||x>600||y<0||y>500){
               this.isLive=false;
               break;
           }
       }
    }
    
}




//坦克类
class Tank{




   //x表示坦克的横坐标
     int x=0;
    //y表示坦克的纵坐标
     int y=0;
   
     //坦克方向
     //0表示向上  1表示向右  2表示向下  3表示向左
      int direct=0;
     
     //坦克的速度
      int speed=1;
      
      //颜色
      int color;
     boolean isLive=true;
             
    public int getX() {
        return x;
    }

    /**
     * @param x the x to set
     */
    public void setX(int x) {
        this.x = x;
    }

    /**
     * @return the y
     */
    public int getY() {
        return y;
    }

    /**
     * @param y the y to set
     */
    public void setY(int y) {
        this.y = y;
    }
 
    public Tank(int x,int y){
        this.x=x;
        this.y=y;
    }
        /**
     * @return the speed
     */
    public int getSpeed() {
        return speed;
    }

    /**
     * @param speed the speed to set
     */
    public void setSpeed(int speed) {
        this.speed = speed;
    }
    /**
     * @return the direct
     */
    public int getDirect() {
        return direct;
    }

    /**
     * @param direct the direct to set
     */
    public void setDirect(int direct) {
        this.direct = direct;
    }
        /**
     * @return the color
     */
    public int getColor() {
        return color;
    }

    /**
     * @param color the color to set
     */
    public void setColor(int color) {
        this.color = color;
    }

}

class EnemyTank extends Tank implements Runnable{
    
 
    int times=0;
    
    //定义一个向量,可以访问到MyPANEL上所有的坦克;
    Vector<EnemyTank> ets=new Vector<EnemyTank>();
    
    //定义一个向量可以存放敌人的子弹
    Vector<Shot> ss=new Vector();
    //敌人添加坦克应在刚刚创建坦克或死亡后
    public EnemyTank(int x,int y){
        super(x,y);
    }
    
    //得到Mypanel的敌人坦克向量
    public void setEts(Vector<EnemyTank> vv){
        this.ets=vv;
    }
    
    //判断是否碰到了敌人坦克向量
    public boolean isTouchotherEnemy(){
        boolean b=false;
        
        switch(this.direct){
            case 0:
                //我的坦克向上
                //取出所有敌人坦克
                for(int i=0;i<ets.size();i++){
                    
                    //取出第一个坦克
                    EnemyTank 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&&this.y<=et.y+30){
                                return true;
                            }
                            
                            //我的右一点
                            if(this.x+20>=et.x&&this.x+20<=et.x+20&&this.y>=et.y&&this.y<=et.y+30){
                                return true;
                            }
                        }
                        if(et.direct==1||et.direct==3){
                            
                            if(this.x>=et.x&&this.x<et.x+30&&this.y>=et.y&&this.y<=et.y+30){
                                return true;
                            }
                            
                            if(this.x+20>=et.x&&this.x+20<=et.x+30&&this.y>=et.y&&this.y<=et.y+20){
                                return true;
                            }
                        }
                    }
                }
                break;
            case 1:
                //我的坦克向右
                 //取出所有敌人坦克
                for(int i=0;i<ets.size();i++){
                    
                    //取出第一个坦克
                    EnemyTank 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&&this.y<=et.y+30){
                                return true;
                            }
                            
                            //我的下一点
                            if(this.x+30>=et.x&&this.x+30<=et.x+20&&this.y+20>=et.y&&this.y+20<=et.y+30){
                                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&&this.y<=et.y+30){
                                return true;
                            }
                            
                            if(this.x+30>=et.x&&this.x+30<=et.x+30&&this.y+20>=et.y&&this.y+20<=et.y+20){
                                return true;
                            }
                        }
                    }
                }
                break;
            case 2:
                //我的坦克向下
                 //取出所有敌人坦克
                for(int i=0;i<ets.size();i++){
                    
                    //取出第一个坦克
                    EnemyTank 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&&this.y+30<=et.y+30){
                                return true;
                            }
                            
                            //我的右一点
                            if(this.x+20>=et.x&&this.x+20<=et.x+20&&this.y+30>=et.y&&this.y+30<=et.y+30){
                                return true;
                            }
                        }
                        if(et.direct==1||et.direct==3){
                            
                            if(this.x>=et.x&&this.x<et.x+30&&this.y+30>=et.y&&this.y+30<=et.y+30){
                                return true;
                            }
                            
                            if(this.x+20>=et.x&&this.x+20<=et.x+30&&this.y+30>=et.y&&this.y+30<=et.y+20){
                                return true;
                            }
                        }
                    }
                }
                break;
            case 3:
                //我的坦克向左
                 //取出所有敌人坦克
                for(int i=0;i<ets.size();i++){
                    
                    //取出第一个坦克
                    EnemyTank 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&&this.y<=et.y+30){
                                return true;
                            }
                            
                            //下一点
                            if(this.x>=et.x&&this.x<=et.x+20&&this.y+20>=et.y&&this.y+20<=et.y+30){
                                return true;
                            }
                        }
                        if(et.direct==1||et.direct==3){
                            
                            //上一点
                            if(this.x>=et.x&&this.x<et.x+30&&this.y>=et.y&&this.y<=et.y+30){
                                return true;
                            }
                            
                            //下一点
                            if(this.x>=et.x&&this.x<=et.x+30&&this.y+20>=et.y&&this.y+20<=et.y+20){
                                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.isTouchotherEnemy()){
                      y-=speed;
                    }
                    try{
                    Thread.sleep(50);
                     }catch(Exception e){
                   e.printStackTrace();
                   }
               }
                    break;
                case 1:
                    //向右
                   for(int i=0;i<30;i++){
                     if(x<600&&!this.isTouchotherEnemy()){ 
                       x+=speed;
                      }
                try{
                 Thread.sleep(50);
                  }catch(Exception e){
                   e.printStackTrace();
                   }
               }
                    break;
                case 2:
                    //向下
                   for(int i=0;i<30;i++){
                     if(y<500&&!this.isTouchotherEnemy()){
                       y+=speed;
                     }
                   try{
                    Thread.sleep(50);
                    }catch(Exception e){
                     e.printStackTrace();
                   }
                 }
                    break;
                case 3:
                    //向左
                    for(int i=0;i<30;i++){
                     if(x>0&&!this.isTouchotherEnemy()){
                        x-=speed;
                     }
                    try{
                    Thread.sleep(50);
                  }catch(Exception e){
                   e.printStackTrace();
                  }
                }
                    break;
            }
            
            this.times++;
            
            if(times%2==0){
                if(isLive){
                    if(ss.size()<5){
                        
                        Shot s=null;
                        //没有子弹
                        //添加
                        switch(direct){
                            case 0:
                                //创建一颗子弹
                                s=new Shot(x+10,y,0);
                                //把子弹加入向量
                                  ss.add(s);
                                  break;
                            case 1:
                                s=new Shot(x+30,y+10,1);
                                ss.add(s);
                                break;
                            case 2:
                                s=new Shot(x+10,y+30,2);
                                ss.add(s);
                                break;
                            case 3:
                                s=new Shot(x,y+10,3);
                                ss.add(s);
                                break;
                        }
                        //启动子弹
                        Thread t=new Thread(s);
                        t.start();
                    }
                }
            }
            //让坦克随机产生一个新的方向
            this.direct=(int)(Math.random()*4);
            
            //判断敌人坦克是否死亡
            if(this.isLive==false){
                //让坦克死亡后,退出线程
                break;
            }
        }
    }
}
//我的坦克
class Hero extends Tank{
    //添加集合
    Vector<Shot> ss=new Vector<Shot>();    
    Shot s=null;
  public Hero(int x,int y){
      super(x,y);
  }
  
  //开火
  public void shotEnemy(){
      
      switch(this.direct){
          case 0:
              s=new Shot(x+10,y,0);
              ss.add(s);
              break;
          case 1:
              s=new Shot(x+30,y+10,1);
              ss.add(s);
              break;
          case 2:
              s=new Shot(x+10,y+30,2);
              ss.add(s);
              break;
          case 3:
              s=new Shot(x,y+10,3);
              ss.add(s);
              break;
              
      }
      //启动子弹线程
      Thread t=new Thread(s);
      t.start();
  }
  
  //坦克向上移动
  public void moveUp(){
      y-=speed+2;
  }
  //坦克向右移动
  public void moveRight(){
      x+=speed+2;
  }
    //坦克向下移动
  public void moveDown(){
      y+=speed+2;
  }
    //坦克向左移动
  public void moveLeft(){
      x-=speed+2;
  }
}
最后一个音频效果没有实现
微笑
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值