Unity学习笔记(10)仿手游贪吃蛇第六章「随机生成食物」「吃到食物蛇身自动增加」

本文介绍了一个使用Unity3D开发的贪吃蛇游戏。游戏核心功能包括随机生成食物和蛇吃到食物后增长。当蛇头碰到食物时,会自动增加新的身体部分,并立即生成新的食物。同时,如果蛇碰到墙壁,则游戏结束。代码中详细展示了游戏逻辑,包括蛇的移动、身体部分的更新以及与食物和墙壁的碰撞检测。
摘要由CSDN通过智能技术生成

一、实现效果

在这里插入图片描述

二、核心功能

  • 随机生成食物

void CreateFood()
{
float x = Random.Range(-500f, 500f);
float y = Random.Range(-260f, 260f);
float z = 0;
GameObject food=Instantiate< GameObject>(FoodPrefab, new Vector3(x, y, z), Quaternion.identity);
food.transform.SetParent(canv.transform, false);//再将它设为canvas的子物体
Debug.Log(“位置:” + food.transform.position + “父节点:” + food.transform.parent);
}

  • 吃到食物自动增加

private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag.Equals(“Food”))
{
Destroy(collision.gameObject);
GameObject newbodynext = Instantiate(body,
snakeBody[snakeBody.Count - 1].transform.position,
Quaternion.identity);
newbodynext.transform.SetParent(canv.transform, false);//再将它设为canvas的子物体
snakeBody.Add(newbodynext);
CreateFood();
}
else if (collision.tag.Equals(“Wall”))
{
Debug.Log(“游戏结束”);
}
}

三、挂在蛇头上的代码

ps:需要用到上上章的挂在“中心圆”节点上CenterCircle脚本里的thlta变量。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class Plane : MonoBehaviour
{
    float timer = 0f;        //设置加数
    float interval = 0.2f;   //循环一次的时间
    float speed = 1.5f;      //加的速度
   
    public GameObject FoodPrefab;//食物预制体
    public GameObject body;//身体预设体
    List<GameObject> snakeBody = new List<GameObject>();//一定记住设对象数组的格式
    GameObject canv;
   // GameObject Food;



    void Start()
    {
        canv= GameObject.Find("Canvas");
      //  Food= GameObject.Find("蛇头");
        for (int i = 0; i < 3; ++i)
        {
            Vector3 SnakeHeadPos = transform.position;
            SnakeHeadPos.z = 0;
           // Debug.Log("SnakeHeadPos:"+SnakeHeadPos);
            GameObject newbodynext = Instantiate<GameObject>(body,
            SnakeHeadPos - (i + 1) * new Vector3(0, 40f, 0f),/*40f是因为蛇头中心点与蛇身中心点的距离为40f*/ Quaternion.identity);

           // Debug.Log("蛇身:"+newbodynext.transform.position);
            newbodynext.transform.SetParent(canv.transform, false);//再将它设为canvas的子物体
            snakeBody.Add(newbodynext);
        }

        CreateFood();
        CreateFood();


    }

    void Update()
    {
        
        timer += speed * Time.deltaTime;
        if (timer > interval)
        {
            timer = 0;
            Vector3 tmpPosition = transform.position;    //记录头部变化前的位置
            List<Vector3> tmpList = new List<Vector3>(); //记录身体变化前的位置 


            for (int i = 0; i < snakeBody.Count; ++i)
            {
                tmpList.Add(snakeBody[i].transform.position);
            }

            transform.Translate(0, 0.5f, 0);//定时移动一定的距离(蛇头中心点到蛇身中心点的距离)

            snakeBody[0].transform.position = tmpPosition;//将0移到头部之前的位置

            for (int i = 1; i < snakeBody.Count; ++i)//依次前移身体的位置
            {
                snakeBody[i].transform.position = tmpList[i - 1];
            }
        }

        //从中心圆获取Rotation的z值
        float thlta_z = GameObject.Find("中心圆").GetComponent<CenterCircle>().thlta;
        //Debug.Log("转角:"+thlta_z);
        transform.rotation = Quaternion.Euler(0, 0, thlta_z);

    }
    void CreateFood()
    {
        float x = Random.Range(-500f, 500f);
        float y = Random.Range(-260f, 260f);
        float z = 0;
     
        GameObject food=Instantiate<GameObject>(FoodPrefab, new Vector3(x, y, z), Quaternion.identity);
        food.transform.SetParent(canv.transform, false);//再将它设为canvas的子物体
        Debug.Log("位置:" + food.transform.position + "父节点:" + food.transform.parent);

    }


    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag.Equals("Food"))
        {
            Destroy(collision.gameObject);
            GameObject newbodynext = Instantiate<GameObject>(body,
            snakeBody[snakeBody.Count - 1].transform.position,
            Quaternion.identity);
            newbodynext.transform.SetParent(canv.transform, false);//再将它设为canvas的子物体
            snakeBody.Add(newbodynext);
            CreateFood();
        }
        else if (collision.tag.Equals("Wall"))
        {
            Debug.Log("游戏结束");
        }

    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值