首页 > Unity3D专栏 > 【Unity3D】纯GUI实现Flappy Bird
2017
07-25

【Unity3D】纯GUI实现Flappy Bird

直接将下面的脚本随意挂在任何GameObject上即可

using UnityEngine;  
using System.Collections;  
using System.Collections.Generic;  
   
public class FlappyBox : MonoBehaviour  
{  
        private float cStartY;  
        private float y;  
        private float x;  
        private float size;  
        private float brockSize;  
        private const float cacc = -500;  
        private float acc;  
        private List<BrockCell> lCells = new List<BrockCell>();  
        private const float csep = 3;  
        private float fsep = csep;  
        private bool bStart = false;  
        private bool bGameOver = false;  
        private float btnSize;  
        private int score;  
        private int hightScore;  
        private bool bHighScore = false;  
        private float cFloorSize = 50;  
        void ResetMe()  
        {  
                y = cStartY;  
                acc = 0;  
                lCells.Clear();  
                fsep = csep;  
                bStart = true;  
                bGameOver = false;  
                bHighScore = false;  
                score = 0;  
                x = Screen.width / 2 - size * 2;  
        }  
        void Start()  
        {  
                btnSize = Screen.height / 10;  
                size = Screen.height / 20;  
                brockSize = size * 5;  
                x = Screen.width / 2 - size * 2;  
                cStartY = Screen.height / 2 - size / 2;  
                y = cStartY;  
                hightScore = PlayerPrefs.GetInt(cHighScorePref);  
        }  
        void OnGUI()  
        {  
                GUI.Box(new Rect(-10, Screen.height - cFloorSize, Screen.width + 20, cFloorSize + 10), "");  
                GUI.Box(new Rect(x, y, size, size), "");  
                foreach (BrockCell bc in lCells)  
                {  
                        GUI.Box(new Rect(bc.x, 0, bc.size, bc.upy), "");  
                        GUI.Box(new Rect(bc.x, bc.downy, bc.size, Screen.height), "");  
                }  
   
                if (bGameOver)  
                {  
                        float btnx = Screen.width / 2 - btnSize / 2;  
                        float btny = Screen.height / 2 - btnSize / 2;  
                        if (GUI.Button(new Rect(btnx, btny, btnSize, btnSize / 2), "Restart"))  
                        {  
                                ResetMe();  
                        }  
                        GUI.Label(new Rect(btnx, btny + btnSize / 2 + 5, btnSize, btnSize / 2), "score:" + score);  
                        GUI.Label(new Rect(btnx, btny + (btnSize / 3 + 5)*2, btnSize, btnSize / 2), "high score:" + hightScore);  
                        if (bHighScore)  
                        {  
                                GUI.Box(new Rect(btnx, btny + (btnSize / 2 + 10) * 2, btnSize*2, btnSize / 3), "new high score!");  
                        }  
                           
                        return;  
                }  
   
                if (!bStart)  
                {  
                        return;  
                }  
   
                GUI.Label(new Rect(10, 10, 100, 20), "score:" + score);  
        }  
        const string cHighScorePref = "highscore";  
        void OnGameOver()  
        {  
                bGameOver = true;  
                if (score > PlayerPrefs.GetInt(cHighScorePref))  
                {  
                        PlayerPrefs.SetInt(cHighScorePref, score);  
                        hightScore = score;  
                        bHighScore = true;  
                }  
        }  
        private const float c_g = 980;  
        void Update()  
        {  
                if (bGameOver)  
                {  
                        if (y + size < Screen.height - cFloorSize)  
                        {  
                                acc += Time.deltaTime * c_g;  
                                y += Time.deltaTime * acc;  
                        }  
                        return;  
                }  
   
                if (Input.GetMouseButtonDown(0))  
                {  
                        if (!bStart)  
                        {  
                                ResetMe();  
                        }  
                        acc = cacc;  
                }  
                if (!bStart)  
                {  
                        return;  
                }  
   
                fsep -= Time.deltaTime;  
                if (fsep < 0)  
                {  
                        fsep = csep;  
                        lCells.Add(new BrockCell());  
                }  
   
                foreach (BrockCell bc in lCells)  
                {  
                        bc.x -= Time.deltaTime * 100;  
   
                        if (x + size > bc.x  
                                && (y < bc.upy || y + size > bc.downy)  
                                && x < bc.x + size  
                                && (y < bc.upy || y + size > bc.downy))  
                        {  
                                OnGameOver();  
                        }  
                        else if (!bc.bPassed && x > bc.x + bc.size / 2)  
                        {  
                                score++;  
                                bc.bPassed = true;  
                        }  
                }  
   
                acc += Time.deltaTime * c_g;  
                y += Time.deltaTime * acc;  
   
                if (y < 0 || y + size > Screen.height - cFloorSize)  
                {  
                        OnGameOver();  
                }  
        }  
}  
   
public class BrockCell  
{  
        public float upy;  
        public float downy;  
        public float size;  
        public float x;  
        public bool bPassed;  
        public BrockCell()  
        {  
                size = Screen.height / 16;  
                x = Screen.width + size;  
                upy = Screen.height / 20 * Random.Range(0, 13);  
                downy = upy + Screen.height / 20 * 5.5f;  
        }  
}  

 



虚拟世界提醒您:如果您觉得本文不错,快快将这篇文章分享出去吧 ,感谢您的支持和关注,谢谢!
最后编辑:
作者:虚拟世界
专注互联网技术及资讯 技术研究中心|有相关Unity3D问题或者作事宜请联系我哦!
捐 赠如果您愿意花10块钱请我喝一杯咖啡的话,请用手机扫描二维码即可通过支付宝或者微信直接向我捐款,在此非常感谢您对虚拟世界的捐赠。

留下一个回复

你的email不会被公开。