Unity貪吃蛇

Loading界面腳本dom

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

public class SnakeUI : MonoBehaviour {

// Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {
        if(Input.GetMouseButtonDown(0))
        {
            SceneManager.LoadScene(1);
        }
    }
}

Snake移動腳本ide

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using UnityEngine.SceneManagement;

public class SnakeMove : MonoBehaviour {
    List<Transform> body = new List<Transform>();
    Vector2 direction = Vector2.up;
    public GameObject SnakeBody;
    private bool flag;
    float speed = 0.3f;
    void Move() {
        Vector2 position = transform.position;
        this.transform.Translate(direction);
        if (flag)
        {
            GameObject bodypfd = (GameObject)Instantiate(SnakeBody, position, Quaternion.identity);
            body.Insert(0, bodypfd.transform);
            flag = false;
        }
        else if (body.Count > 0)
        {
            body.Last().position = position;
            body.Insert(0, body.Last().transform);
            body.RemoveAt(body.Count - 1);
        }
    }
    // Use this for initialization
    void Start () {
        InvokeRepeating("Move", speed, speed);
    }
    
    // Update is called once per frame
    void Update () {
        if (Input.GetKeyDown(KeyCode.W) || (Input.GetKeyDown(KeyCode.UpArrow)))
        {
            direction = Vector2.up;
        }
        else if (Input.GetKeyDown(KeyCode.S) || (Input.GetKeyDown(KeyCode.DownArrow)))
        {
            direction = Vector2.down;
        }
        else if (Input.GetKeyDown(KeyCode.A) || (Input.GetKeyDown(KeyCode.LeftArrow)))
        {
            direction = Vector2.left;
        }
        else if (Input.GetKeyDown(KeyCode.D) || (Input.GetKeyDown(KeyCode.RightArrow)))
        {
            direction = Vector2.right;
        }
    }

void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Food"))
        {
            Destroy(other.gameObject);
            flag = true;
        }
        else
        {
            SceneManager.LoadScene(0);
        }
    }
}

食物生成腳本this

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

public class FoodCreat : MonoBehaviour {

public GameObject S_food;
    public int x_limit = 30;
    public int y_limit = 16;

void Food()
    {
        int x = Random.Range(-x_limit, x_limit);
        int y = Random.Range(-y_limit, y_limit);
        Instantiate(S_food, new Vector2(x, y), Quaternion.identity);
    }
    // Use this for initialization
    void Start () {
        InvokeRepeating("Food", 2, 3);
    }
    
    // Update is called once per frame
    void Update () {
        
    }
}

 

錄屏文件分享:code

連接:https://pan.baidu.com/s/1kmovtK9Dd_uJiHuG4YP_CA 
提取碼:98x8 orm

相關文章
相關標籤/搜索