TankGame坦克大战1.0版本

MainFrame


package myFrame;

public class Game {

    public static void main(String[] args) {
        MyFrame mf = new MyFrame();
        mf.addListener();
    }

}

Shot(子弹类)


class Shot implements Runnable{
    int x;
    int y;
    int direct;
    int speed=3;
    //是否活着
    boolean isLive = true;
    public Shot(int x,int y,int direct){
        this.x=x;
        this.y=y;
        this.direct=direct;
    }
    @Override
    public void run() {

        while(true){

            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            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>400||y<0||y>300){
                this.isLive=false;
                break;
            }
        }
    }
}

Tank(坦克类)


//把敌人的坦克做成线程类
class Tank {
    //坦克的横坐标
    int x=0;
    int y=0;
    //0上 1右 2下  3左 
    int direct=0;
    int speed=5;
    int color;
    boolean isLive=true;
    public int getColor() {
        return color;
    }

    public void setColor(int color) {
        this.color = color;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public int getDirect() {
        return direct;
    }

    public void setDirect(int direct) {
        this.direct = direct;
    }

    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 Tank(int x,int y){
        this.x=x;
        this.y=y;

    }
}

Enemy类(敌军类)


class EnemyTank extends Tank implements Runnable{


    int times=0;
    Vector<EnemyTank> ets=new Vector<EnemyTank>();
    Vector<Shot> ss = new Vector<Shot>();

    //敌人添加子弹在刚刚创建坦克和敌人子弹死亡后


    public EnemyTank(int x,int y){
        super(x,y);
    }

    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+20){
                            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+20){
                            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>=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+30>=et.y && this.y+30<=et.y+20){
                            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>=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+20>=et.y && this.y+20<=et.y+20){
                            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;
    }

    @Override
    public void run() {

        while(true){

            switch(this.direct){
            case 0:
                for(int i=0;i<30;i++){
                    if(y>5&&!this.isTouchOtherEnemy()){
                    y-=speed;
                    }
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();    
                    }
                }
                break;
            case 1:
                for(int i=0;i<30;i++){
                    if(x<350&&!this.isTouchOtherEnemy()){
                    x+=speed;
                    }
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();    
                    }
                }
                break;
            case 2:
                for(int i=0;i<30;i++){
                    if(y<230&&!this.isTouchOtherEnemy()){
                    y+=speed;
                    }
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();    
                    }
                }
                break;
            case 3:
                for(int i=0;i<30;i++){
                    if(x>5&&!this.isTouchOtherEnemy()){
                    x-=speed;
                    }
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();    
                    }
                }
                break;
            }



            //让坦克随机产生一个新的方向
            this.direct=(int)(Math.random()*4); 
            //判断敌人坦克是否死亡
            if(this.isLive==false){
                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);
        }
    }
}

Hero类(本机类)


class Hero extends Tank
{

        //子弹

        //Shot s=null;
        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;
        }
        //坦克向右移动
        public void moveRight()
        {
            x+=speed;
        }

        //坦克向下移动
        public void moveDown()
        {
            y+=speed;
        }

        //向左
        public void moveLeft()
        {
            x-=speed;
        }
    }

音乐播放类


class AePlayWave extends Thread {

    private String filename;
    public AePlayWave(String wavfile) {
        filename = wavfile;
    }
    public void run() {

        File soundFile = new File(filename);

        AudioInputStream audioInputStream = null;
        try {
            audioInputStream = AudioSystem.getAudioInputStream(soundFile);
        } catch (Exception e1) {
            e1.printStackTrace();
            return;
        }

        AudioFormat format = audioInputStream.getFormat();
        SourceDataLine auline = null;
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

        try {
            auline = (SourceDataLine) AudioSystem.getLine(info);
            auline.open(format);
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        auline.start();
        int nBytesRead = 0;
        //这是缓冲
        byte[] abData = new byte[512];

        try {
            while (nBytesRead != -1) {
                nBytesRead = audioInputStream.read(abData, 0, abData.length);
                if (nBytesRead >= 0)
                    auline.write(abData, 0, nBytesRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return;
        } finally {
            auline.drain();
            auline.close();
        }

    }
}

缓存节点类


class Node{ //恢复记录点
    public Node(int x, int y, int direct) {
        this.x=x;
        this.y=y;
        this.direct=direct;
    }
    int x;
    int y;
    int direct;
}

记录节点类


class Recorder{

    private static int enNum=20;
    private static int myLisfe=3;
    private static int allEnNum=0;

    //从文件中恢复记录点
        /*private*/ static 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 static Vector<EnemyTank> ets=new Vector<EnemyTank>();

        public Vector<Node> getNodesAndEnNums()
        {
            try {
                fr=new FileReader("f:\\myRecording.txt");
                br=new BufferedReader(fr);
                String n="";

                //先读取第一行
                n=br.readLine();
                allEnNum=Integer.parseInt(n);

                while((n=br.readLine())!=null)
                {
                    String []x=n.split(" "); 
                    Node node=new Node(Integer.parseInt(x[0]),Integer.parseInt(x[1]),Integer.parseInt(x[2]));
                    nodes.add(node);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                try{
                    //后打开则先关闭
                    br.close();
                    fr.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            return nodes;

        }

        public static void keepRecAndEnemyTank()
        {
            try {
                //创建
                fw=new FileWriter("f:\\myRecording.txt");
                bw=new BufferedWriter(fw);

                bw.write(allEnNum+"\r\n");

                System.out.println("size="+ets.size());
                //保存当前活的敌人坦克的坐标和方向
                for(int i=0;i<ets.size();i++)
                {
                    //取出第一个坦克
                    EnemyTank et=ets.get(i);

                    if(et.isLive)
                    {
                        //活的就保存
                        String recode=et.x+" "+et.y+" "+et.direct;
                        bw.write(recode+"\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("f:\\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("f:\\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 Vector<Node> getNodes() {
            return nodes;
        }


        public static void setNodes(Vector<Node> nodes) {
            Recorder.nodes = nodes;
        }


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

        public static  void setEts(Vector<EnemyTank> ets) {
            Recorder.ets = ets;//
        }

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

    public static int getEnNum() {
        return enNum;
    }
    public static void setEnNum(int enNum) {
        Recorder.enNum = enNum;
    }
    public static int getMyLisfe() {
        return myLisfe;
    }
    public static void setMyLisfe(int myLisfe) {
        Recorder.myLisfe = myLisfe;
    }

    public static void reduceEnNum(){
        enNum--;
    }
    public static void reduceMylife(){
        myLisfe--;
    }

    public static void addEnNumRec(){
        allEnNum++;
    }
}

炸弹类


class Bomb{

    int x,y;
    int life=16; 
    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;
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值