在android view中写坦克大战

我是把以前写在java里的代码直接移植到android上了(后面贴的代码有比较的部分)
只改了画笔的对象,和控制方式
代码大致思路

1.画

我们要画出坦克,炮弹,爆炸效果

1.1画坦克

1.1.1画竖直的坦克

以坦克左上为基本坐标x,y 然后画出坦克的模型(履带,车身,炮管等)

1.1.2画水平的坦克

以坦克左上为基本坐标x,y 然后画出坦克的模型(履带,车身,炮管等)

1.2画炮弹

炮弹要从炮管口出发沿炮口朝向发射

1.2画爆炸效果

当判定坦克死亡时画出坦克爆炸效果.坐标起点要求:起码覆盖坦克但也不要太大

2.逻辑判断

我们要判断坦克是否被炮弹击中,坦克触碰问题,坦克血量是否为0

2.1判断坦克是否被炮弹击中

2.1.1判敌方竖直的坦克是否被炮弹击中

炮弹本身的大小也要考虑进去,然后被击中的话血量减1如果减完血量为0则死亡

2.1.2判敌方断水平的坦克是否被炮弹击中

炮弹本身的大小也要考虑进去,然后被击中的话血量减1如果减完血量为0则死亡

2.1.3判断我方竖直的坦克是否被炮弹击中

炮弹本身的大小也要考虑进去,然后被击中的话血量减1如果减完血量为0则死亡(判断方式跟敌方坦克不一样因为敌方坦克子弹和我方坦克子弹判断方式不一样)

2.1.4判断我方水平的坦克是否被炮弹击中

炮弹本身的大小也要考虑进去,然后被击中的话血量减1如果减完血量为0则死亡(判断方式跟敌方坦克不一样因为敌方坦克子弹和我方坦克子弹判断方式不一样)

2.2坦克触碰问题

2.2.1坦克触碰屏幕边缘

    要考虑坦克不能冲出边缘

2.2.2敌人坦克相互触碰

    要考虑敌人坦克不能相互穿过身体(2次循环判断就是每一辆敌人坦克与剩余坦克依次判断是否下一次前进会触碰到,触碰到则回头)

2.2.3我放坦克与地方坦克触碰

    我方坦克触碰到敌方坦克则血量下降(一次循环判断)

2.3判断坦克是否血量为0

    血量为0 设置坦克死亡

3.下面是具体实现的代码

3.1模型

3.1.1BaseTank

public class BaseTank
{
    int boundX ;                                 //屏幕宽度
    int boundY;                                  //屏幕高度
    private int size = 1;                       //坦克放大倍数
    private int x = 0;                          //坦克x坐标
    private int y = 0;                          //坦克y坐标
    private int hp = 0;                         //坦克血量
    private int speed = 0;                      //坦克速度
    private boolean isLive = true;              //是否活着
    private boolean isVertical = true;          //竖直状态
    private boolean isRight  = false;           //朝右炮管
    private boolean isUp = true;                //朝上炮管
    private Vector<Shell> tankShells = null ;   //炮弹组
    private Vector<Thread> tankThreads = null;      //炮弹线程对象组
    private Vector<ComputerTank> toucheds = null;           //传入敌人坦克组  用来判断是否与敌人坦克触碰 吗(包括敌人坦克判断是否与敌人坦克触碰)


    public void setLive(boolean live) {
        isLive = live;
    }

    public void setVertical(boolean vertical) {
        isVertical = vertical;
    }

    public void setRight(boolean right) {
        isRight = right;
    }

    public void setUp(boolean up) {
        isUp = up;
    }

    public Vector<Shell> getTankShells() {
        return tankShells;
    }

    public void setTankShells(Vector<Shell> tankShells) {
        this.tankShells = tankShells;
    }

    public Vector<Thread> getTankThreads() {
        return tankThreads;
    }

    public void setTankThreads(Vector<Thread> tankThreads) {
        this.tankThreads = tankThreads;
    }

    public Vector<ComputerTank> getToucheds() {
        return toucheds;
    }

    public void setToucheds(Vector<ComputerTank> toucheds) {
        this.toucheds = toucheds;
    }

