20242人加入学习
(121人评价)
Unity初级案例 - 贪吃蛇(Unity2017.2.0)

制作于2017.12.16

价格 免费

初级案例。

Toggle 

UI 

空格加速。

暂停。

分数换f

[展开全文]

Outline组件,描边;Shadow组件,阴影。

[展开全文]

//引用library

using System.Collections;

 

//调用方法

 StartCoroutine(gameOver(1.5f));

 

//写携程方法 IEnumerator

 IEnumerator gameOver(float t)
 {
        yield return new WaitForSeconds(t);
                                    UnityEngine.SceneManagement.SceneManager.LoadScene(1);
}

 

 

PlayerPrefs.SetInt("lastl" ,)

[展开全文]

using UnityEngine;

public class snakehesd : MonoBehaviour

{
    public float velocity=0.35f;
    public int step;
    private int x;
    private int y;
    private Vector3 HeadPos;

    void Start()
    {
        InvokeRepeating("Move", 0, velocity);
        x=0;y=step;
    }
    void Update()
    {

if(Input.GetKeyDown(KeyCode.Space))

{

CancelInvoke();

InvokeRepeating("Move",0,velocity-0.2f);

}

if(Input.GetKeyUp(KeyCode.Space))

{

CancelInvoke();

InvokeRepeating("Move",0,velocity);

}

        if (Input.GetKey(KeyCode.W)&&y!=-step)
        {

 gameObject.transform.localRotation=Quaternion.Euler(0,0,0);
            x=0;y=step;
        }
        if (Input.GetKey(KeyCode.S)&&y!=step)
        {

 gameObject.transform.localRotation=Quaternion.Euler(0,0,180);
            x=0;y=-step;
        }
        if (Input.GetKey(KeyCode.A)&&x!=step)
        {

 gameObject.transform.localRotation=Quaternion.Euler(0,0,90);
            x=-step;y=0;
        }
        if (Input.GetKey(KeyCode.D)&&x!=-step)
        {

 gameObject.transform.localPlocalRotationosition=Quaternion.Euler(0,0,-90);
            x=step;y=0;
        }
    }
    void Move()
    {
        HeadPos=gameObject.transform.localPosition;
        gameObject.transform.localPosition=new Vector3(HeadPos.x+x,HeadPos.y+y,HeadPos.z);
    }
}

[展开全文]

贪吃蛇移动

方法一:只移动蛇头,蛇尾移到原来蛇头位置后

1.代码:移动函数下

//使用需要引入Linq

if(bodyList.Count>0)

{

bodyList.Last().localPosition = headPos;

bodyList.Insert(0,bodyList.Last());

bodyList.Remove(bodyList.count-1);

}

 

2.将Grow()放入碰撞函数

 

3.在监视面板为蛇身图片赋值

 

4.出现问题,由于实例化后的蛇身会出现在中心区域,然后通过计算在到它该有的位置,所以需要实例化的蛇身在相机外

GameObject body = Instantiate(bodyPrefab,new Vector3(2000,2000,0),Quaterntion.identity);

 

[展开全文]

1.制作 蛇身 预制体,设置图片,改变大小,添加碰撞器,设置碰撞器,改名“SnakeBody”

 

2.使用一个集合 存放 蛇身位置

引入命名空间 using System.Collections.Generic;

定义 蛇身位置 集合

public List<ReckTransform> bodyList = new List<Transform>();

定义 蛇身预制体

public GameObject bodyPrefab;

定义 两种蛇身 图片

public Sprite[] bodySprite = new Sprite[2];

 

定义 画板 Canvas,以便作为父物体

private Transform canvas

并初始化canvas

void start()

{canvas = GameObject.Find(”Canvas“) .transform}

 

3.生长蛇身 函数

void Grow()

{

//图片索引选择

int index = (bodyList.count%2==0)?0:1;

//实例化蛇身

GameObject body = Instantiate(bodyPrefab);

//设置图片

body.GetComponent<Image>.sprite = bodySprites[index]

//设置父物体

body.transform.SetParent(canvas,false);

//将身体放入蛇身的数组

bodyList.Add(body.transform);

 

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

在 与 食物碰撞处写生成蛇身的代码

[展开全文]

1.为蛇头,食物添加碰撞器

2.碰撞器需要比蛇头本体,食物本体要小一圈,否则擦过时会发生碰撞

3.蛇头IsTrigger勾上,碰撞器变成触碰器,碰撞器与碰撞器间不会有撞击效果

 食物不勾

 

4.蛇头添加刚体,不启用重力(重力比例=0)

 

5.为food添加tag:

监视面板-tag-添加tag-加号-“food”

返回监视面板-tag-标签设为food

 

6添加吃到食物的代码

1)蛇头脚本中添加函数

    private void OnTriggerEnter2D(Collider2D collison)
    {
        Destroy (collison.gameObject);
        FoodMaker.Instance.FoodMake ();

    }

 

 

 

