原文链接 : 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();
}
}
}