    public BaseTank(int x, int y, int hp, int speed, boolean isVertical,
                    boolean isRight, boolean isUp, Vector<ComputerTank> toucheds,int boundX,int boundY ,int size)
    {
        super();
        this.x = x;
        this.y = y;
        this.hp = hp;
        this.speed = speed*size;
        this.isVertical = isVertical;
        this.isRight = isRight;
        this.isUp = isUp;
        this.toucheds = toucheds;
        this.boundX = boundX;
        this.boundY = boundY;
        this.size = size;

    }


    public BaseTank(){
    }

    //扣血
    public void buckleBlood()
    {
        if(hp>1)
        {
            hp--;
        }
        else
        {
            isLive = false;
        }

    }

    public boolean isLive() {
        return isLive;
    }


    public void setIsLive(boolean isLive) {
        this.isLive = isLive;
    }


    public boolean isVertical() {
        return isVertical;
    }

    public void setIsVertical(boolean isVertical) {
        this.isVertical = isVertical;
    }

    public boolean isRight() {
        return isRight;
    }

    public void setIsRight(boolean isRight) {
        this.isRight = isRight;
    }

    public boolean isUp() {
        return isUp;
    }

    public void setIsUp(boolean isUp) {
        this.isUp = isUp;
    }

    public int getSpeed()
    {
        return speed;
    }

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

    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 int getHp()
    {
        return hp;
    }
    public void setHp(int hp)
    {
        this.hp = hp;
    }

    // 向右走
    public void goRight()
    {
        if(x<boundX-36*size)
        {
            x+=speed;    isVertical = false ; isRight = true;
        }
    }
    // 向左走
    public void goLeft()
    {
        if(x>speed)
        {
            x-=speed;    isVertical = false ; isRight = false;
        }
    }
    // 向下走
    public void goDown()
    {
        if(y<boundY-36*size)
        {
            y+=speed;  isVertical = true; isUp = false ;
        }
    }
    // 向上走
    public void goUp()
    {
        if(y>speed)
        {
            y-=speed;  isVertical = true; isUp = true ;
        }
    }

    //判断坦克头是否与其他坦克触碰
    public boolean judgeTankTouch()
    {

        if(toucheds != null &&isLive )
        {


            for(int i = 0;i<toucheds.size();i++)
            {
                BaseTank touched = toucheds.get(i);
                if(touched != this)
                {
                    int xOne,yOne,xTwo,yTwo,edx,edy;      //取A坦克头的2个点  只要这两个点都不在被B触碰坦克里 那么就知道A坦克没有被碰到B

                    edx = touched.getX();
                    edy = touched.getY();
                    if(isVertical)
                    {
                        if(this.isUp())
                        {
                            xOne = x;
                            yOne = y;

                            xTwo = x+34*size;
                            yTwo = y;

                        }
                        else
                        {
                            xOne = x;
                            yOne = y+36*size;

                            xTwo = x+34*size;
                            yTwo = y+36*size;
                        }

                    }

                    else
                    {
                        if(isRight)
                        {
                            xOne = x+36*size;
                            yOne = y;

                            xTwo = x+36*size;
                            yTwo = y+34*size;
                        }

                        else
                        {
                            xOne = x;
                            yOne = y;

                            xTwo = x;
                            yTwo = y+34*size;
                        }
                    }
                    if(touched.isVertical())
                    {
                        //取A坦克头的2个点  只要这两个点都不在被B触碰坦克里 那么就知道A坦克没有被碰到B
                        if( (xOne>=edx&&xOne<=edx+34*size&&yOne>=edy&&yOne<=edy+36*size)
                                || (xTwo>=edx&&xTwo<=edx+34*size&&yTwo>=edy&&yTwo<=edy+36*size) )
                        {
                            return true;

                        }

                    }
                    else
                    {
                        //取A坦克头的2个点  只要这两个点都不在被B触碰坦克里 那么就知道A坦克没有被碰到B
                        if( (xOne>=edx&&xOne<=edx+36*size&&yOne>=edy&&yOne<=edy+34*size)
                                || (xTwo>=edx&&xTwo<=edx+34*size&&yTwo>=edy&&yTwo<=edy+36*size) )
                        {
                            return true;
                        }

                    }
                }
            }
        }
        return false;     //若没有触碰则输出false
    }

