Android项目--坦克大战

要求

项目的内容主要实现我方坦克与敌方坦克进行对打,消灭对方坦克则成功,相反则游戏失败,完成对我方坦克的移动攻击以及相关的随机道具对坦克进行强化,敌方坦克也进行道具的获取和强化,游戏分为四个难度可以使用的坦克类型也不相同。对于的坦克的造型以及道具的UI的选取就很重要,对于炮弹对敌人造成的伤害以及不同难度下自己所受到的伤害都有所不同,不同的坦克的护甲血量以及攻击力也不相同,对应的不同的难度的敌人的AI也更加的智能伤害也随着难度的增加而增加,并且还增加了蓝牙对战的功能好友安装了游戏的话,双人可以进行对战。

基础代码

public class Tank {
   
   private Point centerPoint;
   
   
   private Point pp=new Point();
   
   private int speed=GamePanel.UNIT;
   
   private int  flag=0;//坦克敌我标志
   
   private int screenHeight;
   
   private int screenWidth;
   
   private Bitmap tankBmp ;

   
   private int direction=GamePanel.UP;
   
   public Matrix m=new Matrix();
   
   private long lastTrunTime;
   
   private int column;//坦克所占行数
   private int row;//坦克所占列数
   
   
   public Tank(){
      
   }
   public Tank(Point p){
      this.centerPoint=p;
   }
   public Tank(BitmapDrawable tankDrawable,int flag){
      centerPoint=new Point(200, 350);
      this.flag=flag;
      this.tankBmp=tankDrawable.getBitmap();
      pp.setY(centerPoint.getY());
      pp.setX(centerPoint.getX()+tankBmp.getWidth()/2);
      column=tankBmp.getWidth()/GamePanel.UNIT;
      row=tankBmp.getHeight()/GamePanel.UNIT;
   }
   public Tank(BitmapDrawable tankDrawable,Point p,int flag,int direction,int h,int w){
      this.flag=flag;
      this.tankBmp=tankDrawable.getBitmap();
      m.postRotate((direction-GamePanel.UP)*90);
      tankBmp=Bitmap.createBitmap(tankBmp, 0, 0, tankBmp.getWidth(), tankBmp.getHeight(), m, true);
      this.direction=direction;
      this.centerPoint=p;
      pp.setY(centerPoint.getY());
      pp.setX(centerPoint.getX()+tankBmp.getWidth()/2);
      this.screenHeight=h;
      this.screenWidth=w;
      column=tankBmp.getWidth()/GamePanel.UNIT;
      row=tankBmp.getHeight()/GamePanel.UNIT;
   }
   
   public void setScreenHeight(int h){
      screenHeight=h;
   }
   public void setScreenWidth(int w){
      screenWidth=w;
   }
   /**
    * 重新初始化炮筒的坐标点
    */
   private void resetFrontPoint(){
      if(direction==GamePanel.UP){
         pp.setY(centerPoint.getY());
         pp.setX(centerPoint.getX()+tankBmp.getWidth()/2);
      }else if(direction==GamePanel.DOWN){
         pp.setY(centerPoint.getY()+tankBmp.getHeight());
         pp.setX(centerPoint.getX()+tankBmp.getWidth()/2);
      }else if(direction==GamePanel.LEFT){
         pp.setY(centerPoint.getY()+tankBmp.getHeight()/2);
         pp.setX(centerPoint.getX());
      }else if(direction==GamePanel.RIGHT){
         pp.setY(centerPoint.getY()+tankBmp.getHeight()/2);
         pp.setX(centerPoint.getX()+tankBmp.getWidth());
      }
   }
   private void resetTankPoint(){
      modifyMapStatus(0);//释放原有地图占位
      if(direction==GamePanel.UP){
         this.centerPoint.setY(centerPoint.getY()-speed);
      }else if(direction==GamePanel.DOWN){
         this.centerPoint.setY(centerPoint.getY()+speed);
      }else if(direction==GamePanel.LEFT){
         this.centerPoint.setX(centerPoint.getX()-speed);
      }else if(direction==GamePanel.RIGHT){
         this.centerPoint.setX(centerPoint.getX()+speed);
      }
      resetFrontPoint();
      modifyMapStatus(1);//重新进行地图占位
   }
   /** 
    * 修改地图状态
    */
   private void modifyMapStatus(int status){
      int bC=centerPoint.getX()/GamePanel.UNIT;
      int bR=centerPoint.getY()/GamePanel.UNIT;
   
      for(int i=bR;i<bR+row;i++){
         for(int j=bC;j<bC+column;j++){
            GamePanel.map[i][j]=status;
         }
      }
   }
   
