C# unity3d 贪吃蛇 游戏 源码 及其感想

这个游戏的设计过程是这样的:

1,创建 的身体,在 u3d里我只是用一个小方块表示 设计好蛇头后就添加meterial 这样蛇的基本元素都有了

2,创建地图,在这个项目里,我是使用一个 3d projiect 叫做 Quad的对象。然后要将地图大小改变为50,50.就是将该对象的scale改为50,50,1

3,创建食物,也是新建一个小方块,在tag那里添加新标签,填为 Food

4,创建body,还是一个小方块, 在tag那里添加新标签,填为 Body

方法上难点

1.触发器的使用,记住 OnTriggerEnter的使用,前提是两个碰撞的对象,必须其一个有rigidbody刚体组件,然后其中一个(不是另外一个 是两个中的一个)必须要勾上Is Trigger,两个不冲突,就可以了。设计碰撞器的大小,也是个细活。

在这里我只是使用了 Box Collider的组件,在完成路上曾经将的Is Trigger打开,所以跟地图Quad冲突了,所以导致不断刷新这个场景。

2.一个方法  叫做other.gameObject.CompareTag ("Food"),我在OnTriggerEnter里的主要部分。

检测是否碰到一个Tag是否为Food的对象,然后检测到就将其删除,并延伸自己的身体。在后面后几次开发中都使用到,是个很重要的方法。

具体使用方法为

C#⇒ bool CompareTag(string tag);

Is this game object tagged with /tag/?

3.蛇的身体的移动,见到概括 就是当蛇 吃到食物后,在下一帧中延长了身体.蛇的身体是保存在一个集合中去的,还有最重要的一点!就是

用一个Vector3 VPosition记录蛇在移动前的位置,然后移动蛇,在原地生成一个body,位置为VPosition,然后lis.Insert(0,g)//g为body那个对象

((GameObject)list[list.Count-1]).transform.position = VPostion;//这句则是将当前集合最后一个元素赋值为VPosition

list.Insert (0, list [list.Count - 1]);//然后在集合的0位置插入最后一个元素

这样的话,g就排到了集合的第二个,而最后一个元素排到了第一个,

list.RemoveAt (list.Count-1);

然后删除集中中最后的元素,整个蛇移动的过程 和吃食物生成蛇的过程就清晰了。

代码最后 还有两段小UI,就不放上来了。

第一次记录自己的u3d开发过程,以后有经验后,应该不会这样全部详细保留下来了,主要供自己学习使用,代码未优化,只是达到实现效果而已。

==================================究极无敌华丽分界线==================================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 Body = new List ();//Transform的表

public GameObject BodyObject;

public GameObject sFood;

//updown Y轴 left down是x轴  forward back是z 轴

Vector3 postion = Vector3.up;

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);

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 ().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

}

}

}

你可能感兴趣的:(C# unity3d 贪吃蛇 游戏 源码 及其感想)