    //发射炮弹
    public void shutShell()
    {
        if(tankShells == null)
        {
            tankShells = new Vector<Shell>();
            tankThreads = new Vector<Thread>(); //是炮弹对象与炮弹线程对象同步
        }
        if(isVertical)
        {
            if(isUp)
            {
                tankShells.add(new Shell(x+15*size, y-18*size,15*size,
                        true,1*size,isVertical,isRight,isUp,boundX,boundY,size));       //朝上炮管
            }
            else
            {
                tankShells.add(new Shell(x+15*size, y+18*size,15*size,
                        true,1*size,isVertical,isRight,isUp,boundX,boundY,size));       //朝下炮管
            }
        }
        else
        {
            if(isRight)
            {
                tankShells.add(new Shell(x+52*size, y+16*size,15*size,true,1,
                        isVertical,isRight,isUp,boundX,boundY,size));
            }//朝右炮管
            else
            {
                tankShells.add(new Shell(x-18*size, y+16*size,15*size,true,1,
                        isVertical,isRight,isUp,boundX,boundY,size));               //朝左炮管
            }
        }
        //创建炮弹
        Thread sheelThread = new Thread(tankShells.get(tankShells.size()-1));
        tankThreads.add(sheelThread);
        sheelThread.start();
    }

}

3.1.2ComputerTank


public class ComputerTank extends BaseTank implements Runnable
{
    int type = 0;               //坦克类型
    int stay = 100;             //坦克持续一个动作至少走100个像素
    int any = 0;                //随机数

    public ComputerTank(int x, int y, int type, Vector<ComputerTank> toucheds,int boundX,int boundY ,int size)
    {
        super(x, y, type, 1,true,false,false,toucheds,boundX,boundY,size);
        this.type = type;
    }

    public int getType()
    {
        return type;
    }

    public void setType(int type)
    {
        this.type = type;
    }



    //让电脑随机自动跑,每个动作持续stay个像素
    public void run()
    {
        while(true)
        {
            if(super.isLive() == false)
            {
                break;
            }
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if(stay<=0)
            {
                any = (int)(Math.random()*10%4);
                stay = 60;
            }
            else if(super.getX()>super.boundX)                             //设置边界x(也就是右边界) 680
            {
                any = 1;
            }
            else if(super.getX() <= super.getSpeed())                           //设置x左边界  0
            {
                any = 0;
            }
            else if(super.getY()>super.boundY)                           //设置Y的边界  (也就是下边界)490
            {
                any = 2;
            }
            else if(super.getY() <= super.getSpeed())                          //设置Y上边界 0
            {
                any = 3;
            }
            if(any == 0)
            {
                goRight();                      //向右跑若触碰到坦克 则往反方向跑
                if(judgeTankTouch())            //下面也一样
                {                               //遇到问题先相信自己逻辑没问题,下去看看是否是低级错误
                    goLeft();                   //在去判断是否是逻辑问题经验当逻辑没有问题  日了狗了白浪费30分钟 错误居然是IF后面加了个“;”
                    any = 1;
                }

            }
            else if(any == 1)
            {
                goLeft();
                if(judgeTankTouch())
                {
                    goRight();
                    any = 0;
                }
            }
            else if(any == 2)
            {
                goUp();
                if(judgeTankTouch())
                {
                    goDown();
                    any = 3;
                }
            }
            else if(any == 3)
            {
                goDown();
                if(judgeTankTouch())
                {
                    goUp();
                    any = 2;
                }
            }
            stay --;
            if((int)(Math.random()*100%30) == 1)    //随机打炮弹
            {
                this.shutShell();
            }

        }

    }

}

3.1.3PlayerTank


public class PlayerTank extends BaseTank
{
    int life = 10;   //命数
    int score = 0;   //分数



    public PlayerTank(int x, int y, int score,Vector<ComputerTank> toucheds,int boundX,int boundY ,int size) {
        super(x, y,4, 7, true, false, true,toucheds,boundX,boundY ,size);
        this.score = score;
    }

    public int getLife()
    {
        return life;
    }

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

    public int getScore()
    {
        return score;
    }

    public void setScore(int score)
    {
        this.score = score;
    }


    public void buckleLife()
    {
        this.life --;
    }




}

3.1.4 Shell



public class Shell implements Runnable{