   public int getDirection(){
      return this.direction;
   }
   public void drawTank(Canvas canvas){
      canvas.drawBitmap(tankBmp, centerPoint.x, centerPoint.y, null);
   }
   public Point getFrontPoint(){
      return this.pp;
   }
   
   public void moveUp(){
      m.reset();
      m.postRotate((GamePanel.UP-direction)*90);
      tankBmp=Bitmap.createBitmap(tankBmp, 0, 0, tankBmp.getWidth(), tankBmp.getHeight(), m, true);
      direction=GamePanel.UP;
      if(!canMove()){
         resetFrontPoint();//虽然不能移动,但需要将炮筒位置重新计算
         return ;
      }
      resetTankPoint();
   }
   public void moveDown(){
      m.reset();
      m.postRotate((GamePanel.DOWN-direction)*90);
      tankBmp=Bitmap.createBitmap(tankBmp, 0, 0, tankBmp.getWidth(), tankBmp.getHeight(), m, true);
      direction=GamePanel.DOWN;
      if(!canMove()){
         resetFrontPoint();//虽然不能移动,但需要将炮筒位置重新计算
         return ;
      }
      resetTankPoint();
   }
   public void moveLeft(){
      m.reset();
      
      m.postRotate((GamePanel.LEFT-direction)*90);
      tankBmp=Bitmap.createBitmap(tankBmp, 0, 0, tankBmp.getWidth(), tankBmp.getHeight(), m, true);
      direction=GamePanel.LEFT;
      if(!canMove()){
         resetFrontPoint();//虽然不能移动,但需要将炮筒位置重新计算
         return ;
      }
      resetTankPoint();
   }
   public void moveRight(){
      m.reset();
      m.postRotate((GamePanel.RIGHT-direction)*90);
      tankBmp=Bitmap.createBitmap(tankBmp, 0, 0, tankBmp.getWidth(), tankBmp.getHeight(), m, true);
      direction=GamePanel.RIGHT;
      if(!canMove()){
         resetFrontPoint();//虽然不能移动,但需要将炮筒位置重新计算
         return ;
      }
      resetTankPoint();
   }
   
   public void move(){
      if(direction==GamePanel.UP){
         moveUp();
      }else if(direction==GamePanel.DOWN){
         moveDown();
      }else if(direction==GamePanel.LEFT){
         moveLeft();
      }else if(direction==GamePanel.RIGHT){
         moveRight();
      }
   }
   public Shells fire(){
      Shells s=GameFactory.createShells();
      s.setCenterPoint(pp);
      s.direction=direction;
      s.flag=flag;
      return s;
   }
   //获得坦克所占的矩形区域
   public Rect getFillRect(){
      return new Rect(centerPoint.getX(), centerPoint.getY(), centerPoint.getX()+tankBmp.getWidth()-1, centerPoint.getY()+tankBmp.getHeight()-1);
   }
   //判断子弹是否击中坦克,判断子弹所在区域及坦克所占区域是否相交
   public boolean hit(Shells s){
      if(s.getFlag()==flag){
         return false;
      }
      if(getFillRect().intersect(s.getFillRect())){//有交集,坦克被击中
         modifyMapStatus(0);//释放地图占位
         return true;
      }
      return false;
   }

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值