热门标签 | HotTags
当前位置:  开发笔记 > 运维 > 正文

Android实战打飞机游戏之怪物(敌机)类的实现(4)

这篇文章主要为大家详细介绍了Android实战打飞机游戏之怪物(敌机)类的实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

先看看效果图:

分析: 根据敌机类型区分 敌机 运动逻辑 以及绘制

/**
 * 敌机
 * 
 * @author liuml
 * @time 2016-5-31 下午4:14:59
 */
public class Enemy {

  // 敌机的种类标识
  public int type;
  // 苍蝇
  public static final int TYPE_FLY = 1;
  // 鸭子(从左往右运动)
  public static final int TYPE_DUCKL = 2;
  // 鸭子(从右往左运动)
  public static final int TYPE_DUCKR = 3;
  // 敌机图片资源
  public Bitmap bmpEnemy;
  // 敌机坐标
  public int x, y;
  // 敌机每帧的宽高
  public int frameW, frameH;
  // 敌机当前帧下标
  private int frameIndex;
  // 敌机的移动速度
  private int speed;;
  // 判断敌机是否已经出屏
  public boolean isDead;

  // 敌机的构造函数
  public Enemy(Bitmap bmpEnemy, int enemyType, int x, int y) {
    this.bmpEnemy = bmpEnemy;
    frameW = bmpEnemy.getWidth() / 10;
    frameH = bmpEnemy.getHeight();
    this.type = enemyType;
    this.x = x;
    this.y = y;
    // 不同种类的敌机血量不同
    switch (type) {
    // 苍蝇
    case TYPE_FLY:
      speed = 25;
      break;
    // 鸭子
    case TYPE_DUCKL:
      speed = 3;
      break;
    case TYPE_DUCKR:
      speed = 3;
      break;
    }
  }

  // 敌机绘图函数
  public void draw(Canvas canvas, Paint paint) {
    canvas.save();
    canvas.clipRect(x, y, x + frameW, y + frameH);
    canvas.drawBitmap(bmpEnemy, x - frameIndex * frameW, y, paint);
    canvas.restore();
  }

  // 敌机逻辑AI
  public void logic() {
    // 不断循环播放帧形成动画
    frameIndex++;
    if (frameIndex >= 10) {
      frameIndex = 0;
    }
    // 不同种类的敌机拥有不同的AI逻辑
    switch (type) {
    case TYPE_FLY:
      if (isDead == false) {
        // 减速出现,加速返回
        speed -= 1;
        y += speed;
        if (y <= -200) {
          isDead = true;
        }
      }
      break;
    case TYPE_DUCKL:
      if (isDead == false) {
        // 斜右下角运动
        x += speed / 2;
        y += speed;
        if (x > MySurfaceView.screenW) {
          isDead = true;
        }
      }
      break;
    case TYPE_DUCKR:
      if (isDead == false) {
        // 斜左下角运动
        x -= speed / 2;
        y += speed;
        if (x <-50) {
          isDead = true;
        }
      }
      break;
    }
  }

}

在MySurfaceView 中 生成敌机

public class MySurfaceView extends SurfaceView implements Callback, Runnable {
  private SurfaceHolder sfh;
  private Paint paint;
  private Thread th;
  private boolean flag;
  private Canvas canvas;

  // 1 定义游戏状态常量
  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子弹
  public static int screenW;
  public static int screenH;

  // 声明一个敌机容器
  private Vector 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 GameMenu gameMenu;
  private GameBg gameBg;

  private Player player;