    int x;
    int y;
    int speed;
    boolean isLive;
    int size ;
    boolean vertical ;
    boolean right  ;
    boolean up ;
    int boundX ;
    int boundY;
    int viewSize;
    public Shell(int x, int y, int speed, boolean isLive, int size,
                 boolean isVertical ,boolean isRight , boolean isUp,int boundX,int boundY ,int viewSize)
    {
        this.x = x;
        this.y = y;
        this.speed = speed/3;//
        this.isLive = isLive;
        this.size = size;
        vertical = isVertical;
        right = isRight;
        up = isUp;
        this.boundX = boundX;
        this.boundY = boundY;
        this.viewSize = viewSize;
    }

    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 int getSpeed() {
        return speed;
    }

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

    public boolean isLive() {
        return isLive;
    }

    public void setLive(boolean isLive) {
        this.isLive = isLive;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public void run()
    {

        while(true)
        {
            if(isLive == false||x<0||x>boundX||y<0||y>boundY)      //炮弹判断是否应该死亡
            {
                isLive = false;
                break;
            }

            if(isLive == false||x<0||x>boundX||y<0||y>boundY)      //炮弹判断是否应该死亡
            {
                isLive = false;
                break;
            }

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

                e.printStackTrace();
            }
            if(vertical)
            {
                if(up)
                    y -= speed*viewSize;                //朝上炮管  放大倍数viewSize
                else
                    y += speed*viewSize;                //朝下炮管
            }
            else
            {
                if(right)
                    x += speed*viewSize;            //朝右炮管
                else
                    x -= speed*viewSize;            //朝左炮管
            }

        }

    }

}

3.1.5 Boom


public class Boom {
    int x;
    int y;
    int time;
    boolean isLive;
    boolean isVertical;
    public Boom(int x, int y,boolean isVertical) {
        super();
        this.x = x;
        this.y = y;
        this.time = 48;
        this.isLive = true;
        this.isVertical = isVertical;
    }

    public boolean isVertical() {
        return isVertical;
    }

    public void setVertical(boolean isVertical) {
        this.isVertical = isVertical;
    }

    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 int getTime() {
        return time;
    }
    public void setTime(int time) {
        this.time = time;
    }
    public boolean isLive() {
        return isLive;
    }
    public void setLive(boolean isLive) {
        this.isLive = isLive;
    }


    public void timeGo()
    {
        if(time >0)
            time-=5;
        else isLive = false;
    }


3.2 View

3.2.1 GameView


public  class GameView extends View implements Runnable{

    public static final String TAG = "GameView";



    private final byte  COM_NUMBER = 5;                      //敌人坦克默认最大数量
    private final int    MAJOR_MOVE = 10;
    //定义手势检测器实例
    private GestureDetector detector;
    private boolean isFirst = true;
    private int mHeight ;                                    //本view的高度
    private int mWidth ;                                     //本view的宽度

    private int size = 2;                                    //设置物体大小
    private byte nowComNumber;                               //敌人坦克数量

    private Bitmap []pageBooms = null;                       //图片数组

    private PlayerTank myTank = null;                        //玩家坦克
    private Vector <ComputerTank>comTanks = null;            //敌人坦克集
    private Vector<Thread> comTankThreads = null;            //敌人坦克线程集  (因为考虑到线性所以用了Vector)
    private Vector <Boom>booms = null;                       //爆炸对象集
    private int [] boomBitmapId = {R.mipmap.icon_one,
            R.mipmap.icon_two,R.mipmap.icon_three,
            R.mipmap.icon_four,R.mipmap.icon_five,
            R.mipmap.icon_six,R.mipmap.icon_seven,
            R.mipmap.icon_eight};


    public GameView(Context context, AttributeSet attrs) {
        super(context, attrs);

        //初始化
        nowComNumber = COM_NUMBER;
        //自己的方法,加载图像一项目文件为根目录
        pageBooms = new Bitmap[7];
        for(int i =0;i < pageBooms.length;i++)
        {
            pageBooms[i] = BitmapFactory.decodeResource(getResources(), boomBitmapId[i]);
            pageBooms[i] = Bitmap.createScaledBitmap(pageBooms[i], 60*size,80*size, true);

        }
        initGestureDetector(context);
    }


