您的位置:首页 > 移动开发

flappy brid 开发实录(基于3d环境)

2016-03-02 22:11 507 查看
一、游戏场景逻辑:

创建三个游戏面块,通过迭代改变面块的位置来生成无尽模式的游戏场景,通过常量来控制游戏状态

二、开发顺序:

1、场景搭建

2、主角控制

3、声音添加与游戏管理器

4、设计UI  
三、开发难点

一、小鸟动作

 动画计时器原理:
1.定义3个变量:

       timer=0 时间常量记录时间的流逝
      frame_Count=10(一秒10帧) 规定动作的帧数
      frame_Number= 0告诉游戏当前播放第几帧
2.思路逻辑
每次更新游戏界面,timer加上更新所用时间,当timer的大小大于播放
    一帧需要的时间时,播放动作,减去加上的时间常量,然后动作帧数(frame
    _Number)自加(下次需要播放后一帧)

         3.代码实现: public int farme_Count = 10;//帧数
private float timer = 0;//时间常量
private int farmer_Number = 0;//动作帧数

void Update ()
{
timer += Time.deltaTime;
if (timer > 1.0f / farme_Count)
{
timer -= 1.0f / farme_Count;
int farmer_index = farmer_Number % 3;
this.GetComponent<Renderer> ().material.SetTextureOffset ("_MainTex", new Vector2 (farmer_index*0.33333f, 0));
//标签"_MainTex"指的是该材质里的主要图片(有的材质存在多张texture)
farmer_Number++;

}
}


二、场景迭代

1思路:有三个块面来迭代游戏场景

2.迭代原理:在每个块面里添加一个触发器,当Player进入触发器时,将当前的这个块面移动到

最后一个块面的后面,使用单例模式创建一个gamemanager类,实例化为主摄像机,再添加一个

3.fristbg的位置信息(transform)来存储最后一个块面的位置信息;
4.代码实现:
//单例模式下的gamemanager,和声明位置信息属性

using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour {

public Transform Fristbg;
public static GameManager _instance;
void Awake(){
_instance = this;
}

}

//场景面块的迭代和位置信息的储存
using UnityEngine;
using System.Collections;

public class MoveTrigger : MonoBehaviour
{

public Transform bg_position;
//bg_position 可以赋值为当前面块的位置信息

public void OnTriggerEnter(Collider other)
{
if(other.tag=="Player")
{  //更改面块的位置
bg_position.position=new Vector3(GameManager._instance.Fristbg.position.x+6.24f,bg_position.position.y,bg_position.position.z);
//将最后一个面块的位置信息改为当前面块的位置
GameManager._instance.Fristbg = bg_position;

}

}

}
三、游戏管理器(GameManager)

创建一个类,实例化为Main Canmera,用于控制游戏状态,储存游戏得分,控制小鸟的横向移动速度

游戏状态通过三个常量来控制,代码如下:
public class GameManager : MonoBehaviour {
//定义游戏状态

public static int GAMEMUNE = 0;
public static int GAMEING = 1;
public static int GAMROVER = 2;
public int GameState=GAMEMUNE;
private GameObject brid;
public int x_Force = 100;
public int Source = 0;

//场景迭代:单例模式
public static GameManager _instance;
public Transform Fristbg;
void Awake(){
_instance = this;
brid=GameObject.FindGameObjectWithTag("Player");
}

//游戏启动

void Update() {

if(this.GameState==GAMEMUNE)

{
if (Input.GetMouseButtonUp(0))
{
GameState = GAMEING;
brid.GetComponent<Rigidbody>().useGravity = true;
brid.GetComponent<Rigidbody>().AddForce(new Vector3(x_Force, 0, 0));
this.GetComponent<AudioSource>().Play();

}
}

}
}

这是笔者刚刚学习u3d,参考了taikr网上,siki老师的设计思路,和taikr网提供的素材来编写,笔者刚刚学习u3d,很多不足之处希望各位指出,让笔者可以学习。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unity flabby bird 场景