  /**
   * 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);
  }

  /**
   * 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() {
    // 加载游戏资源
    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);

    // 菜单类实例化
    gameMenu = new GameMenu(bmpMenu, bmpButton, bmpButtonPress);
    // 实例游戏背景
    gameBg = new GameBg(bmpBackGround);
    // 实例主角
    player = new Player(bmpPlayer, bmpPlayerHp);

    // 实例敌机容器
    vcEnemy = new Vector();
    // 实例随机库
    random = new Random();
  }

  /**
   * 游戏绘图
   */
  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:
          gameBg.draw(canvas, paint);
          player.draw(canvas, paint);
          if (isBoss == false) {
            // 敌机绘制
            for (int i = 0; i 

碰撞检测
修改Player类

package com.gsf;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.KeyEvent;

public class Player {

  private int playerHp = 3;

  private Bitmap bmpPlayerHP;
  // 主角坐标以及位图
  private int x, y;
  private Bitmap bmpPlayer;
  // 主角移动速度

  private int speed = 5;
  // 主角移动标识
  private boolean isUp, isDown, isLeft, isRight;

  // 主角的构造函数
  public Player(Bitmap bmpPlayer, Bitmap bmpPlayerHp) {
    this.bmpPlayer = bmpPlayer;
    this.bmpPlayerHP = bmpPlayerHp;
    // 飞机初始位置
    x = MySurfaceView.screenW / 2 - bmpPlayer.getWidth() / 2;
    y = MySurfaceView.screenH - bmpPlayer.getHeight();
  }

  // 主角游戏绘制方法
  public void draw(Canvas canvas, Paint paint) {

    // 绘制主角
    canvas.drawBitmap(bmpPlayer, x, y, paint);
    // 绘制血量

    for (int i = 0; i = MySurfaceView.screenW) {
      x = MySurfaceView.screenW - bmpPlayer.getWidth();
    } else if (x <= 0) {
      x = 0;
    }
    // 判断屏幕Y边界
    if (y + bmpPlayer.getHeight() >= MySurfaceView.screenH) {
      y = MySurfaceView.screenH - bmpPlayer.getHeight();
    } else if (y <= 0) {
      y = 0;
    }

  }


  //设置主角血量
  public void setPlayerHp(int hp) {
    this.playerHp = hp;
  }

  //获取主角血量
  public int getPlayerHp() {
    return playerHp;
  }


  //判断碰撞(敌机与主角子弹碰撞)
  public boolean isCollsionWith(Enemy bullet) {
      int x2 = bullet.x;
      int y2 = bullet.y;
      int w2 = bullet.frameW;
      int h2 = bullet.frameH;
      if (x >= x2 && x >= x2 + w2) {
        return false;
      } else if (x <= x2 && x + bmpPlayer.getWidth() <= x2) {
        return false;
      } else if (y >= y2 && y >= y2 + h2) {
        return false;
      } else if (y <= y2 && y + bmpPlayer.getHeight() <= y2) {
        return false;
      }
      //发生碰撞,让其死亡
      //isDead = true;
      return true;
    }


}

在MySurface中 加上碰撞逻辑

  /**
   * 游戏逻辑
   */
  private void logic() {
    switch (gameState) {
    case GAME_MENU:

      break;
    case GAMEING:
      gameBg.logic();
      player.logic();
      // 敌机逻辑
      if (isBoss == false) {
        // 敌机逻辑
        for (int i = 0; i 

// 计时器
  private int noCollisiOnCount= 0;
  // 因为无敌时间
  private int noCollisiOnTime= 60;
  // 是否碰撞的标识位
  private boolean isCollision;

//判断碰撞(主角与敌机)
  public boolean isCollsionWith(Enemy en) {
    //是否处于无敌时间
    if (isCollision == false) {
      int x2 = en.x;
      int y2 = en.y;
      int w2 = en.frameW;
      int h2 = en.frameH;
      if (x >= x2 && x >= x2 + w2) {
        return false;
      } else if (x <= x2 && x + bmpPlayer.getWidth() <= x2) {
        return false;
      } else if (y >= y2 && y >= y2 + h2) {
        return false;
      } else if (y <= y2 && y + bmpPlayer.getHeight() <= y2) {
        return false;
      }
      //碰撞即进入无敌状态
      isCollision = true;
      return true;
      //处于无敌状态,无视碰撞
    } else {
      return false;
    }
  }

修改逻辑方法

  /**
   * 游戏逻辑
   */
  public void logic() {
    if (isUp) {
      y -= speed;
    }
    if (isDown) {
      y += speed;
    }
    if (isLeft) {
      x -= speed;
    }
    if (isRight) {
      x += speed;
    }
    // 判断屏幕X边界
    if (x + bmpPlayer.getWidth() >= MySurfaceView.screenW) {
      x = MySurfaceView.screenW - bmpPlayer.getWidth();
    } else if (x <= 0) {
      x = 0;
    }
    // 判断屏幕Y边界
    if (y + bmpPlayer.getHeight() >= MySurfaceView.screenH) {
      y = MySurfaceView.screenH - bmpPlayer.getHeight();
    } else if (y <= 0) {
      y = 0;
    }

    // 处理无敌状态
    if (isCollision) {
      // 计时器开始计时
      noCollisionCount++;
      if (noCollisionCount >= noCollisionTime) {
        // 无敌时间过后,接触无敌状态及初始化计数器
        isCollision = false;
        noCollisiOnCount= 0;
      }
    }

  }

修改主角的绘制
Player 类

// 主角游戏绘制方法
  public void draw(Canvas canvas, Paint paint) {

    // 绘制主角
    // 当处于无敌时间时,让主角闪烁
    if (isCollision) {
      // 每2次游戏循环,绘制一次主角
      if (noCollisionCount % 2 == 0) {
        canvas.drawBitmap(bmpPlayer, x, y, paint);
      }
    } else {
      canvas.drawBitmap(bmpPlayer, x, y, paint);
    }
    // 绘制血量

    for (int i = 0; i 

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


推荐阅读
  • 基于layUI的图片上传前预览功能的2种实现方式
    本文介绍了基于layUI的图片上传前预览功能的两种实现方式:一种是使用blob+FileReader,另一种是使用layUI自带的参数。通过选择文件后点击文件名,在页面中间弹窗内预览图片。其中,layUI自带的参数实现了图片预览功能。该功能依赖于layUI的上传模块,并使用了blob和FileReader来读取本地文件并获取图像的base64编码。点击文件名时会执行See()函数。摘要长度为169字。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • Android中高级面试必知必会,积累总结
    本文介绍了Android中高级面试的必知必会内容,并总结了相关经验。文章指出,如今的Android市场对开发人员的要求更高,需要更专业的人才。同时,文章还给出了针对Android岗位的职责和要求,并提供了简历突出的建议。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文讲述了如何通过代码在Android中更改Recycler视图项的背景颜色。通过在onBindViewHolder方法中设置条件判断,可以实现根据条件改变背景颜色的效果。同时,还介绍了如何修改底部边框颜色以及提供了RecyclerView Fragment layout.xml和项目布局文件的示例代码。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • baresip android编译、运行教程1语音通话
    本文介绍了如何在安卓平台上编译和运行baresip android,包括下载相关的sdk和ndk,修改ndk路径和输出目录,以及创建一个c++的安卓工程并将目录考到cpp下。详细步骤可参考给出的链接和文档。 ... [详细]
  • 【Windows】实现微信双开或多开的方法及步骤详解
    本文介绍了在Windows系统下实现微信双开或多开的方法,通过安装微信电脑版、复制微信程序启动路径、修改文本文件为bat文件等步骤,实现同时登录两个或多个微信的效果。相比于使用虚拟机的方法,本方法更简单易行,适用于任何电脑,并且不会消耗过多系统资源。详细步骤和原理解释请参考本文内容。 ... [详细]
  • Android Studio Bumblebee | 2021.1.1(大黄蜂版本使用介绍)
    本文介绍了Android Studio Bumblebee | 2021.1.1(大黄蜂版本)的使用方法和相关知识,包括Gradle的介绍、设备管理器的配置、无线调试、新版本问题等内容。同时还提供了更新版本的下载地址和启动页面截图。 ... [详细]
author-avatar
个信2602926933
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有