    public GameView(Context context) {
        super(context);


        //初始化
        nowComNumber = COM_NUMBER;
        //自己的方法,加载图像一项目文件为根目录
        pageBooms = new Bitmap[7];
        for(int i =0;i < pageBooms.length;i++)
        {
            pageBooms[i] = BitmapFactory.decodeResource(getResources(), boomBitmapId[i]);
            pageBooms[i] = Bitmap.createScaledBitmap(pageBooms[i], 60*size,80*size, true);

        }

        initGestureDetector(context);





    }


    //初始化手指滑动
    private void initGestureDetector(Context context)
    {
        //创建手势检测器 手指单下为起点 手指离开为终点

        detector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {

            boolean doRun = false;
            int turn = 0;  // 0 上      1 下         2  左       3  右        -1 停

            Thread thread = new Thread(){
                @Override
                public void run() {
                    super.run();
                    while (true)
                    {
                        try {
                            sleep(80);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        if (!myTank.isLive())
                        {
                            turn = -1;
                        }

                        switch (turn)
                        {
                            case 0:
                                myTank.goUp();
                                break;

                            case 1:
                                myTank.goDown();
                                break;

                            case 2:
                                myTank.goLeft();
                                break;

                            case 3:
                                myTank.goRight();
                                break;


                            default:
                                try {
                                    sleep(30);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                                break;
                        }
                    }
                }
            };
            //在按下被调用
            @Override
            public boolean onDown(MotionEvent motionEvent) {
//                turn = -1;
                return false;
            }

            //在按住时被调用
            @Override
            public void onShowPress(MotionEvent motionEvent) {

                turn = -1;



            }

            @Override
            public boolean onSingleTapUp(MotionEvent motionEvent) {
                return false;
            }

            //滑动的时候被调用
            @Override
            public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
                return false;
            }


            //长按时候被调用
            @Override
            public void onLongPress(MotionEvent motionEvent) {

            }

            //在抛掷动作时被调用
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                int dx = (int) (e2.getX() - e1.getX()); //计算滑动的距离
                if (Math.abs(dx) > MAJOR_MOVE && Math.abs(velocityX) > Math.abs(velocityY)) { //降噪处理,必须有较大的动作才识别
                    //加入没有开启线程则开启线程
                    if (!doRun)
                    {
                        thread.start();
                        doRun = true;
                    }

                    if (velocityX > 0) {
//向右边
                        turn = 3;
                    } else {
//向左边
                        turn = 2;
                    }
                    return true;
                } else {

                    if (velocityY>0){
//向下边
                        turn = 1;
                    } else {
//向上边
                        turn = 0;
                    }
                    return true;
                }
            }
        });
    }

    /**
     * 初始化tank位置 等等属性
     */
    private void initTank()
    {


        comTanks = new Vector<ComputerTank>();

        comTankThreads = new Vector <Thread>();
        myTank = new PlayerTank(mWidth/2-12*size, mHeight-50*size, 1,comTanks,mWidth,mHeight,size);
        booms =  new Vector <Boom>();
        for(int i = 0;i<nowComNumber;i++)
        {
            ComputerTank comOne= new ComputerTank(mWidth/nowComNumber*i,0,
                    (int)(Math.random()*10/3+1),comTanks,mWidth,mHeight,size);
            comTanks.add(comOne);
            Thread adThread = new Thread(comOne);
            comTankThreads.add(adThread);               //进行同步线程同步
            adThread.start();
        }

    }




    @Override
    protected void onDraw(Canvas canvas) {



        //获取view的高度和宽度
        mHeight = getHeight();
        mWidth = getWidth();


        //未初始化 则初始化
        if (isFirst)
        {
            initTank();
            isFirst = false;

        }


        Paint paint = new Paint();
        ComputerTank comOne = null;
        Shell comShell = null;
        Shell myShell = null;
        Vector<Shell> myShells = myTank.getTankShells();
        if(myShells != null)
        {
            for(int i = 0 ; i<myShells.size();i++)                 //画我的坦克炮弹
            {
                myShell = myShells.get(i) ;
                paintShell(myShell,myTank,canvas,paint);
            }
        }

        //玩家坦克
        paintTanks(myTank,canvas,paint,true);

        for(int i = 0;i<comTanks.size();i++)
        {


            comOne = comTanks.get(i);
            Vector<Shell> comShells = comOne.getTankShells();
            if(comShells != null)
            {
                for(int j = 0 ; j<comShells.size();j++)                 //画敌人的坦克炮弹
                {
                    comShell = comShells.get(j) ;
                    paintShell(comShell,comOne,canvas,paint);
                }
            }
    //敌人坦克
            paintTanks(comOne,canvas,paint,false);

        }
        painBoom(booms,canvas,paint);                                                //画爆炸
    }


