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
        }
    }
}

转载于:https://www.cnblogs.com/wshyj/p/6094635.html

游戏名称:贪吃蛇 游戏级别:10级 注意:因为文件太大,无法上传,所以将音乐和图片文件另外发布,请下载(免费),下载完解压缩后,将音乐和图片放在Debug根目录下即可。 游戏说明: 关于控制蛇运动方向: 用键盘上的↑、↓、←、→控制蛇的运动方向。 当蛇向一个方向运动时,它的反向键被锁定。 不能通过连续摁某个方向键而加快蛇的运动。 若在游戏中,蛇头碰了墙或是自己的身体,则游戏结束。 关于蛇吃食物: 蛇每吃一个食物增长一节,并且得分加10分,总共为100节。 蛇每增长10节游戏上升一个级别。 关于游戏结束: 如果在游戏过程中,蛇头碰墙或是碰到了自己的身体,则游戏结束。 如果在游戏过程中,用户选择退出游戏,则会提醒用户游戏正在进行中,是否要退出。 选择退出,则游戏结束。 选择取消,则游戏继续。 如果玩家成功过关,则游戏结束。 在上述任何一种情况下,系统都会询问用户:是否保存游戏成绩? 选择确定,如果姓名为空,则默认以“匿名玩家”记录。 选择取消,则不记录。 关于蛇运动的速度: 玩家可以通过:选项->速度 打开窗口。 系统默认的方式是变速游戏。 玩家可以自行选择变速游戏或是均速游戏。 变速游戏:速度分1-10个级别。 并且根据关卡的高低决定速度的快慢。 均速游戏:用户可以自行选择所要速度的快慢。 1表示最慢,10表示最快。 关于音乐的播放: 当打开游戏界面时,音乐会自动播放,默认的音乐是:Remeber。 若玩家要修改或关闭音乐,可以通过:选项->音乐 或是 F5快捷键 打开窗口。 若玩家不想播放音乐,请点击关闭。 若玩家要播放选中的音乐,请点击确定。 关于界面的选择: 玩家可以根据爱好选择想要的图片,总共有8张背景图片供选择。 玩家选择图片后,可以在对话框的右边预览到图片。 点击确定则显示选中的图片,点击取消则显示原先图片。 当游戏开始后,此功能键不能用。 关于查看记录: 玩家可以通过:关于->查看记录 或 F6快捷键 打开窗口。 窗口中显示了以往玩家的前三名成绩。 关于游戏说明: 玩家可以通过:关于->游戏说明 或 F7快捷键 打开窗口。 窗口中显示了本游戏的说明。 关于游戏的快捷键: 开始:F2 暂停:F3 退出:F4 音乐:F5 查看记录:F6 游戏说明:F7
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值