用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

坦克大战2

2015-03-15 作者: 云代码会员举报

[java]代码库

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package tank;

import java.util.Vector;

class Tank {

    int x = 0, y = 0;
    int fangxiang;
    int sudu = 9;
    boolean shengming = true;

    public int getSudu() {
        return sudu;
    }

    public void setSudu(int sudu) {
        this.sudu = sudu;
    }

    public int getFangxiang() {
        return fangxiang;
    }

    public void setFangxiang(int fangxiang) {
        this.fangxiang = fangxiang;
    }

    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;
    }
}

class DiTank extends Tank implements Runnable {

    int sudu = 2;

    Vector<zidan> dzd = new Vector<>();
    Vector<DiTank> dtk = new Vector<>();

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

    public boolean huxiangpengzhuang() {
        boolean b = false;
        switch (this.fangxiang) {
            case 0:
                for (int i = 0; i < dtk.size(); i++) {
                    DiTank dt = dtk.get(i);
                    if (dt != this) {
                        if (dt.fangxiang == 0 || dt.fangxiang == 2) {
                            if (this.x > dt.x && this.x < dt.x + 20 && this.y >= dt.y && this.y <= dt.y + 30) {
                                return true;
                            }
                            if (this.x + 20 >= dt.x && this.x + 20 <= dt.x + 20 && this.y >= dt.y && this.y <= dt.y + 30) {
                                return true;
                            }
                        }
                        if (dt.fangxiang == 3 || dt.fangxiang == 1) {
                            if (this.x >= dt.x && this.x <= dt.x + 30 && this.y >= dt.y && this.y <= dt.y + 20) {
                                return true;
                            }
                            if (this.x + 20 >= dt.x && this.x + 20 <= dt.x + 30 && this.y >= dt.y && this.y <= dt.y + 20) {
                                return true;
                            }
                        }
                    }
                }
            case 1:
                for (int i = 0; i < dtk.size(); i++) {
                    DiTank dt = dtk.get(i);
                    if (dt != this) {
                        if (dt.fangxiang == 0 || dt.fangxiang == 2) {
                            if (this.x > dt.x && this.x < dt.x + 20 && this.y >= dt.y && this.y <= dt.y + 30) {
                                return true;
                            }
                            if (this.x >= dt.x && this.x <= dt.x + 20 && this.y + 20 >= dt.y && this.y + 20 <= dt.y + 30) {
                                return true;
                            }
                        }
                        if (dt.fangxiang == 3 || dt.fangxiang == 1) {
                            if (this.x >= dt.x && this.x <= dt.x + 30 && this.y >= dt.y && this.y <= dt.y + 20) {
                                return true;
                            }
                            if (this.x >= dt.x && this.x <= dt.x + 30 && this.y + 20 >= dt.y && this.y + 20 <= dt.y + 20) {
                                return true;
                            }
                        }
                    }
                }
            case 2:
                for (int i = 0; i < dtk.size(); i++) {
                    DiTank dt = dtk.get(i);
                    if (dt != this) {
                        if (dt.fangxiang == 0 || dt.fangxiang == 2) {
                            if (this.x > dt.x && this.x < dt.x + 20 && this.y + 30 >= dt.y && this.y + 30 <= dt.y + 30) {
                                return true;
                            }
                            if (this.x + 20 >= dt.x && this.x + 20 <= dt.x + 20 && this.y + 30 >= dt.y && this.y <= dt.y + 30) {
                                return true;
                            }
                        }
                        if (dt.fangxiang == 3 || dt.fangxiang == 1) {
                            if (this.x >= dt.x && this.x <= dt.x + 30 && this.y + 30 >= dt.y && this.y <= dt.y + 20) {
                                return true;
                            }
                            if (this.x + 20 >= dt.x && this.x + 20 <= dt.x + 30 && this.y + 30 >= dt.y && this.y <= dt.y + 20) {
                                return true;
                            }
                        }
                    }
                }
            case 3:
                for (int i = 0; i < dtk.size(); i++) {
                    DiTank dt = dtk.get(i);
                    if (dt != this) {
                        if (dt.fangxiang == 0 || dt.fangxiang == 2) {
                            if (this.x + 30 > dt.x && this.x + 30 < dt.x + 20 && this.y >= dt.y && this.y <= dt.y + 30) {
                                return true;
                            }
                            if (this.x + 30 >= dt.x && this.x + 30 <= dt.x + 20 && this.y + 20 >= dt.y && this.y + 20 <= dt.y + 30) {
                                return true;
                            }
                        }
                        if (dt.fangxiang == 3 || dt.fangxiang == 1) {
                            if (this.x + 30 >= dt.x && this.x + 30 <= dt.x + 30 && this.y >= dt.y && this.y <= dt.y + 20) {
                                return true;
                            }
                            if (this.x + 30 >= dt.x && this.x + 30 <= dt.x + 30 && this.y + 20 >= dt.y && this.y + 20 <= dt.y + 20) {
                                return true;
                            }
                        }
                    }
                }
            break ;
        }
        return false;
    }