    //画爆炸
    public void painBoom(Vector <Boom>booms,Canvas canvas,Paint paint)
    {
        Boom boom = null;
        if(booms != null)
        {
            for(int i = 0;i<booms.size();i++)
            {
                boom = booms.get(i);

                if(boom != null)
                {
                    if(boom.isVertical())
                    {               //动画帧数
                        canvas.drawBitmap(pageBooms[boom.getTime()/7],boom.getX(),boom.getY(),paint);
//                        g.drawImage(pageBooms[boom.getTime()/7], boom.getX(),boom.getY(),
//                                51, 54, this);
                    }


                    else
                    {
                        canvas.drawBitmap(pageBooms[boom.getTime()/7],boom.getX(),boom.getY(),paint);
//                        g.drawImage(pageBooms[boom.getTime()/7], boom.getX(),boom.getY(),
//                                54, 51, this);
                    }

                    boom.timeGo();
                    if(boom.isLive() == false)
                    {
                        booms.remove(boom);
                        boom = null;
                    }

                }
            }
        }
    }



    //画坦克
    public void paintTanks(BaseTank tank,Canvas canvas,Paint paint,boolean isPlayer)
    {


        int x = tank.getX();
        int y = tank.getY();
        int hp = tank.getHp();
        boolean isVertical =tank.isVertical();
        boolean isUp = tank.isUp();
        boolean isRight = tank.isRight();

        boolean isLive = tank.isLive();

        if(isPlayer)
        {
            paint.setColor(Color.BLUE);                      /*g.setColor(Color.yellow);*/
        }
        else {
            switch(hp)
            {
                //不同类型不同颜色    灰1血     白2血    红3血   黄4血
                case 1 :      paint.setColor(Color.GRAY);      /* g.setColor(Color.red);break;  */
                    break;

                case 2 :      paint.setColor(Color.LTGRAY);    /*g.setColor(Color.cyan);break;  */
                    break;

                case 3 :     paint.setColor(Color.WHITE);      /*g.setColor(Color.white);break; */
                    break;

                case 4 :   paint.setColor(Color.YELLOW);       /*g.setColor(Color.gray);break;  */
                    break;

                default:
                    break;
            }
        }
        if(isLive)
        {
            if(isVertical)
            {
                canvas.drawRect(x, y, x+8*size,y+36*size,paint);
//                g.fill3DRect(x, y, 8,36 ,true);       //左履带

                canvas.drawRect(x+24*size, y, x+32*size,y+36*size,paint);
//                g.fill3DRect(x+26, y, 8,36 ,true);        //右履带
                for(int i =0;i < 6;i++)             //履带条纹
                {
                    canvas.drawRect(x, y+6*i*size, x+7*size, y+6*i*size,paint);
//                    g.drawLine(x, y+6*i, x+7, y+6*i);

                    canvas.drawRect(x, y+6*i*size, x+7*size, y+6*i*size,paint);
//                    g.drawLine(x+26, y+6*i, x+33, y+6*i);
                }

                canvas.drawRect(x+5*size, y+3*size, x+29*size,y+33*size,paint);
//                g.fill3DRect(x+5, y+3, 24,30 ,true);          //车体
                if(isUp)
                {
                    canvas.drawRect(x+14*size, y-18*size, x+18*size,y+17*size,paint);
//                    g.fillRect(x+15, y-18, 4,35 );                //朝上炮管
                }
                else
                {
                    canvas.drawRect(x+14*size, y+18*size, x+18*size,y+53*size,paint);
//                    g.fillRect(x+15, y+18, 4,35 );                //朝下炮管
                }
            }
            else
            {
                canvas.drawRect(x-1*size, y+1*size , x+35*size,y+9*size,paint);
//                g.fill3DRect(x-1, y+1 ,  36 ,8,true);         //上履带

                canvas.drawRect(x-1*size, y+27*size, x+35*size,y+35*size,paint);
//                g.fill3DRect(x-1, y+27, 36 ,8,true);          //下履带
                for(int i =0;i < 6;i++)                     //履带条纹
                {
                    //                  g.drawLine();
                    //                  g.drawLine();
                }

                canvas.drawRect(x+2*size, y+6*size, x+32*size,y+30*size,paint);
//                g.fill3DRect(x+2, y+6,30 , 24,true);          //车体
                if(isRight)
                {
                    canvas.drawRect(x+18*size, y+16*size, x+53*size,y+20*size,paint);
//                    g.fillRect(x+18, y+16, 35,4);             //朝右炮管
                }
                else
                {
                    canvas.drawRect(x-18*size, y+16*size, x+17*size,y+20*size,paint);
//                    g.fillRect(x-18, y+16, 35,4 );                //朝左炮管
                }
            }
            canvas.drawRect(x+7*size, y+8*size, x+27*size,y+28*size,paint);
//            g.fillOval(x+7, y+8, 20, 20);             //盖子
        }
    }







