我是把以前写在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坦克没有被碰到Bedx = 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坦克没有被碰到Bif( (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坦克没有被碰到Bif( (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;                //朝上炮管  放大倍数viewSizeelsey += speed*viewSize;                //朝下炮管}else{if(right)x += speed*viewSize;            //朝右炮管elsex -= 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(){@Overridepublic 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;}}}};//在按下被调用@Overridepublic boolean onDown(MotionEvent motionEvent) {
//                turn = -1;return false;}//在按住时被调用@Overridepublic void onShowPress(MotionEvent motionEvent) {turn = -1;}@Overridepublic boolean onSingleTapUp(MotionEvent motionEvent) {return false;}//滑动的时候被调用@Overridepublic boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {return false;}//长按时候被调用@Overridepublic void onLongPress(MotionEvent motionEvent) {}//在抛掷动作时被调用@Overridepublic 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();}}@Overrideprotected 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);}}//实现刷新页面的效果   主要判断功能都要放在这里 (是否从新开始,判断是都击中)@Overridepublic 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();                                 //刷新页面}}@Overridepublic boolean onTouchEvent(MotionEvent event) {detector.onTouchEvent(event);return true;}}

3.3 Demo

3.3.1布局文件中定义

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

3.3.2activity 中启动

        @Overrideprotected 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爆炸图片自己网上随便找

在android view中写坦克大战相关推荐

  1. android studio写坦克大战代码_GitHub 项目推荐:俄罗斯小游戏、Markdown 幻灯片、头像生成器、Logo 制作、坦克大战...

    今天跟大家分享一下,过去几天在各大社交平台分享的一些开源项目. 本周新增了粉丝推荐环节,如果你有发现优质的开源项目,欢迎在公众号或其它平台私信推荐,我们会不定期筛选推送. 小编推荐 俄罗斯方块小游戏 ...

  2. javascript写坦克大战

    无意间浏览到别人写的js坦克大战,这是我这段时间看过最复杂的代码了(相对而言),作者博文链接:http://blog.whlcsj.com/js-tankwar.html github链接:https ...

  3. android listview settag,Android View中setTag的二三事

    每一个APP,都离不开View的使用,小到一个登陆注册页面,大到复杂的网上商城,都是View使用的具体体现. 往往我们使用View,其实就是为了向用户展示一定的数据,因此,view的使用又总是离不开数 ...

  4. 【首届Google暑期大学生博客分享大赛——2010 Andriod篇】我理想中的坦克大战游戏

      在我们这些80后的儿时记忆里,肯定少不了坦克大战这个游戏. 我现在希望在Android手机上也能够有一款这样的游戏,但是它应该是多人互动的游戏,手机和电脑都可以互动的游戏. 具体功能: 1.多人通 ...

  5. 有手就能学会- C语言零基础手写坦克大战

    1.2 项目介绍 2.1. 项目需求 实现1款和经典的<90坦克大战>一样的游戏,任务是消灭敌对坦克,保护己方领地.防止敌方打破你的老窝围墙而把你的鹰打坏. 2.2. 学习目标 回顾经 ...

  6. android studio中写中文注释时,输入法不跟随光标问题

    在android studio中,如果你使用的系统是win8或者win10系统,经常会见到在写中文注释的时候,发现好多输入法的输入框不在光标下面,也有一种情况是别人用的输入法(比如搜狗输入法)可以使用 ...

  7. python 中的坦克大战0.1版本

    这个只是初步的坦克大战,只有一个坦克,进行刷新的动作.线程类的操作不是太懂.唯一知道的就是不能再主线程里面使用sleep ,这样子的话,主线程就进入休眠的状态,不能进行时间的监听. import py ...

  8. 【Android View】写一个蛛网评分控件

    上周项目中要用到一个蛛网评分控件,于是就先上Github搜,搜了半天没搜着(也可能是我搜的关键词不对),那只好自己写一个了,就叫SpiderWebScoreView 先放一张最终效果图: 先整理一下需 ...

  9. 手写坦克大战联网版(持续更新)

    用到脚本语言 javascript es5 es6 es7 node |   models |       socket.io mysql 先了解下目录结构 前端 css:游戏的一些样式 js:动态脚 ...

最新文章

  1. Unity3D Adam Demo的学习与研究
  2. OpenSSL再曝CCS注入漏洞-心伤未愈又成筛子
  3. git/github使用完整教程(1)基础
  4. VirtualBox安装Centos6.8出现——E_INVALIDARG (0x80070057)
  5. Java8 Stream详解~映射(map/flatMap)
  6. python实现RSA加密解密 及 签名验签功能
  7. 这个火热的社区都升级到2.0了,你还不知道它?
  8. python手机销售系统结论于心得_python实现手机销售管理系统
  9. 【解决】U盘装系统(Win7/Win8) 装双系统
  10. 一个简单的flask程序
  11. 报错:error while loading shared libraries: libz.so.1: cannot open shared object file
  12. arm-linux-gcc 64位下载,arm-linux-gcc下载与安装
  13. 全开源、低代码开发平台:搭建工作管理系统
  14. 阿里巴巴图表库 Bizcharts 正式开源
  15. 在微型计算机中1gb等于多少字节,1GB等于多少字节
  16. js基础-点击切换div背景颜色
  17. 好看的皮囊 · 也是大自然的杰作 · 全球高质量 · 美图 · 集中营 · 美女 · 2017-08-22期...
  18. vbs 合并 excel 表格
  19. 国产操作系统和Linux
  20. Python selenium新窗口和老窗口的切换

热门文章

  1. 招商银行一网通H5接口调试和测试报告撰写注意事项
  2. 小游戏《别踩白块》-第十一个程序20200625
  3. 笔记本计算机的连接无线网络连接,笔记本电脑连接wifi的方法步骤
  4. 如果因为溢出导致了结果为负,那么逻辑上真正的结果必然为正
  5. WebPack 学习:从阮神的15个DEMO开始
  6. 省市区三级联动area
  7. 安徽省大数据与人工智能竞赛经验分享-4【从赛题角度看人员分工】
  8. Jetson nano——控制风扇旋转速度的方法
  9. 磁珠和电感在解决EMI和EMC的不同应用
  10. 数据营销“教父”宋星十年倾心之作,让数据真正赋能企业