坦克大战(一)

**

坦克大战(总章程)

## 本章主要写出敌我方坦克。

**
首先我们要设定一个tank类,然后敌我方都能继承它。

public class Tank {
    private int x;
    private int y;
    private int speed;
    private int direction;
    private boolean life =true;

    public boolean isLife(){
        return life;
    }

    public void setLife(boolean life) {
        this.life = life;
    }

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

    public void setDirection(int direction) {
        this.direction = direction;
    }

    public int getSpeed() {
        return speed;
    }

    public int getDirection() {
        return direction;
    }

    public void setX(int x) {
        this.x = x;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }
    Tank(int x,int y){
        this.x=x;
        this.y=y;
        speed=2;
    }
}

然后创建三个类。分别是:**我方坦克类**(mytank),**队友坦克类**(friendtank),**敌人坦克类**(enemytank)。

 

import java.util.Vector;

public class MyTank extends Tank {
    MyTank(int x,int y){
        super(x,y);
        setSpeed(7);
    }
    //向上
    public void upper(){
        this.setY(this.getY()-this.getSpeed());
    }
    //向左
    public void left(){
        this.setX(this.getX()-this.getSpeed());
    }
    //向下
    public void lower(){
        this.setY(this.getY()+this.getSpeed());
    }
    //向右
    public void right(){
        this.setX(this.getX()+this.getSpeed());
    }
    Vector<Bullet> bulletVector = new Vector<>();
    //攻击
    Bullet bullet = null;
    public void attack() {
        switch (this.getDirection()){       //获取当前枪管方向
            case 0:
               bullet = new Bullet(this.getX()+10,this.getY(),0);
                break;
            case 1:
                bullet = new Bullet(this.getX(),this.getY()+10,1);
                break;
            case 2:
                bullet = new Bullet(this.getX()+10,this.getY()+30,2);
                break;
            case 3:
                bullet = new Bullet(this.getX()+30,this.getY()+10,3);
                break;
        }
        //放入Vector
        bulletVector.add(bullet);
        //创建线程
        Thread thread = new Thread(bullet);
        //线程启动
        thread.start();
    }
}

由于我和友军的类写的内容是一样的就名字不一样所以就不挂出来了。

import java.util.Vector;

public class EnemyTank extends Tank implements Runnable {
    private int x;
    private int y;

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

    Vector<Bullet> bulletVector = new Vector<>();

    public void setY(int y) {
        this.y = y;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public int getX() {
        return x;
    }

    @Override
    public void run() {
        while (true) {
            try {                                //设置睡眠时间
                Thread.sleep(16);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
                                    //敌方坦克方向
            switch (this.getDirection()) {
                case 0:
                    for (int i = 0; i < 10; i++) {
                        if (this.getY() > 0)
                            this.setY(this.getY() - this.getSpeed());
                        try {
                            Thread.sleep(16);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 1:
                    for (int i = 0; i < 10; i++) {
                        if (this.getX() > 0)
                            this.setX(this.getX() - this.getSpeed());
                        try {
                            Thread.sleep(16);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 2:
                    for (int i = 0; i < 10; i++) {
                        if (this.getY() < 660)        //Y轴不能超过660
                            this.setY(this.getY() + this.getSpeed());
                        try {
                            Thread.sleep(16);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 3:
                    for (int i = 0; i < 10; i++) {
                        if (this.getX() < 1360)        //X轴不能超过1360 ,为后面画布做准备
                            this.setX(this.getX() + this.getSpeed());
                        try {
                            Thread.sleep(16);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
            }
            //敌方坦克方向变化 0~3
            this.setDirection((int) (Math.random() * 4));        //生成0~3的随机数

            if (!this.isLife()) {        //判断坦克是否还活着
                break;
            } else {
                if (bulletVector.size() < 5) {
                    Bullet bullet = null;
                    switch (this.getDirection()) {       //获取当前枪管方向
                        case 0:
                            bullet = new Bullet(this.getX() + 11, this.getY(), 0);
                            break;
                        case 1:
                            bullet = new Bullet(this.getX(), this.getY() + 10, 1);
                            break;
                        case 2:
                            bullet = new Bullet(this.getX() + 10, this.getY() + 30, 2);
                            break;
                        case 3:
                            bullet = new Bullet(this.getX() + 30, this.getY() + 10, 3);
                            break;
                    }
                    //放入Vector
                    bulletVector.add(bullet);
                    //创建线程
                    Thread thread = new Thread(bullet);
                    //线程启动
                    thread.start();
                }

            }
        }
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值