    //画炮弹
    public void paintShell(Shell shell,BaseTank tank,Canvas canvas,Paint paint)
    {
        Vector<Shell> shells = tank.getTankShells();

            paint.setColor(Color.YELLOW);
//            g.setColor(Color.yellow);   //玩家子弹和敌人子弹


        if(shell.isLive())
        {
            canvas.drawRect(shell.getX(), shell.getY(),shell.getX()+4*size, shell.getY()+4*size,paint);
//            g.fillRect(shell.getX(), shell.getY(), 4,4 );
        }
        else
        {
            int index = shells.indexOf(shell);

            Thread one = tank.getTankThreads().get(index);
            tank.getTankThreads().remove(one);
            shells.remove(index);          //删除线程对象
            shell = null;
            one = null;
        }


    }




    //判断敌人的坦克是否玩家被击中
    public void judgeTankKill(PlayerTank myTank,
                              Vector <ComputerTank> adms)
    {
        ComputerTank comOne = null;
        Shell myShell = null;
        Vector<Shell> shells = myTank.getTankShells();
        int  coX,coY;   //(我方坦克被击中还未做) 已做
        int myShX,myShY;

        for(int i = 0; i<adms.size();i++)
        {
            comOne = adms.get(i);
            coX = comOne.getX();
            coY = comOne.getY();

            if(shells != null)
            {
                for(int j = 0; j<shells.size();j++)
                {
                    myShell = shells.get(j);
                    if(myShell != null)
                    {
                        myShX = myShell.getX();
                        myShY = myShell.getY();

                        if(myTank.isVertical())        //坦克死亡时是否竖直  size是方大倍数
                        {
                            if(myShX+4*size>coX&&myShX<coX+34*size&&myShY+4*size>coY&&myShY<coY+36*size)
                            {
                                tankHpReduce(comOne,myShell,adms);

                            }
                        }
                        else
                        {
                            if(myShX+4*size>coX&&myShY<coY+34*size&&myShY+4*size>coY&&myShX<coX+36*size)
                            {
                                tankHpReduce(comOne,myShell,adms);
                            }
                        }
                    }
                }
            }
        }
    }


    //作用敌方坦克如果血量减1 如果血量为零则发生爆炸
    private void tankHpReduce(ComputerTank comOne,Shell myShell,Vector <ComputerTank> adms)
    {
        Vector<Shell> shells = myTank.getTankShells();
        comOne.buckleBlood();
        myShell.setLive(false);
        if(!comOne.isLive()) //判断敌人是否活着
        {
            //产生爆炸
            booms.add(new Boom(comOne.getX()-2*size,
                    comOne.getY()-2*size,comOne.isVertical()));
            //移去敌人坦克
            int index = adms.indexOf(comOne);
            Thread one = comTankThreads.get(index);
            comTankThreads.remove(one);       //移去线程敌人坦克对象
            one = null;
            adms.remove(comOne);
            comOne = null;
            //移去我的炮弹
            shells.remove(myShell);
            myShell = null;
            comOne = new ComputerTank(mWidth/2,0,       //产生新的敌人
                    (int)(Math.random()*10/3+1),comTanks,mWidth,mHeight,size) ;
            adms.add(comOne);
            Thread adThread = new Thread(comOne);
            comTankThreads.add(adThread);
            adThread.start();                        //启动敌人自动走路线程

        }
    }

