U3D贪吃蛇心得以及个人遇到的问题警戒以及勉励今后的自己

首先附上头部代码:

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


public class trans : MonoBehaviour {
    //
    
    public GameObject foodPrefab;
    public GameObject bodyPrefab;
    public float speed=2;
    private bool _isOver=false;

                              //计时器
    private float _Time = 0f;

                            //链表头部第一个身体部分
    private body FirstBody;

                           //链表尾部最后一个身体部分
    private body LastBody;
    private Vector3 now_pos;
    private enum direction {
        forward,
        back,
        left,
        right,
    };
    private direction _now = direction.forward;
    private direction _turn=direction.forward;
    // Use this for initialization
    void Start() {
        createFood();

    }
    void createFood() {
        float x = Random.Range(-9.5f, 9.5f);
        float z = Random.Range(-9.5f, 9.5f);                    //旋转自身的角度(不旋转)
        GameObject food = Instantiate(foodPrefab,new Vector3(x,0f,z),Quaternion.identity) as GameObject;
    }
     
    void OnTriggerEnter(Collider other) {   //这个other就是被碰撞体
        
        if (other.tag.Equals("bound")|| other.tag.Equals("body")) {
            
            _isOver = true;
        }
        else if (other.tag.Equals("food")) {
           Destroy(other.gameObject);
            createBody();
            createFood();
            }
    }

    // Update is called once per frame
    void Update() {
        if (!_isOver){
            check_key_toMove(speed);
        }
      
    }
    void createBody(){
       GameObject _body = Instantiate(bodyPrefab,new Vector3(9999f,9999f,9999f),Quaternion.identity) as GameObject;
        if (_body == null) {
        
        }
        //obj与body类绑定获取方法(刚开始是猜测)
        //我在疑惑既然是绑定也就是说脚本body和GameObject类型的_body绑定了,却显示空指针异常
        //为何还要把body脚本绑定在身子GameObject的模型上成为预制体才能正常,这不是多此一举吗
        //事后我又把body脚本绑定在head的(GameObject)模型上,依然显示空指针异常
        //根据事实结果我的猜测是错误的,根据字义理解是获取成分
        //我再次推测GetComponent并没有绑定,只是单纯的获取bodyprefab的成分
        //于是为了验证,我去查了官方文档
        //Returns the component of Type type if the game object has one attached, null if it doesn't.
        //果不其然获取依附在GameObject(bodyPrefab)的成分,如果没有就返回空。果然空指针就是这么引起的
        //由此看来后来的假设是正确的,其实现在想想以前的想法确实挺傻,给body脚本一个模型成预制体->创建Gameobject->选取Gameobject的成份就是为了调用脚本,就行了
        body obj =_body.GetComponent<body>();
        
        if (FirstBody == null) {
            //C#这里是吧obj的地址赋予给了FirstBody,而不是克隆
      
            FirstBody = obj;
        }
        if (LastBody != null) {
           
            LastBody.next = obj;
        }
        LastBody = obj;


    }
    
    void check_key_toMove(float Speed){
        if (Input.GetKey(KeyCode.W)) {
            //如果当前方向为下根据游戏规则他是不能按上的,否则响应按键
            if (_now != direction.back)
            {
                _turn = direction.forward;
            }
            else {
                //如果当前方向为为下,他会忽视按键,继续当前方向运行。
                _turn = _now;
            }


        }
        else if (Input.GetKey(KeyCode.S)){
            
            if (_now != direction.forward)
            {
                _turn = direction.back;
            }
            else
            {
                _turn = _now;
            }

        }
        else if (Input.GetKey(KeyCode.A))
        {
          
            if (_now != direction.right)
            {
                _turn = direction.left;
            }
            else
            {
                _turn = _now;
            }

        }

        else if (Input.GetKey(KeyCode.D))
        {     //相当于自身的蓝线(Z轴)改变了正方向,他是旋转性的改变。
            
            if (_now != direction.left)
            {
                _turn = direction.right;
            }
            else
            {
                _turn = _now;
            }
        }
        _Time += Time.deltaTime;
        if (_Time >= 1f / Speed) {
            switch (_turn) {
                case direction.left: transform.forward = Vector3.left; break;
                case direction.right: transform.forward = Vector3.right; break;
                case direction.forward: transform.forward = Vector3.forward; break;
                case direction.back: transform.forward = Vector3.back; break;
            }
            move();
            _Time = 0f;
        }
    }
    void move(){


        now_pos = transform.position;
        //这里沿着自身蓝线(Z轴)前进,但是世界坐标并没有改变
        //Vector3.forward虽说是(0,0,1)但是这里是相对于自身蓝线进行的(0,0,1),见Translate的解释
        //如果相对于世界的话需要引入第二个参数Space.World,见transform.forward与Vector.forward的区别
        transform.Translate(Vector3.forward);
        _now = _turn;
        if (FirstBody != null) {
            FirstBody._move(now_pos);
            }
        }
}

再次附上body代码

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

public class body : MonoBehaviour {
    //下一个body的引用,其实就是链表
    public body next;
    public int say() {
        return 5;
    }
    public void _move(Vector3 pos) {
        Vector3 nextPos = transform.position;
        transform.position = pos;
        if (next != null)
        {
            next._move(nextPos);
        }
    }
}

Time.deltaTime代表的是一秒为单位每帧消耗的时间, 在Update中 就是按帧来运行的 一秒运行60次60帧。 当一个数据乘以Time.deltaTime时代表一秒钟走该数据,而不是每帧走该数据。在update中Time.deltaTime 大约平均在在1/60=0.016秒。


总结:自己突发想法写一个游戏,来验收自己最近学习的成果,因为初学,于是选择了较为简单的贪吃蛇,代码根据思路,贪吃蛇就是单纯的用到了链表,总其步骤有5步:第一步创建蛇头让蛇头自由运动加入刚体创建碰撞触发器(墙的边缘),第二部创建食物(完善碰撞触发器),第三部创建身体(完善脚本,算法用到的就是链表,进行构造链表方法,完成body预制体) 第四部蛇头引入body预制体完成链表并开始移动  第五步测试最终BUG,除了第二步第三步  第一步第四部第五步都遇到到了难点疑点,在注释中大多都标注了,我相信天道酬勤,写此文章,警示以及勉励今后我unity3D的学习。



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值