忙碌的一周又过去了,代码谁都会写,重要的是思维,同一个功能每个人都有每个人的实现方法,所以要先去想自己怎么去实现这个功能,在摸不到头绪的时候去参考别人的代码,然后回来再想自己的代码怎么实现。
游戏开发的重点就是把一个游戏拆开,怎么去实现它。
贪吃蛇的玩法:按键控制它得移动,吃到食物之后,食物消失,它会在身体的末端增加一个身体,当撞到墙或者自己身体的时候,游戏结束
相对,要实现的功能也就显而易见了
先分析游戏里用到的对象
1.蛇头
2.蛇身子
3.食物
4.地面
5.墙
再分析游戏的功能怎么实现
1键盘控制它得移动--------可以使用InPut.GetKey来实现它的移动
2.吃到食物后,食物消失,也就是食物跟蛇头相接触后,食物消失---------可以采用碰撞检测或触发检测来实现,很明显,我们不需要食物跟蛇头产生“碰撞”效果,所以采用触发检测来实现
3.蛇在吃到食物之后,它得身体会增长1-------可以把身体做成预设体,在蛇头跟食物相接触的时候,实例化一个身子
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">4.</span><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">当撞到墙或者自己身体的时候,游戏结束----同样可以采用触发检测的方法来实现</span>
游戏的实现方法大体都分析明白了,但是有一个问题,蛇头移动,身体跟着它动,这要怎么实现,也就是一个跟着一个移动的实现方法
这也算是游戏的核心算法了吧,跟数据结构里的链表是不是很像,头把它的位置传给第一个身子,第一个身子把它得位置传给第二个身子........
源码:
SnackHead 身上的HeadMove脚本
using UnityEngine;
using System.Collections;
public class HeadMove : MonoBehaviour {
public GameObject snack_Body;
private GameObject firstBody;
private GameObject lastBody;
private GameObject CreatedBody;
//随机生成食物球
private float cube_foodMax_X =12F;
private float cube_foodMin_X = -12.8f;
private float cube_foodMax_Z =14f;
private float cube_foodMin_Z = -13.8f;
public GameObject cube_food;
private float timer = 0f;
private float time = 0.3f;
private GameObject cc;
void RandomFood(){
//随机在哪个位置上生成球
//求它能出现的最大范围
//x=-12.8 ,12 z = -13.8, 14
float cube_food_PositionX = Random.Range (cube_foodMin_X,cube_foodMax_X);
float cube_food_PositionZ = Random.Range (cube_foodMin_Z,cube_foodMax_Z);
cc = Instantiate (cube_food, new Vector3 (cube_food_PositionX,0,cube_food_PositionZ), Quaternion.identity)as GameObject;
}
void Start () {
RandomFood ();
}
/// <summary>
/// 按键移动
/// W:forward
/// S:back
/// A:left
/// D:right
/// </summary>
void Update () {
if (timer > time) {
Vector3 old = this.transform.position;
this.transform.position += transform.forward;
if (firstBody != null) {
// firstBody.transform.position = old;
firstBody.GetComponent<CreateBody> ().moveTo (old);
}
timer = 0;
} else {
timer+=Time.deltaTime;
}
//当它得尾巴在前,头在后时,此时,它就不能往前走了(尾巴不能拖着头走)
if(Input.GetKeyDown(KeyCode.W)&&Vector3.Angle(Vector3.forward,this.transform.forward)<160f){
//此时可以移动了
//类似于单链表,头移动,头的坐标传给第一个身体,第一个身体传给第二个身体......
//写一个移动的方法
this.transform.forward = Vector3.forward*1;
}
if (Input.GetKeyDown(KeyCode.S) && Vector3.Angle(Vector3.back,this.transform.forward) < 160f)
{
this.transform.forward = Vector3.back*1;
}
if (Input.GetKeyDown(KeyCode.A) && Vector3.Angle(Vector3.left,this.transform.forward) < 160f)
{
this.transform.forward = Vector3.left*1;
}
if (Input.GetKeyDown(KeyCode.D) && Vector3.Angle(Vector3.right,this.transform.forward) < 160f)
{
this.transform.forward = Vector3.right*1;
}
}
void Move(){
GameObject CreatedBodys = Instantiate(snack_Body, new Vector3(100f, 100f, 100f), Quaternion.identity) as GameObject;
if(lastBody!=null){
lastBody.GetComponent<CreateBody>().next = CreatedBodys;
}
if (firstBody== null) {
firstBody = CreatedBodys;
print("firstBody==null");
}
lastBody = CreatedBodys;
}
void OnTriggerEnter(Collider other){
if(other.gameObject.tag.CompareTo("food")==0){
//创建一个身体
Destroy(cc);
RandomFood();
Move();
}
}
}
Body身上的CreateBody脚本
using UnityEngine;
using System.Collections;
public class CreateBody : MonoBehaviour {
public GameObject next;
void Start () {
}
void Update () {
}
public void moveTo(Vector3 pos){
Vector3 old = this.transform.position;
this.transform.position = pos;//当前坐标变为pos.
if(next!=null){
next.GetComponent<CreateBody>().moveTo(old);
}
}
}