纳金网
标题:
单脚本纯GUI实现 Flappy Bird (转载)
[打印本页]
作者:
刀锋狼
时间:
2014-4-30 18:31
标题:
单脚本纯GUI实现 Flappy Bird (转载)
这款游戏其实实现起来很简单,正如那个越南人说的,只是花了两三天时间就做出来了,但是它火的如此迅猛,如此诡异,各中原因还是很值得借鉴的。
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
lCells = new List
();
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;
}
}
复制代码
作者:
xx232
时间:
2014-4-30 19:53
小笨鸟都火过头了 哎 搞不懂啊搞不懂
作者:
wusky751023
时间:
2014-5-7 02:50
越式朝不同角度思考,越能滿足不同遊戲玩家啊!!!@@
好東西,學習去!@@
欢迎光临 纳金网 (http://rs.narkii.com/club/)
Powered by Discuz! X2.5