当前位置: 移动技术网 > IT编程>开发语言>c# > Unity实现Flappy Bird游戏开发实战

Unity实现Flappy Bird游戏开发实战

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了unity实现flappy bird游戏的具体代码,供大家参考,具体内容如下

参考:腾讯课程(零基础制作像素鸟)
环境:unity2017.2.0f3

主界面(main)的制作

没有什么技巧性

注意点:

1.写好button的点击效果,并在ui上添加效果
2.切换界面的实现不需要通过load,直接设置setactive()true or false 来的更快更效率

// 比如:当点击打开解释说明的按钮时候
  public void clickopenexplainscene() {
    if (!explainscene.activeself) {
      explainscene.setactive (true);
    } 
    if (startscene.activeself) {
      startscene.setactive (false);
    }  
  }

2.因为不管是哪个场景,背景音乐都只有一个。所以背景音乐通过单例模式实现

// 实现背景音乐播放的单例类
using system.collections;
using system.collections.generic;
using unityengine;

public class bgsingleton : monobehaviour {

  private static bgsingleton instance = null;
  public audiosource audiosource = null;

  public static bgsingleton getsingleton() {
    if (instance == null) {
      instance = new bgsingleton ();
    }
    return instance;
  }
  void awake () {
    if (instance != null && instance != this) {
      destroy (this.gameobject);
    } else {
      instance = this;
      debug.log ("create");
    }
    dontdestroyonload (this.gameobject);
  }

//通过主界面上的开关button控制是否静音
  public void isplay(bool isplay) {
    if (isplay == true) {
      audiosource.mute = false;
      debug.log ("play background music");
    } else {
      audiosource.mute = true;
    }
  }
}

游戏主场景

1 场景的来回切换

通过2个场景,来回播放实现
前一个场景完全移除屏幕后,立刻重新设置坐标,并且让柱子的位置随机出现

// mapmove.cs
using system.collections;
using system.collections.generic;
using unityengine;

public class mapmove : monobehaviour {

  public float speed = 300f;
  public recttransform tube1;
  public recttransform tube2;

  private recttransform transform;
  // use this for initialization
  void awake () {
    transform = getcomponent<recttransform>(); 
  }

  // update is called once per frame
  void update () {

    // translate:moves the transform in the direction and distance of translation.

    // if you add or subtract to a value every frame chances are you should multiply with time.deltatime.
    // when you multiply with time.deltatime you essentially express: 
    // i want to move this object 10 meters per second instead of 10 meters per frame.
    transform.translate (vector3.left * time.deltatime * speed);

    // 如果前一个地图移到-764以外的地方,重放到764
    if (transform.anchoredposition.x <= -764) {

      transform.anchoredposition = new vector2 (764, 0);
      // 设置水管的位置为随机出现,x不变,y随机出现
      tube1.anchoredposition = new vector2 (tube1.anchoredposition.x, random.range (-110, 200));
      tube2.anchoredposition = new vector2 (tube2.anchoredposition.x, random.range (-110, 200));
    }
  }
}

2 主要是鸟和柱子接触后产生的碰撞检测,首先需要设置鸟和柱子都为is trigger触发器,因为这里不需要物理的碰撞效果

提前给所有柱子和上下面设置好tag,通过tag检测碰撞,停止移动地图并销毁鸟

// 碰撞检测
  void ontriggerenter2d (collider2d other)
  {
    // 如果鸟碰到柱子
    if (other.gameobject.comparetag("tube")) {

      if (!gameover.activeself) {
        gameover.setactive (true);
        audiomanager.singer.setaudio (audiocliptype.hit);
      }

      // 通过地图的名字获取到地图移动脚本
      mapmove map1 = gameobject.find ("map1").getcomponent<mapmove> ();
      map1.enabled = false;

      mapmove map2 = gameobject.find ("map2").getcomponent<mapmove> ();
      map2.enabled = false;

      rigidbody2d playerrigidbody2d = gameobject.find ("player").getcomponent<rigidbody2d> ();
      destroy (playerrigidbody2d);

    }
  }

3 音效设置和鸟的朝向问题

因为鸟震动翅膀的声音需要和其他音效不是一个线程,所以只能单独领出来写

// playercontroller.cs
using system.collections;
using system.collections.generic;
using unityengine;

public class playercontroller : monobehaviour {

  private rigidbody2d player;

  public audiosource playeraudio;
  public float speed;
  // use this for initialization
  void start () {
    player = getcomponent<rigidbody2d>();
    // 重置分数
    gamesingleton.getsingleton ().score = 0;
  }

  // update is called once per frame
  void update () {
    // 如果鸟被销毁游戏结束了,直接返回
    if (player == null) { return; }

    // 当点击鼠标,给鸟一个向上的速度
    if (input.getmousebuttondown (0)) {
      player.velocity = new vector2(0, speed);
//     audiomanager.singer.setaudio (audiocliptype.wing);
      // 因为翅膀的声音和过柱子的声音,不能是同个线程的
      if (!gamesingleton.getsingleton ().ismute) {
        playeraudio.play();
      }

    }

    // 通过判断鸟的速度正负设计鸟的头的转向,
    if (player.velocity.y > 0) {
      transform.eulerangles = new vector3 (0, 0, 45);
    } else {
      transform.eulerangles = new vector3 (0, 0, -45);
    }
  }

}

4 分数的计算

这里需要再次用到触碰检测,给柱子之间空隙加个透明的检测器,每次一过柱子就加一分

用单例类存储分数等数据:

// gamesingleton.cs
using system.collections;
using system.collections.generic;
using unityengine;

public class gamesingleton {

  // 使用单例模式记录分数
  // 显然单例模式的要点有三个;一是某个类只能有一个实例;
  // 二是它必须自行创建这个实例;三是它必须自行向整个系统提供这个实例。
  // 从具体实现角度来说,就是以下三点:一是单例模式的类只提供私有的构造函数,
  // 二是类定义中含有一个该类的静态私有对象,三是该类提供了一个静态的公有的函数用于创建或获取它本身的静态私有对象。

  public float score;
  public float bestscore;
  public bool ismute; // 用来控制音效
// public bool isfirsttoplay;

  // 含有一个静态私有对象,这也是唯一一个对象
  private static gamesingleton singer;

  // 私有的构造函数
  private gamesingleton () {
    score = 0;
    bestscore = 0;
    ismute = false;
//   isfirsttoplay = true;
  }

  // 提供一个静态的公有函数 用于创建或获取本身的静态私有对象
  public static gamesingleton getsingleton() {
    if (singer == null) {
      singer = new gamesingleton ();
    }
    return singer;
  }

  public void setbestscore() {
    if (score > bestscore) {
      bestscore = score;
    }
  }

}

5 最后的gameover界面的动画效果,可以通过unity的animation窗口制作

导入到ios设备上

file- buildsetting - 加入所有场景,- ios - build
会产生xcode的项目文件

在xcode中打开,在general里设置下证书(网上教程很多,不需要99刀也能真机测试)

主要遇到的问题

  • 为了设置分辨率需要调出game窗口
  • 类中的变量,需要提前初始化好,不然就为空报错。要么从ui上吧对应的组件拖下来,要么写一句getcomponent
  • bgm需要拖出来自立个类写,因为他不随场景变化而消失或者重复创建
  • 真机测试的时候,bundle ldentifier不能乱写,假如出现security问题,需要在手机-通用-描述文件与设备管理中,点击信任你的苹果账号。

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网