【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;
        }
}


猜你喜欢

转载自blog.csdn.net/hackdjh/article/details/41721935