FlappyBird不用多说了,一款极其简单,但是又很火的游戏。我在得知这款疯狂的游戏后,就有一种把它重现的冲动,然后花了我4个多小时,产生出了一个可以玩的版本,分享给大家(文末尾付下载链接)。
下面简单介绍游戏的开发过程(本文的例子需要使用unity4.3.0以上的版本打开)。
public class road : MonoBehaviour {
....
// Update is called once per frame
void Update () {
Vector3 pos = trans.position;
pos.x -= speed * Time.deltaTime;
trans.position = pos;
if(pos.x <= -1.6f - 3.35f*idx) { //当道路移出屏幕后,从小调整其位置
Vector3 pp = roads[idx%2].transform.position;
pp.x += 3.35f;
idx++;
roads[idx%2].transform.position = pp;
if(isBegin){
roads[idx%2].GetComponent().gen();
}
}
}
public class roadGen : MonoBehaviour {
public GameObject[] zhuzi;
public float down=3.8f, upper = 6.0f; ...
public void gen() { // 一格道路有两个柱子
zhuzi[0].SetActive(true);
zhuzi[1].SetActive(true);
Vector3 p = zhuzi[0].transform.localPosition;
float vv = Random.value;
p.y = Mathf.Lerp(down, upper, vv);
zhuzi[0].transform.localPosition = p; //设置第一个柱子的位置
p = zhuzi[1].transform.localPosition;
vv = Random.value;
p.y = Mathf.Lerp(down, upper, vv);
zhuzi[1].transform.localPosition = p; //设置第二个柱子的位置
}
public void hidden() {
zhuzi[0].SetActive(false);
zhuzi[1].SetActive(false);
}
}
using UnityEngine;
using System.Collections;
public class clider : MonoBehaviour {
public score s;
public int clideNum;
public string tag;
public bool isSuccess = false, isFail = false;
// Use this for initialization
void Start () {
s = GameObject.Find("score").GetComponent();
clideNum = 0;
tag = "";
isSuccess = false;
isFail = false;
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D(Collider2D other) {
if(other.gameObject.tag.Equals("success")) {
if(!isSuccess) {
print("===trigger enter==");
isSuccess = true;
s.success();
print ("success");
}
} else if(!isFail) {
print("===trigger enter==");
isFail = true;
s.fail();
print ("fail");
}
}
void OnTriggerExit2D(Collider2D other) {
print("===trigger exit==");
isSuccess = false;
}
void OnCollisionEnter2D(Collision2D other) {
if(other.gameObject.tag.Equals("success")) {
if(!isSuccess) {
print("===collision enter==");
isSuccess = true;
s.success();
}
} else if(!isFail) {
print("===collision enter==");
isFail = true;
s.fail();
}
}
void OnCollisionExit2D(Collision2D coll) {
print("===collision exit==");
isSuccess = false;
}
public void reset() {
isSuccess = false;
isFail = false;
}
}
(补充:回过头看一下这个代码,由于当时写的时候对Trigger和Collision理解不够,发现Trigger和Collision处理的不太优雅;关于Trigger和Collision区别,请参考《【Unity】技巧集合》中的51和52条。)
public class isReady : MonoBehaviour {
public GameObject road, bird;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetButtonDown("Fire1")){ //用户触摸屏幕之后,就开始游戏了
gameObject.SetActive(false);
road.GetComponent().isBegin = true;
bird.GetComponent().isKinematic = false;//欢迎页面,这里设置为true,使小鸟不响应重力,开始后要设置为false
bird.GetComponent().enabled = false;
}
}
}
public class restart : MonoBehaviour {
public Camera cam2d;
public GameObject ready;
void Update () {
if(Input.GetButtonDown("Fire1")){
Collider2D h = Physics2D.OverlapPoint(cam2d.ScreenToWorldPoint(Input.mousePosition), (1<
声明:这篇文章中所引用的资源部分来自网络,仅供学习之用,请勿商业化。
下载地址:http://download.csdn.net/detail/stalendp/6914227
apk下载链接:http://pan.baidu.com/s/1c09rKMc