SIKI学习——贪吃蛇案例05

本文详细介绍了如何实现贪吃蛇游戏,包括蛇身生成、两种移动方法、边界穿越机制以及奖励目标的生成与获取。通过处理蛇身的移动和边界交互,增加了游戏的趣味性和挑战性。
摘要由CSDN通过智能技术生成

01-处理蛇身的生成

将Sh贪吃蛇头更名为SnakeHead,新建Image更名为SnakeBody
在这里插入图片描述

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SnakeHead : MonoBehaviour
{
    public List<Transform> bodyList = new List<Transform>();//存储身体的位置
    public float velocity=0.35f;//每隔多久要调用,实际就相当于速度
    public int step;//小蛇每一步要走的路
    private int x;//x和Y都是移动的增量
    private int y;
    private Vector3 HeadPos;//记录头的位置
    private Transform canvas;
    public GameObject bodyPrefab;//蛇身预制体
    public Sprite[] bodySprites = new Sprite[2];
     void Start()
    {
        InvokeRepeating("Move",0,velocity);
        x = 0;//刚开始的时候,贪吃蛇就会往上走
        y =step;
    }
      void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))//CancelInvoke()取消当前的Invoke
        {
            CancelInvoke();
            InvokeRepeating("Move", 0, velocity-0.2f);//跑快
        }
        if (Input.GetKeyUp(KeyCode.Space))
        {
            CancelInvoke();
            InvokeRepeating("Move", 0, velocity);//减速就是回到正常速度
        }
        if (Input.GetKey(KeyCode.W)&&y!=-step)//y!=-step加这句是蛇头向下的时候不是直接向下的
        {
            gameObject.transform.localRotation = Quaternion.Euler(0,0,0);//将Float值转换成四元数
            x = 0;
            y = step;
        }
        if (Input.GetKey(KeyCode.S)&&y != step)
        {
            gameObject.transform.localRotation = Quaternion.Euler(0, 0, 180);//将Float值转换成四元数
            x = 0;
            y = -step;
        }
        if (Input.GetKey(KeyCode.A) && x != step)
        {
            gameObject.transform.localRotation = Quaternion.Euler(0, 0, 90);//将Float值转换成四元数
            x = -step;
            y = 0;
        }
        if (Input.GetKey(KeyCode.D) && x != -step)
        {
            gameObject.transform.localRotation = Quaternion.Euler(0, 0, -90);//将Float值转换成四元数
            x = step;
            y = 0;
        }
    }
        /// <summary>
    /// 头部的移动
    /// </summary>
    void Move()
    {
        HeadPos = gameObject.transform.localPosition;
        gameObject.transform.localPosition = new Vector3(HeadPos.x + x,HeadPos.y+y,HeadPos.z);
    }
     /// <summary>
    /// 生成蛇身
    /// </summary>
    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);
    }
       private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.CompareTag("Food"))//也可以写成collision.tag == "Food"
        {
            Destroy(collision.gameObject);
            FoodMaker.Instance.MakeFood();
        }
    }
}

02-处理蛇身的移动之方法一

用移动方法二,这个不适合双色蛇身

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
public class SnakeHead : MonoBehaviour
{
    public List<Transform> bodyList = new List<Transform>();//存储身体的位置
    public float velocity=0.35f;//每隔多久要调用,实际就相当于速度
    public int step;//小蛇每一步要走的路
    private int x;//x和Y都是移动的增量
    private int y;
    private Vector3 HeadPos;//记录头的位置
    private Transform canvas;
    public GameObject bodyPrefab;//蛇身预制体
    public Sprite[] bodySprites = new Sprite[2];
     private void Awake()
    {
        canvas = GameObject.Find("Canvas").transform;
    }
    void Start()
    {
        InvokeRepeating("Move",0,velocity);
        x = 0;//刚开始的时候,贪吃蛇就会往上走
        y =step;
    }
     void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))//CancelInvoke()取消当前的Invoke
        {
            CancelInvoke();
            InvokeRepeating("Move", 0, velocity-0.2f);//跑快
        }
        if (Input.GetKeyUp(KeyCode.Space))
        {
            CancelInvoke();
            InvokeRepeating("Move", 0, velocity);//减速就是回到正常速度
        }
        if (Input.GetKey(KeyCode.W)&&y!=-step)//y!=-step加这句是蛇头向下的时候不是直接向下的
        {
            gameObject.transform.localRotation = Quaternion.Euler(0,0,0);//将Float值转换成四元数
            x = 0;
            y = step;
        }
         
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值