2018年5月9日 星期三

Week11_是沫不是沬

🌟期末專案_嗨!地鼠

🌠遊戲程式碼撰寫

控制每一Round最多產生的地鼠數量,產生地鼠的預置物且不重複產生
程式碼如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EasyControl : MonoBehaviour {
    public static float gametimer;
    float roundTimer;
    int max;
    bool gameOver = false, round = false;
    public GameObject s1MouseOne, s1MouseTwo;
    public List<ControlMouse> mouse = new List<ControlMouse>();
    public List<Transform> hole = new List<Transform>();
   
    void Start()
    {
        gametimer = 60f;
        roundTimer = 0f;
        gameOver = false;
    }
    void Update()
    {
        if (!gameOver)
        {
            if (gametimer <= 0) gameOver = true;
            gametimer -= Time.deltaTime;
            roundTimer -= Time.deltaTime;
            //Debug.Log(gametimer);
           
            if (!gameOver && roundTimer <= 0.2f)
            {
                round = true;
                roundTimer = 4f;
            }
            if (round)
            {
                max = Random.Range(1, 4);  //隨機產生最大值,其值1≤max<4
               
//Debug.Log(max);
                while (max > 0)
                {
                    max -= 1;
                    spawnS1Mouse();
                }

                round = false;
            }

        }
    }

    void spawnS1Mouse()  //產生第一關的地鼠
    {
        int index = Random.Range(0, 8);
        while (mouse[index] != null) index = Random.Range(0, 8);  //判斷該洞口是否已經有地鼠
        if (index % 2 == 0)
        {
            GameObject s1One = Instantiate(s1MouseOne, new Vector3(hole[index].position.x,
                     hole[index].position.y - 15f, hole[index].position.z), hole[index].transform.rotation);
            mouse[index] = s1One.GetComponent<ControlMouse>();
            Destroy(s1One, 3f);
        }
        else
        {
            GameObject s1Two = Instantiate(s1MouseTwo, new Vector3(hole[index].position.x,
                     hole[index].position.y - 15f, hole[index].position.z), hole[index].transform.rotation);
            mouse[index] = s1Two.GetComponent<ControlMouse>();
            Destroy(s1Two, 3f);
        }
    }
}

結果如下圖所示


沒有留言:

張貼留言