坦克大战(一)

**

坦克大战(总章程)

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

**
首先我们要设定一个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 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 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();
                }

            }
        }
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(java面向对象)