zoukankan      html  css  js  c++  java
  • c# 贪吃蛇源码

    using UnityEngine;

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


    public class SnakeMove : MonoBehaviour {
        List<Transform> Body = new List<Transform> ();//存放Transform数据类型的数组Body
        public GameObject BodyObject;/    /定义一个游戏物体  蛇
        public GameObject sFood;////  定义一个游戏物体 食物


        //updown Y轴 left down是x轴  forward back是z 轴
        Vector3 postion = Vector3.up;       //Vector3.up的简称  Vertor3(0,1,0)
        private bool s = false;
        // Use this for initialization
        public float speed=0.1f;
        public float time = 0;
        //public float time0 =1;
    //    public float xlimit = 25.5f;
    //    public float ylimit = 25.5f;
        public int xlimit = 25;
        public int ylimit = 25;


        //伤害数值
        public int Value;
        //目标位置
        private Vector3 mTarget;
        //屏幕坐标
        private Vector3 mScreen;
        //文本宽度
        public float ContentWidth = 100;
        //文本高度
        public float ContentHeight = 50;
        //GUI坐标
        private Vector2 mPoint;
        //炫酷的字体
        GUIStyle frontStyle = new GUIStyle();

        public Text text;
        Vector3 VPostion;

        public GameObject body1;
        public ArrayList list = new ArrayList();
        private bool isEated = false;
        void Start () {
            
            //Time.timeScale=1;
            //time = Time.time + time;
            //InvokeRepeating 从第一秒开始,每隔四秒调用一次
            InvokeRepeating ("Food", 1, 4);1秒后 调用Food  之后每4秒调用一次
            InvokeRepeating ("Move", speed, speed);

            //获取目标位置
            mTarget =transform.position;
            //获取屏幕坐标
            mScreen =Camera.main.WorldToScreenPoint(mTarget);
            //将屏幕坐标转化为GUI坐标
            mPoint = new Vector2(mScreen.x,Screen.height-mScreen.y);
            //
            Value =0;
        }
        
        // Update is called once per frame
        void Update () {

            if(Input.GetMouseButtonDown(0))
                Time.timeScale=1;
            if (Input.GetKeyDown//(获取键按下) (KeyCode.D)&&postion!=Vector3.left) 

            {
                postion = Vector3.right;
            }
            if (Input.GetKeyDown (KeyCode.A)&&postion!=Vector3.right) 
            {
                postion = Vector3.left;
            }
            if (Input.GetKeyDown (KeyCode.S)&&postion!=Vector3.up) 
            {
                postion = Vector3.down;
            }
            if (Input.GetKeyDown (KeyCode.W)&&postion!=Vector3.down) 
            {
                postion = Vector3.up;
            }
            //Time.tiem 系统时间
    //        if (time<=Time.time) 
    //        {
    //            transform.Translate (postion);
    //            time += 1;
    //            //time 越小 速度越快
    //        }

            //Random r = new Random ();
            //OnTriggerEnter();
        
        }
        void Move()
        {
            //transform.Translate (postion);

            VPostion = transform.position;
            transform.Translate (postion); //Transform.Translate平移 向某方向移动物体多少距离
            if (isEated) 
            {
                GameObject g = (GameObject)Instantiate(body1,VPostion,Quaternion.identity);
                g.GetComponent<MeshRenderer> ().material.color = new Color (Random.Range (0, 1.0f), Random.Range (0, 1.0f), Random.Range (0, 1.0f));
                list.Insert (0, g);//将一个项插入指定索引处的 IList<(Of <(T>)>)。 
                                             //将元素插入 ArrayList 的指定索引处。 可在任意位置插入。
                isEated = false;
            }
            else if (list.Count>0)
            {
                //            //最后一个元素的位置赋值给新的位置
                //            //最后一个元素插入在第一个元素地址
                //            //删除最后一个元素
                ((GameObject)list[list.Count-1]).transform.position = VPostion;
                list.Insert (0, list [list.Count - 1]);//在0的位置插入一个
                list.RemoveAt (list.Count-1);//移除 ArrayList 的指定索引处的元素。
            }
        }
        void Food()
        {
            System.Random r = new System.Random ();
            float x = r.Next (-xlimit,xlimit);
            float y = r.Next (-ylimit,ylimit);

    //        float x = Random.Range(-xlimit,xlimit);
    //        float y = Random.Range (-ylimit, ylimit);
            Instantiate (sFood, new Vector2 (x, y), Quaternion.identity);
        }
        void OnTriggerEnter(Collider other)
        {
       
            if (other.gameObject.CompareTag ("Food")) 
            {
                if(!isEated)
                    Value++;
                isEated = true;
                Destroy (other.gameObject);
            }
            else 
            {
                

                Time.timeScale=0;

                SceneManager.LoadScene (0);
            }
            text.text = "Score :" + Value;
        }
        void OnGUI()
        {
            //保证目标在摄像机前方
            if (mScreen.z > 0) 
            {
                //GUI.color = Color.blue;
                //内部使用GUI坐标进行绘制
                frontStyle.fontSize=40;
                frontStyle.normal.background = null;//设置背景填充
                frontStyle.normal.textColor = new Color (100, 0, 128);//设置字体颜色
                GUI.Label(new Rect(30,0,ContentWidth,ContentHeight),"分数为"+Value.ToString(),frontStyle);
                //                  mPoint.x,mPoint.y
            }
        }
    }

  • 相关阅读:
    肩部肌肉劳损zt
    大屏幕手机上网页字体显示很小的问题
    SWT的Display
    The connection to adb is down, and a severe error has occured.
    [ZT]使用tmpfs缓存文件提高性能
    Mutex
    javascript阻塞加载问题【转】
    IE参考
    2台电脑网线对接注意的事项
    重建索引
  • 原文地址:https://www.cnblogs.com/wshyj/p/6094635.html
Copyright © 2011-2022 走看看