1. 程式人生 > >小遊戲--旋轉的小球

小遊戲--旋轉的小球

這是個小遊戲,最早出現在某個頁遊平臺,玩法簡單,先上圖:


遊戲主要分為3各部分, 1.小球旋轉(這個太簡單了,只有幾句程式碼); 2. 生成併發射 針(我反正覺得像針); 3.檢測發射結果(加分或者失敗)

基本上沒有什麼難點,主要是檢測碰撞的針頭一定要綁碰撞器和剛體,用2D的就行了, 剛體記得要把重力設為0,不然針頭會往下掉;

針頭的tag可以新建一個 "Head",方便識別,或者就用已有的也行

程式碼有2個指令碼,HeadCheck是綁在針頭上的,另外一個放在最上層節點即可;所有Public物件是手動繫結的

using UnityEngine;
using System.Collections;

public class HeadCheck : MonoBehaviour 
{
    public EveryBitOfTime _GameRoot;

    //碰撞檢測,需要繫結碰撞器(Collider2D)和 rigidbody2D
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "Head")
        {
            Debug.Log("GG");
            _GameRoot.IsGameOver = true;
        }

    }
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class EveryBitOfTime : MonoBehaviour 
{

    public Transform _circle;//轉動的圓圈
    public Text _txtScore;//得分
    public Transform _needle_Prefab, _root;//針預製體
    public GameObject _text_GG, _btn_Restart;

    Transform _needle_Fly, _needle_Start;
    float _speed = -30, _lv;//轉動速度, 難度
    bool _isShoot = false, _isFly = false, _isGameOver = false;//已經發射, 正在飛行中
    int _score = 0;
    Vector3 _end;
    public bool IsGameOver
    {
        get { return _isGameOver; }
        set { _isGameOver = value; }
    }
    


	// Use this for initialization
	void Start () 
    {
        _end = _circle.position;
        _end.y -= 0.55f;//去掉半徑\
        _text_GG.SetActive(false);
        _btn_Restart.SetActive(false);
	}
	
	// Update is called once per frame
	void Update () 
    {
        //每10個會加快轉動
        _lv = _score / 10;
        //旋轉小圈
        if (!_isGameOver)
            _circle.Rotate(new Vector3(0, 0, _speed * Time.deltaTime - _lv * 0.2f));

        ShootNeedle();
	}

    /// <summary>
    /// 建立一個針在底部
    /// </summary>
    /// <returns></returns>
    Transform CreateNeedle()
    {
        GameObject go = GameObject.Instantiate(_needle_Prefab.gameObject) as GameObject;
        go.SetActive(true);
        go.transform.SetParent(_root);
        go.transform.localPosition = new Vector3(0, -340, 0);
        go.transform.localScale = Vector3.one;
        
        return go.transform;
    }


    /// <summary>
    /// 發射
    /// </summary>
    void ShootNeedle()
    {
        if (!_isShoot && Input.GetMouseButtonDown(0))
        {
            _needle_Start = CreateNeedle();
            _isShoot = true;
            _isFly = true;
            
        }

        if (_isFly)
        {
            //移動
            _needle_Fly = _needle_Start;
            _needle_Fly.position = Vector3.MoveTowards(_needle_Fly.position, _end, 5.0f * Time.deltaTime);
            //到達終點
            if (Vector3.Distance(_needle_Fly.position, _end) < 0.05f)
            {
                _needle_Fly.SetParent(_circle);//更新父節點,讓針一起旋轉
                
                if (_isGameOver)
                {
                    StartCoroutine(GameOver());//防止有時卡頓
                    Debug.Log("呼叫GameOver");
                }
                else
                {
                    _score++;//加分
                    _txtScore.text = _score.ToString();
                    _isShoot = false;
                    _isFly = false;
                }
                
            }
        }
    }

    IEnumerator GameOver()
    {
        _text_GG.SetActive(true);
        _btn_Restart.SetActive(true);
        _isShoot = true;
        _isFly = false;
        yield return 0;
    }

    //按鈕事件
    public void BtnEvent_Restart()
    {
        _text_GG.SetActive(false);
        _btn_Restart.SetActive(false);
        _score = 0;
        _txtScore.text = "0";
        _isGameOver = false;
        _isShoot = false;

        for (int i = 0; i < _circle.childCount; ++i)
        {
            Destroy(_circle.GetChild(i).gameObject);
        }
    }


}