欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

打飞机游戏终极BOSS Android实战打飞机游戏完结篇

程序员文章站 2024-03-05 10:09:12
本文实例为大家分享了打飞机游戏boss以及胜利失败页面设计的android代码,具体内容如下 修改子弹类: public class bullet {...

本文实例为大家分享了打飞机游戏boss以及胜利失败页面设计的android代码,具体内容如下

打飞机游戏终极BOSS Android实战打飞机游戏完结篇

修改子弹类:

public class bullet {
 //子弹图片资源
 public bitmap bmpbullet;
 //子弹的坐标
 public int bulletx, bullety;
 //子弹的速度
 public int speed;
 //子弹的种类以及常量
 public int bullettype;
 //主角的
 public static final int bullet_player = -1;
 //鸭子的
 public static final int bullet_duck = 1;
 //苍蝇的
 public static final int bullet_fly = 2;
 //boss的
 public static final int bullet_boss = 3;
 //子弹是否超屏, 优化处理
 public boolean isdead;

 //boss疯狂状态下子弹相关成员变量
 private int dir;//当前boss子弹方向
 //8方向常量
 public static final int dir_up = -1;
 public static final int dir_down = 2;
 public static final int dir_left = 3;
 public static final int dir_right = 4;
 public static final int dir_up_left = 5;
 public static final int dir_up_right = 6;
 public static final int dir_down_left = 7;
 public static final int dir_down_right = 8;

 //子弹当前方向
 public bullet(bitmap bmpbullet, int bulletx, int bullety, int bullettype) {
 this.bmpbullet = bmpbullet;
 this.bulletx = bulletx;
 this.bullety = bullety;
 this.bullettype = bullettype;
 //不同的子弹类型速度不一
 switch (bullettype) {
 case bullet_player:
  speed = 4;
  break;
 case bullet_duck:
  speed = 3;
  break;
 case bullet_fly:
  speed = 4;
  break;
 case bullet_boss:
  speed = 5;
  break;
 }
 }

 /**
 * 专用于处理boss疯狂状态下创建的子弹
 * @param bmpbullet
 * @param bulletx
 * @param bullety
 * @param bullettype
 * @param dir
 */
 public bullet(bitmap bmpbullet, int bulletx, int bullety, int bullettype, int dir) {
 this.bmpbullet = bmpbullet;
 this.bulletx = bulletx;
 this.bullety = bullety;
 this.bullettype = bullettype;
 speed = 5;
 this.dir = dir;
 }

 //子弹的绘制
 public void draw(canvas canvas, paint paint) {
 canvas.drawbitmap(bmpbullet, bulletx, bullety, paint);
 }

 //子弹的逻辑
 public void logic() {
 //不同的子弹类型逻辑不一
 //主角的子弹垂直向上运动
 switch (bullettype) {
 case bullet_player:
  bullety -= speed;
  if (bullety < -50) {
  isdead = true;
  }
  break;
 //鸭子和苍蝇的子弹都是垂直下落运动
 case bullet_duck:
 case bullet_fly:
  bullety += speed;
  if (bullety > mysurfaceview.screenh) {
  isdead = true;
  }
  break;
 //boss疯狂状态下的8方向子弹逻辑
 case bullet_boss:
  //boss疯狂状态下的子弹逻辑待实现
  switch (dir) {
  //方向上的子弹
  case dir_up:
  bullety -= speed;
  break;
  //方向下的子弹
  case dir_down:
  bullety += speed;
  break;
  //方向左的子弹
  case dir_left:
  bulletx -= speed;
  break;
  //方向右的子弹
  case dir_right:
  bulletx += speed;
  break;
  //方向左上的子弹
  case dir_up_left:
  bullety -= speed;
  bulletx -= speed;
  break;
  //方向右上的子弹
  case dir_up_right:
  bulletx += speed;
  bullety -= speed;
  break;
  //方向左下的子弹
  case dir_down_left:
  bulletx -= speed;
  bullety += speed;
  break;
  //方向右下的子弹
  case dir_down_right:
  bullety += speed;
  bulletx += speed;
  break;
  }
  //边界处理
  if (bullety > mysurfaceview.screenh || bullety <= -40 || bulletx > mysurfaceview.screenw || bulletx <= -40) {
  isdead = true;
  }
  break;
 }
 }
}

下面 新建boss类

创建boss

public class boss {
 //boss的血量
 public int hp = 50;
 //boss的图片资源
 private bitmap bmpboss;
 //boss坐标
 public int x, y;
 //boss每帧的宽高
 public int framew, frameh;
 //boss当前帧下标
 private int frameindex;
 //boss运动的速度
 private int speed = 5;
 //boss的运动轨迹
 //一定时间会向着屏幕下方运动,并且发射大范围子弹,(是否狂态)
 //正常状态下 ,子弹垂直朝下运动
 private boolean iscrazy;
 //进入疯狂状态的状态时间间隔
 private int crazytime = 200;
 //计数器
 private int count;

 //boss的构造函数
 public boss(bitmap bmpboss) {
 this.bmpboss = bmpboss;
 framew = bmpboss.getwidth() / 10;
 frameh = bmpboss.getheight();
 //boss的x坐标居中
 x = mysurfaceview.screenw / 2 - framew / 2;
 y = 0;
 }

 //boss的绘制
 public void draw(canvas canvas, paint paint) {
 canvas.save();
 canvas.cliprect(x, y, x + framew, y + frameh);
 canvas.drawbitmap(bmpboss, x - frameindex * framew, y, paint);
 canvas.restore();
 }

 //boss的逻辑
 public void logic() {
 //不断循环播放帧形成动画
 frameindex++;
 if (frameindex >= 10) {
  frameindex = 0;
 }
 //没有疯狂的状态
 if (iscrazy == false) {
  x += speed;
  if (x + framew >= mysurfaceview.screenw) {
  speed = -speed;
  } else if (x <= 0) {
  speed = -speed;
  }
  count++;
  if (count % crazytime == 0) {
  iscrazy = true;
  speed = 24;
  }
  //疯狂的状态
 } else {
  speed -= 1;
  //当boss返回时创建大量子弹
  if (speed == 0) {
  //添加8方向子弹
  mysurfaceview.vcbulletboss.add(new bullet(mysurfaceview.bmpbossbullet, x+40, y+10, bullet.bullet_boss, bullet.dir_up));
  mysurfaceview.vcbulletboss.add(new bullet(mysurfaceview.bmpbossbullet, x+40, y+10, bullet.bullet_boss, bullet.dir_down));
  mysurfaceview.vcbulletboss.add(new bullet(mysurfaceview.bmpbossbullet, x+40, y+10, bullet.bullet_boss, bullet.dir_left));
  mysurfaceview.vcbulletboss.add(new bullet(mysurfaceview.bmpbossbullet, x+40, y+10, bullet.bullet_boss, bullet.dir_right));
  mysurfaceview.vcbulletboss.add(new bullet(mysurfaceview.bmpbossbullet, x+40, y+10, bullet.bullet_boss, bullet.dir_up_left));
  mysurfaceview.vcbulletboss.add(new bullet(mysurfaceview.bmpbossbullet, x+40, y+10, bullet.bullet_boss, bullet.dir_up_right));
  mysurfaceview.vcbulletboss.add(new bullet(mysurfaceview.bmpbossbullet, x+40, y+10, bullet.bullet_boss, bullet.dir_down_left));
  mysurfaceview.vcbulletboss.add(new bullet(mysurfaceview.bmpbossbullet, x+40, y+10, bullet.bullet_boss, bullet.dir_down_right));
  }
  y += speed;
  if (y <= 0) {
  //恢复正常状态
  iscrazy = false;
  speed = 5;
  }
 }
 }

 //判断碰撞(boss被主角子弹击中)
 public boolean iscollsionwith(bullet bullet) {
 int x2 = bullet.bulletx;
 int y2 = bullet.bullety;
 int w2 = bullet.bmpbullet.getwidth();
 int h2 = bullet.bmpbullet.getheight();
 if (x >= x2 && x >= x2 + w2) {
  return false;
 } else if (x <= x2 && x + framew <= x2) {
  return false;
 } else if (y >= y2 && y >= y2 + h2) {
  return false;
 } else if (y <= y2 && y + frameh <= y2) {
  return false;
 }
 return true;
 }

 //设置boss血量
 public void sethp(int hp) {
 this.hp = hp;
 }
}

在主界面实例化 子弹 以及boss

public class mysurfaceview extends surfaceview implements callback, runnable {
 private surfaceholder sfh;
 private paint paint;
 private thread th;
 private boolean flag;
 private canvas canvas;
 public static int screenw, screenh;
 //定义游戏状态常量
 public static final int game_menu = 0;//游戏菜单
 public static final int gameing = 1;//游戏中
 public static final int game_win = 2;//游戏胜利
 public static final int game_lost = 3;//游戏失败
 public static final int game_pause = -1;//游戏菜单
 //当前游戏状态(默认初始在游戏菜单界面)
 public static int gamestate = game_menu;
 //声明一个resources实例便于加载图片
 private resources res = this.getresources();
 //声明游戏需要用到的图片资源(图片声明)
 private bitmap bmpbackground;//游戏背景
 private bitmap bmpboom;//爆炸效果
 private bitmap bmpboosboom;//boos爆炸效果
 private bitmap bmpbutton;//游戏开始按钮
 private bitmap bmpbuttonpress;//游戏开始按钮被点击
 private bitmap bmpenemyduck;//怪物鸭子
 private bitmap bmpenemyfly;//怪物苍蝇
 private bitmap bmpenemyboos;//怪物猪头boos
 private bitmap bmpgamewin;//游戏胜利背景
 private bitmap bmpgamelost;//游戏失败背景
 private bitmap bmpplayer;//游戏主角飞机
 private bitmap bmpplayerhp;//主角飞机血量
 private bitmap bmpmenu;//菜单背景
 public static bitmap bmpbullet;//子弹
 public static bitmap bmpenemybullet;//敌机子弹
 public static bitmap bmpbossbullet;//boss子弹
 //声明一个菜单对象
 private gamemenu gamemenu;
 //声明一个滚动游戏背景对象
 private gamebg background;
 //声明主角对象
 private player player;
 //声明一个敌机容器
 private vector<enemy> vcenemy;
 //每次生成敌机的时间(毫秒)
 private int createenemytime = 50;
 private int count;//计数器
 //敌人数组:1和2表示敌机的种类,-1表示boss
 //二维数组的每一维都是一组怪物
 private int enemyarray[][] = { { 1, 2 }, { 1, 1 }, { 1, 3, 1, 2 }, { 1, 2 }, { 2, 3 }, { 3, 1, 3 }, { 2, 2 }, { 1, 2 }, { 2, 2 }, { 1, 3, 1, 1 }, { 2, 1 },
  { 1, 3 }, { 2, 1 }, { -1 } };
 //当前取出一维数组的下标
 private int enemyarrayindex;
 //是否出现boss标识位
 private boolean isboss;
 //随机库,为创建的敌机赋予随即坐标
 private random random;
 //敌机子弹容器
 private vector<bullet> vcbullet;
 //添加子弹的计数器
 private int countenemybullet;
 //主角子弹容器
 private vector<bullet> vcbulletplayer;
 //添加子弹的计数器
 private int countplayerbullet;
 //爆炸效果容器
 private vector<boom> vcboom;
 //声明boss
 private boss boss;
 //boss的子弹容器
 public static vector<bullet> vcbulletboss;

 /**
 * surfaceview初始化函数
 */
 public mysurfaceview(context context) {
 super(context);
 sfh = this.getholder();
 sfh.addcallback(this);
 paint = new paint();
 paint.setcolor(color.white);
 paint.setantialias(true);
 setfocusable(true);
 setfocusableintouchmode(true);
 //设置背景常亮
 this.setkeepscreenon(true);
 }

 /**
 * surfaceview视图创建,响应此函数
 */
 @override
 public void surfacecreated(surfaceholder holder) {
 screenw = this.getwidth();
 screenh = this.getheight();
 initgame();
 flag = true;
 //实例线程
 th = new thread(this);
 //启动线程
 th.start();
 }

 /*
 * 自定义的游戏初始化函数
 */
 private void initgame() {
 //放置游戏切入后台重新进入游戏时,游戏被重置!
 //当游戏状态处于菜单时,才会重置游戏
 if (gamestate == game_menu) {
  //加载游戏资源
  bmpbackground = bitmapfactory.decoderesource(res, r.drawable.background);
  bmpboom = bitmapfactory.decoderesource(res, r.drawable.boom);
  bmpboosboom = bitmapfactory.decoderesource(res, r.drawable.boos_boom);
  bmpbutton = bitmapfactory.decoderesource(res, r.drawable.button);
  bmpbuttonpress = bitmapfactory.decoderesource(res, r.drawable.button_press);
  bmpenemyduck = bitmapfactory.decoderesource(res, r.drawable.enemy_duck);
  bmpenemyfly = bitmapfactory.decoderesource(res, r.drawable.enemy_fly);
  bmpenemyboos = bitmapfactory.decoderesource(res, r.drawable.enemy_pig);
  bmpgamewin = bitmapfactory.decoderesource(res, r.drawable.gamewin);
  bmpgamelost = bitmapfactory.decoderesource(res, r.drawable.gamelost);
  bmpplayer = bitmapfactory.decoderesource(res, r.drawable.player);
  bmpplayerhp = bitmapfactory.decoderesource(res, r.drawable.hp);
  bmpmenu = bitmapfactory.decoderesource(res, r.drawable.menu);
  bmpbullet = bitmapfactory.decoderesource(res, r.drawable.bullet);
  bmpenemybullet = bitmapfactory.decoderesource(res, r.drawable.bullet_enemy);
  bmpbossbullet = bitmapfactory.decoderesource(res, r.drawable.boosbullet);
  //爆炸效果容器实例
  vcboom = new vector<boom>();
  //敌机子弹容器实例
  vcbullet = new vector<bullet>();
  //主角子弹容器实例
  vcbulletplayer = new vector<bullet>();
  //菜单类实例
  gamemenu = new gamemenu(bmpmenu, bmpbutton, bmpbuttonpress);
  //实例游戏背景
  background = new gamebg(bmpbackground);
  //实例主角
  player = new player(bmpplayer, bmpplayerhp);
  //实例敌机容器
  vcenemy = new vector<enemy>();
  //实例随机库
  random = new random();
  //---boss相关
  //实例boss对象
  boss = new boss(bmpenemyboos);
  //实例boss子弹容器
  vcbulletboss = new vector<bullet>();
 }
 }

 /**
 * 游戏绘图
 */
 public void mydraw() {
 try {
  canvas = sfh.lockcanvas();
  if (canvas != null) {
  canvas.drawcolor(color.white);
  //绘图函数根据游戏状态不同进行不同绘制
  switch (gamestate) {
  case game_menu:
   //菜单的绘图函数
   gamemenu.draw(canvas, paint);
   break;
  case gameing:
   //游戏背景
   background.draw(canvas, paint);
   //主角绘图函数
   player.draw(canvas, paint);
   if (isboss == false) {
   //敌机绘制
   for (int i = 0; i < vcenemy.size(); i++) {
    vcenemy.elementat(i).draw(canvas, paint);
   }
   //敌机子弹绘制
   for (int i = 0; i < vcbullet.size(); i++) {
    vcbullet.elementat(i).draw(canvas, paint);
   }
   } else {
   //boos的绘制
   boss.draw(canvas, paint);
   //boss子弹逻辑
   for (int i = 0; i < vcbulletboss.size(); i++) {
    vcbulletboss.elementat(i).draw(canvas, paint);
   }
   }
   //处理主角子弹绘制
   for (int i = 0; i < vcbulletplayer.size(); i++) {
   vcbulletplayer.elementat(i).draw(canvas, paint);
   }
   //爆炸效果绘制
   for (int i = 0; i < vcboom.size(); i++) {
   vcboom.elementat(i).draw(canvas, paint);
   }
   break;
  case game_pause:
   break;
  case game_win:
   canvas.drawbitmap(bmpgamewin, 0, 0, paint);
   break;
  case game_lost:
   canvas.drawbitmap(bmpgamelost, 0, 0, paint);
   break;
  }
  }
 } catch (exception e) {
  // todo: handle exception
 } finally {
  if (canvas != null)
  sfh.unlockcanvasandpost(canvas);
 }
 }

 /**
 * 触屏事件监听
 */
 @override
 public boolean ontouchevent(motionevent event) {
 //触屏监听事件函数根据游戏状态不同进行不同监听
 switch (gamestate) {
 case game_menu:
  //菜单的触屏事件处理
  gamemenu.ontouchevent(event);
  break;
 case gameing:
  break;
 case game_pause:
  break;
 case game_win:
  break;
 case game_lost:

  break;
 }
 return true;
 }

 /**
 * 按键按下事件监听
 */
 @override
 public boolean onkeydown(int keycode, keyevent event) {
 //处理back返回按键
 if (keycode == keyevent.keycode_back) {
  //游戏胜利、失败、进行时都默认返回菜单
  if (gamestate == gameing || gamestate == game_win || gamestate == game_lost) {
  gamestate = game_menu;
  //boss状态设置为没出现
  isboss = false;
  //重置游戏
  initgame();
  //重置怪物出场
  enemyarrayindex = 0;
  } else if (gamestate == game_menu) {
  //当前游戏状态在菜单界面,默认返回按键退出游戏
  mainactivity.instance.finish();
  system.exit(0);
  }
  //表示此按键已处理,不再交给系统处理,
  //从而避免游戏被切入后台
  return true;
 }
 //按键监听事件函数根据游戏状态不同进行不同监听
 switch (gamestate) {
 case game_menu:
  break;
 case gameing:
  //主角的按键按下事件
  player.onkeydown(keycode, event);
  break;
 case game_pause:
  break;
 case game_win:
  break;
 case game_lost:
  break;
 }
 return super.onkeydown(keycode, event);
 }

 /**
 * 按键抬起事件监听
 */
 @override
 public boolean onkeyup(int keycode, keyevent event) {
 //处理back返回按键
 if (keycode == keyevent.keycode_back) {
  //游戏胜利、失败、进行时都默认返回菜单
  if (gamestate == gameing || gamestate == game_win || gamestate == game_lost) {
  gamestate = game_menu;
  }
  //表示此按键已处理,不再交给系统处理,
  //从而避免游戏被切入后台
  return true;
 }
 //按键监听事件函数根据游戏状态不同进行不同监听
 switch (gamestate) {
 case game_menu:
  break;
 case gameing:
  //按键抬起事件
  player.onkeyup(keycode, event);
  break;
 case game_pause:
  break;
 case game_win:
  break;
 case game_lost:
  break;
 }
 return super.onkeydown(keycode, event);
 }

 /**
 * 游戏逻辑
 */
 private void logic() {
 //逻辑处理根据游戏状态不同进行不同处理
 switch (gamestate) {
 case game_menu:
  break;
 case gameing:
  //背景逻辑
  background.logic();
  //主角逻辑
  player.logic();
  //敌机逻辑
  if (isboss == false) {
  //敌机逻辑
  for (int i = 0; i < vcenemy.size(); i++) {
   enemy en = vcenemy.elementat(i);
   //因为容器不断添加敌机 ,那么对敌机isdead判定,
   //如果已死亡那么就从容器中删除,对容器起到了优化作用;
   if (en.isdead) {
   vcenemy.removeelementat(i);
   } else {
   en.logic();
   }
  }
  //生成敌机
  count++;
  if (count % createenemytime == 0) {
   for (int i = 0; i < enemyarray[enemyarrayindex].length; i++) {
   //苍蝇
   if (enemyarray[enemyarrayindex][i] == 1) {
    int x = random.nextint(screenw - 100) + 50;
    vcenemy.addelement(new enemy(bmpenemyfly, 1, x, -50));
    //鸭子左
   } else if (enemyarray[enemyarrayindex][i] == 2) {
    int y = random.nextint(20);
    vcenemy.addelement(new enemy(bmpenemyduck, 2, -50, y));
    //鸭子右
   } else if (enemyarray[enemyarrayindex][i] == 3) {
    int y = random.nextint(20);
    vcenemy.addelement(new enemy(bmpenemyduck, 3, screenw + 50, y));
   }
   }
   //这里判断下一组是否为最后一组(boss)
   if (enemyarrayindex == enemyarray.length - 1) {
   isboss = true;
   } else {
   enemyarrayindex++;
   }
  }
  //处理敌机与主角的碰撞
  for (int i = 0; i < vcenemy.size(); i++) {
   if (player.iscollsionwith(vcenemy.elementat(i))) {
   //发生碰撞,主角血量-1
   player.setplayerhp(player.getplayerhp() - 1);
   //当主角血量小于0,判定游戏失败
   if (player.getplayerhp() <= -1) {
    gamestate = game_lost;
   }
   }
  }
  //每2秒添加一个敌机子弹
  countenemybullet++;
  if (countenemybullet % 40 == 0) {
   for (int i = 0; i < vcenemy.size(); i++) {
   enemy en = vcenemy.elementat(i);
   //不同类型敌机不同的子弹运行轨迹
   int bullettype = 0;
   switch (en.type) {
   //苍蝇
   case enemy.type_fly:
    bullettype = bullet.bullet_fly;
    break;
   //鸭子
   case enemy.type_duckl:
   case enemy.type_duckr:
    bullettype = bullet.bullet_duck;
    break;
   }
   vcbullet.add(new bullet(bmpenemybullet, en.x + 10, en.y + 20, bullettype));
   }
  }
  //处理敌机子弹逻辑
  for (int i = 0; i < vcbullet.size(); i++) {
   bullet b = vcbullet.elementat(i);
   if (b.isdead) {
   vcbullet.removeelement(b);
   } else {
   b.logic();
   }
  }
  //处理敌机子弹与主角碰撞
  for (int i = 0; i < vcbullet.size(); i++) {
   if (player.iscollsionwith(vcbullet.elementat(i))) {
   //发生碰撞,主角血量-1
   player.setplayerhp(player.getplayerhp() - 1);
   //当主角血量小于0,判定游戏失败
   if (player.getplayerhp() <= -1) {
    gamestate = game_lost;
   }
   }
  }
  //处理主角子弹与敌机碰撞
  for (int i = 0; i < vcbulletplayer.size(); i++) {
   //取出主角子弹容器的每个元素
   bullet blplayer = vcbulletplayer.elementat(i);
   for (int j = 0; j < vcenemy.size(); j++) {
   //添加爆炸效果
   //取出敌机容器的每个元与主角子弹遍历判断
   if (vcenemy.elementat(j).iscollsionwith(blplayer)) {
    vcboom.add(new boom(bmpboom, vcenemy.elementat(j).x, vcenemy.elementat(j).y, 7));
   }
   }
  }
  } else {//boss相关逻辑
  //每0.5秒添加一个主角子弹
  boss.logic();
  if (countplayerbullet % 10 == 0) {
   //boss的没发疯之前的普通子弹
   vcbulletboss.add(new bullet(bmpbossbullet, boss.x + 35, boss.y + 40, bullet.bullet_fly));
  }
  //boss子弹逻辑
  for (int i = 0; i < vcbulletboss.size(); i++) {
   bullet b = vcbulletboss.elementat(i);
   if (b.isdead) {
   vcbulletboss.removeelement(b);
   } else {
   b.logic();
   }
  }
  //boss子弹与主角的碰撞
  for (int i = 0; i < vcbulletboss.size(); i++) {
   if (player.iscollsionwith(vcbulletboss.elementat(i))) {
   //发生碰撞,主角血量-1
   player.setplayerhp(player.getplayerhp() - 1);
   //当主角血量小于0,判定游戏失败
   if (player.getplayerhp() <= -1) {
    gamestate = game_lost;
   }
   }
  }
  //boss被主角子弹击中,产生爆炸效果
  for (int i = 0; i < vcbulletplayer.size(); i++) {
   bullet b = vcbulletplayer.elementat(i);
   if (boss.iscollsionwith(b)) {
   if (boss.hp <= 0) {
    //游戏胜利
    gamestate = game_win;
   } else {
    //及时删除本次碰撞的子弹,防止重复判定此子弹与boss碰撞、
    b.isdead = true;
    //boss血量减1
    boss.sethp(boss.hp - 1);
    //在boss上添加三个boss爆炸效果
    vcboom.add(new boom(bmpboosboom, boss.x + 25, boss.y + 30, 5));
    vcboom.add(new boom(bmpboosboom, boss.x + 35, boss.y + 40, 5));
    vcboom.add(new boom(bmpboosboom, boss.x + 45, boss.y + 50, 5));
   }
   }
  }
  }
  //每1秒添加一个主角子弹
  countplayerbullet++;
  if (countplayerbullet % 20 == 0) {
  vcbulletplayer.add(new bullet(bmpbullet, player.x + 15, player.y - 20, bullet.bullet_player));
  }
  //处理主角子弹逻辑
  for (int i = 0; i < vcbulletplayer.size(); i++) {
  bullet b = vcbulletplayer.elementat(i);
  if (b.isdead) {
   vcbulletplayer.removeelement(b);
  } else {
   b.logic();
  }
  }
  //爆炸效果逻辑
  for (int i = 0; i < vcboom.size(); i++) {
  boom boom = vcboom.elementat(i);
  if (boom.playend) {
   //播放完毕的从容器中删除
   vcboom.removeelementat(i);
  } else {
   vcboom.elementat(i).logic();
  }
  }
  break;
 case game_pause:
  break;
 case game_win:
  break;
 case game_lost:
  break;

 }
 }

 @override
 public void run() {
 while (flag) {
  long start = system.currenttimemillis();
  mydraw();
  logic();
  long end = system.currenttimemillis();
  try {
  if (end - start < 50) {
   thread.sleep(50 - (end - start));
  }
  } catch (interruptedexception e) {
  e.printstacktrace();
  }
 }
 }

 /**
 * surfaceview视图状态发生改变,响应此函数
 */
 @override
 public void surfacechanged(surfaceholder holder, int format, int width, int height) {
 }

 /**
 * surfaceview视图消亡时,响应此函数
 */
 @override
 public void surfacedestroyed(surfaceholder holder) {
 flag = false;
 }
}

以及胜利和失败页面

最后附上 mainactivit

public class mainactivity extends activity {
 public static mainactivity instance;
 @override
 public void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 instance = this;
 //设置全屏
 this.getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen);
 requestwindowfeature(window.feature_no_title);
 //显示自定义的surfaceview视图
 setcontentview(new mysurfaceview(this));
 }

 @override
 protected void ondestroy() {
 // todo auto-generated method stub
 mysurfaceview.gamestate = mysurfaceview.game_menu;
 super.ondestroy();
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。