    @Override
    public void run() {
        while (true) {
            switch (this.fangxiang) {
                case 0:
                    for (int i = 0; i < 30; i++) {
                        if (y > 0 && !huxiangpengzhuang()) {
                            y -= sudu;
                        }
                        try {
                            Thread.sleep(50);
                        } catch (Exception e) {

                        }
                    }
                    break;
                case 1: {
                    for (int i = 0; i < 30; i++) {
                        if (x > 0&& !huxiangpengzhuang()) {
                            x -= sudu;
                        }
                        try {
                            Thread.sleep(50);
                        } catch (Exception e) {

                        }
                    }
                    break;

                }
                case 2: {
                    for (int i = 0; i < 30; i++) {
                        if (y < 250 && !huxiangpengzhuang()) {
                            y += sudu;
                        }
                        try {
                            Thread.sleep(50);
                        } catch (Exception e) {

                        }
                    }
                    break;
                }
                case 3: {
                    for (int i = 0; i < 30; i++) {
                        if (x < 360 && !huxiangpengzhuang() ) {
                            x += sudu;
                        }
                        try {
                            Thread.sleep(50);
                        } catch (Exception e) {

                        }
                    }
                    break;

                }
            }
            this.fangxiang = (int) (Math.random() * 4);

            if (this.shengming == false) {
                break;
            }

            if (shengming) {
                if (dzd.size() < 100) {
                    zidan zd = null;
                    switch (fangxiang) {
                        case 0:
                            zd = new zidan(x + 10, y, 0);
                            dzd.add(zd);
                            break;

                        case 1:
                            zd = new zidan(x, y + 10, 1);
                            dzd.add(zd);
                            break;
                        case 2:
                            zd = new zidan(x + 10, y + 30, 2);
                            dzd.add(zd);
                            break;
                        case 3:
                            zd = new zidan(x + 30, y + 10, 3);
                            dzd.add(zd);
                            break;
                    }
                    Thread t4 = new Thread(zd);
                    t4.start();
                }
            }
        }
    }

}

class MyTank extends Tank {

    Vector<zidan> aa = new Vector<zidan>();
    zidan zd = null;

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

    public void xiangshang() {
        y -= sudu;
    }

    public void xiangzuo() {
        x -= sudu;
    }

    public void xiangxia() {
        y += sudu;
    }

    public void xiangyou() {
        x += sudu;
    }

    public void fszd() {
        switch (this.fangxiang) {
            case 0:
                zd = new zidan(x + 10, y, 0);
                aa.add(zd);
                break;
            case 1:
                zd = new zidan(x, y + 10, 1);
                aa.add(zd);
                break;
            case 2:
                zd = new zidan(x + 10, y + 30, 2);
                aa.add(zd);
                break;
            case 3:
                zd = new zidan(x + 30, y + 10, 3);
                aa.add(zd);
                break;
        }
        Thread t = new Thread(zd);
        t.start();
    }
}

class zidan implements Runnable {

    int x, y;
    int fangxiang;
    int sudu = 10;
    boolean shengming = true;

    public zidan(int x, int y, int fangxiang) {
        this.x = x;
        this.y = y;
        this.fangxiang = fangxiang;
    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(100);
                // System.out.println("!!!");
            } catch (InterruptedException e) {
                System.out.println("ERROR:" + e);
            }

            switch (this.fangxiang) {
                case 0:
                    y -= sudu;
                    break;
                case 1:
                    x -= sudu;
                    break;
                case 2:
                    y += sudu;
                    break;
                case 3:
                    x += sudu;
                    break;
            }
            if (x < 0 || x > 500 || y < 0 || y > 400) {
                this.shengming = false;
                break;
            }
        }

    }
}

class BaoZha {

    int x, y;
    int shengcunqi = 9;
    boolean shengming = true;

    public BaoZha(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public void suqsd() {
        if (shengcunqi > 0) {
            shengcunqi--;
        } else {
            this.shengming = false;
        }
    }
}



网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...