本文实例为大家分享了打飞机游戏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 VectorvcEnemy; //每次生成敌机的时间(毫秒) 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 vcBullet; //添加子弹的计数器 private int countEnemyBullet; //主角子弹容器 private Vector vcBulletPlayer; //添加子弹的计数器 private int countPlayerBullet; //爆炸效果容器 private Vector vcBoom; //声明Boss private Boss boss; //Boss的子弹容器 public static Vector 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 (); //敌机子弹容器实例 vcBullet = new Vector (); //主角子弹容器实例 vcBulletPlayer = new Vector (); //菜单类实例 gameMenu = new GameMenu(bmpMenu, bmpButton, bmpButtonPress); //实例游戏背景 backGround = new GameBg(bmpBackGround); //实例主角 player = new Player(bmpPlayer, bmpPlayerHp); //实例敌机容器 vcEnemy = new Vector (); //实例随机库 random = new Random(); //---Boss相关 //实例boss对象 boss = new Boss(bmpEnemyBoos); //实例Boss子弹容器 vcBulletBoss = new Vector (); } } /** * 游戏绘图 */ 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
以及胜利和失败页面
最后附上 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(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。