您当前的位置: 首页 >  游戏

苍狼王unity学院

暂无认证

  • 0浏览

    0关注

    305博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity和C#-游戏开发-贪吃蛇+源代码工程

苍狼王unity学院 发布时间:2021-08-01 09:22:09 ,浏览量:0

 

 

 

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

public class Snake : MonoBehaviour {     // Current Movement Direction     // (by default it moves to the right)     Vector2 dir = Vector2.right;

    List tail = new List();

    // Did the snake eat something?     bool ate = false;

    // Tail Prefab     public GameObject tailPrefab;

    public Action OnLose;

    // Use this for initialization     void Start()     {

        // Move the Snake every 300ms         InvokeRepeating("Move", 0.3f, 0.3f);     }

    void OnTriggerEnter2D(Collider2D coll)     {         // Food?         if (coll.name.StartsWith("food"))         {             // Get longer in next Move call             ate = true;

            // Remove the Food             Destroy(coll.gameObject);         }         // Collided with Tail or Border         else         {             OnLose();         }     }

    void Update()     {         // Move in a new Direction?         if (Input.GetKey(KeyCode.RightArrow))             dir = Vector2.right;         else if (Input.GetKey(KeyCode.DownArrow))             dir = -Vector2.up;    // '-up' means 'down'         else if (Input.GetKey(KeyCode.LeftArrow))             dir = -Vector2.right; // '-right' means 'left'         else if (Input.GetKey(KeyCode.UpArrow))             dir = Vector2.up;     }

    void Move()     {         // Move head into new direction         Vector2 v = transform.position;

        transform.Translate(dir);

        if (ate)         {             // Load Prefab into the world             GameObject g = (GameObject)Instantiate(tailPrefab,                                                   v,                                                   Quaternion.identity);

            // Keep track of it in our tail list             tail.Insert(0, g.transform);

            // Reset the flag             ate = false;         }         // Do we have a Tail?         else if (tail.Count > 0)         {             // Move last Tail Element to where the Head was             tail.Last().position = v;

            // Add to front of list, remove from the back             tail.Insert(0, tail.Last());             tail.RemoveAt(tail.Count - 1);         }     } }

https://item.taobao.com/item.htm?ft=t&id=652525567349

关注
打赏
1665389160
查看更多评论
立即登录/注册

微信扫码登录

0.0401s