2)在食物脚本中创建一个单例模式使得另外一个脚本可以调用该脚本内的函数

    private static FoodMaker _Instance;
    public static FoodMaker Instance
    {
        get{return _Instance; }
    }

    void Awake()
    {
        _Instance = this;
    }

将MakeFood函数公开

 

 

 

 

 

 

 

 

 

 

 

 

 

 

[展开全文]

 

 

 

 

1.食物注意:生成的位置需要让蛇能够吃到,所以以蛇头的长宽做网格,生成在有效的网格内

 食物需要生成在 下-6*30,上6*30,左7*30,右11*30

 

 

2.制作食物的prefab,Image,设置合适大小

 

3.创建归类用的空物体,FoodHolder

注意:在UI中,如果创建一个归类用的空物体,那么最好将大小锚点设置到父物体四个角上

 

3.添加脚本FoodMaker

//上下左右食物生成坐标的限制

public int xRightLimit = 11;

public int yLimit = 6;

public int xLeftLimit = 7;

public GameObject food;

//图片数组,Sprite用来存储图片

public Sprite[] foodSprites;

 

private Transform foodHolder;

 

void Start()

{

  foodHolder = GameObject.Find("FoodHolder").transform;

MakeFood();

}

 

//生成条件:开始生成一个,被吃掉生成一个

void MakeFood()

{

    int index = Random.Range(0,foodSprites.Length);  

 

 

//将食物实例化出来(面板拖出来) 

    GameObject food = Instantiate(foodPrefab);

 

//

food.GetComponent<Image>().sprite = foodSprites[Index];

 

 

//食物归类到父物体,且不调整food的transform

   food.transform.setParent(foodHolder,false)

 

 

//随机位置

int x=Ramdom.Range(-xLeftLimit,xRightLimit );

int y = Ramdom.Range(-yLimit,yLimit);

food.transform.localPosition = new Vector(x*30,y*30,0);

 

 

}

 

4.创建一个空物体,专门用来挂脚本,然后将FoodPrefab赋值,foodSprites全选图片拖住赋值

 

 

 

 

 

 

 

 

 

 

[展开全文]

1.完善蛇头方向朝向代码实现:

  gameObject.transform.localRotation = Quternion.Euler(0,0,90);

//保存Rotation的数据结构是一个四元数,所以想赋值需要把三维向量转换为四元数,用Quternion.Euler即可

//改变z轴,z轴朝内,故逆时针转

 

2.逆转头问题:

每次转头判断是否是逆转头

代码:if (Input.GetKey (KeyCode.W) && y != -Step) 

 

3.蛇头的加速

按下时加速:

抬起时回复:

        if (Input.GetKeyDown (KeyCode.Space))
        {
            CancelInvoke ();

//取消
            InvokeRepeating ("Move", 0, velocity-0.2f);
        }

        if (Input.GetKeyUp (KeyCode.Space))
        {
            CancelInvoke ();
            InvokeRepeating ("Move", 0, velocity);
        }

[展开全文]

1.这是一个纯UGUI游戏,所以蛇头会用UGUI来做

 

2.创建一个Image作为蛇头,做成Prefab

 

3.创建一个脚本SnackHead

注意:脚本名如果写错,删掉重建,否则既要改脚本名,又要改脚本内类名

 

4.删掉所有函数

  代码如下


  public float velocity;
  public int step;//蛇一格走多少
  private int x,y;//蛇头下一次的移动向量
  private Vector3 headPos;//蛇头的位置

  void Move{
      //把蛇头移动一个向量
      headPos =  gameObject.transform.localPosition
      gameObject.transform.localPosition = new Vector(headPos.x+x,head.y+y,headPos.z);

      } 
  void Update()
  {
    if(Input.GetKey(KeyCode.W)) 
       {x=0;y=step;}
    if(Input.GetKey(KeyCode.S))
       {x=0;y=-step;}
    if(Input.GetKey(KeyCode.A))
       {y=0;x=-step;}
    if(Input.GetKey(KeyCode.D))
       {y=0;x=step;}


}
  void start
  { 
     invokeRepeading("move",0,velocity);//每隔一段时间执行这个函数
     x=step;y=0;
  } 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  

[展开全文]

蛇头需要的功能

  移动:自身移动、控制蛇身移动

  吃:增加蛇身,销毁食物

 

每个时间单位,蛇头向前移动一个单位

 

蛇身需要的功能

 移动:·每一节蛇身移动到它前面蛇身的位置

            ·蛇屁股移动到

[展开全文]

1.在四个边界处分别创造四个碰撞器

     创建上边界:创建空物体,长度1280,宽度10,设置锚点在上边,添加BoxColider2D碰撞器组件,将碰撞器的长度设为1280,宽度10

 

     同理创建其他碰撞器,注意锚点

 

2.为边界添加控件Image,如果游戏是边界模式,会现实Image,颜色设为红色

[展开全文]

授课教师

SiKi学院讲师

课程特色

图文(1)
视频(23)
下载资料(1)

学员动态