单脚本纯GUI实现《Flappy Bird》

23 篇文章 0 订阅

原文链接 : http://www.manew.com/thread-95921-1-1.html


效果图如下:


using UnityEngine;
using System.Collections;
using System.Collections.Generic;
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;
    }
}


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 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 bool bHighScore = false;
    private int hightScore;

    private float cFloorSize = 50;
    private float acc;
    private int score = 0;
    private string cHighScorePref = "highest";
    void Reset()
    {
        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), "1");
        foreach (BrockCell  bc in lCells)
        {
            GUI.Box(new Rect(bc.x, 0, bc.size, bc.upy), "2");
            GUI.Box(new Rect(bc.x, bc.downy, bc.size, Screen.height), "3");
        }
        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"))
            {
                Reset();
            }
            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);
    }

    void OnGameOver()
    {
        bGameOver = true;
        if(score > PlayerPrefs.GetInt(cHighScorePref,score))
        {
            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) Reset();
            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();
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值