    //判断玩家的坦克是否被击中
    public void judgeMyTankKill(PlayerTank myTank,
                                Vector <ComputerTank> adm)
    {

        ComputerTank comOne = null;
        Shell comShell = null;
        int  myX = myTank.getX();
        int  myY = myTank.getY();   //(我方坦克被击中还未做)
        int comShX,comShY;
        if(myTank.isLive())
        {
            for(int i = 0; i<adm.size();i++)
            {
                comOne = adm.get(i);
                Vector<Shell> shells = comOne.getTankShells();
                if(shells != null)
                {
                    for(int j = 0; j<shells.size();j++)
                    {
                        comShell = shells.get(j);
                        if(comShell != null)
                        {
                            comShX = comShell.getX();
                            comShY = comShell.getY();

                            if(myTank.isVertical())        //坦克死亡时是否竖直
                            {
                                if(comShX+4*size>myX&&comShX<myX+34*size&&comShY+4*size>myY&&comShY<myY+36*size)
                                {
                                    tankHpReduce(comShell,shells);

                                }
                            }
                            else
                            {
                                if(comShX+4*size>myX&&comShY<myY+34*size&&comShY+4*size>myY&&comShX<myX+36*size)
                                {
                                    tankHpReduce(comShell,shells);


                                }
                            }
                        }
                    }
                }
            }
        }
    }






    //作用我方坦克如果血量减1 如果血量为零则发生爆炸
    private void tankHpReduce(Shell comShell,Vector<Shell> shells)
    {
        myTank.buckleBlood();
        comShell.setLive(false);
        //产生爆炸
        booms.add(new Boom(myTank.getX()-20*size,
                myTank.getY()-20*size,myTank.isVertical()));
        //重置玩家坦克,
        myTank.setX(mWidth/2-12*size);
        myTank.setY(mHeight-50*size);

        //移去炮弹
        shells.remove(comShell);
        comShell = null;
    }




    public void judgeMyTankNew()   //判断游戏结束是否重新开始
    {
        if(!myTank.isLive())
        {

            //暂时定为重新开始
                myTank = new PlayerTank(mWidth/2-12*size, mHeight-50*size,
                        1,comTanks,mWidth,mHeight,size);

        }
    }







    public void judgeMyTanktouched()   //判断玩家坦克是否碰到敌人坦克
    {
        if(myTank.judgeTankTouch())
        {
            myTank.buckleBlood();   //产生爆炸等
            booms.add(new Boom(myTank.getX()-2*size,
                    myTank.getY()-2*size,myTank.isVertical()));
            //重置玩家坦克,
            myTank.setX(mWidth/2-12*size);
            myTank.setY(mHeight-50*size);

        }
    }





    //实现刷新页面的效果   主要判断功能都要放在这里 (是否从新开始,判断是都击中)
    @Override
    public void run() {
        int i = 0;
        while(true)
        {
            try {
                Thread.sleep(40);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //第一次初始化 因为一定要经过 测量才能 获得高度和宽度
            if (!isFirst)
            {
//        //玩家子弹创建

                if (25 == i)
                {
                    if (null != myTank)
                    {
                        myTank.shutShell();
                    }

                    i = 0;
                }else {
                    i++;
                }



                judgeMyTankNew();                                 //当命数用完是否重新开始游戏
                judgeTankKill(myTank, comTanks);                  //敌人是否击中
                judgeMyTankKill(myTank, comTanks);                //玩家坦克是都被击中
                judgeMyTanktouched();                             //玩家坦克是都被敌人坦克触碰到
            }



            postInvalidate();                                 //刷新页面
        }
    }







        @Override
        public boolean onTouchEvent(MotionEvent event) {
            detector.onTouchEvent(event);
            return true;
        }



}

3.3 Demo

3.3.1布局文件中定义

    <pers.lijunxue.tankgame.gameview.GameView
        android:id="@+id/game"
        android:background="#000"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

3.3.2activity 中启动

        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        gameView = (GameView) findViewById(R.id.game);
        OneThread = new Thread(gameView);
        OneThread.start();
    }

3.3.3爆炸图片自己网上随便找

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值