Unity realizes Snake game

Time:2020-12-5

Unity greedy snake basic principle realization, for your reference, the specific content is as follows

Principle:

1. Each body moves with the body in front of it;
2. The snake head automatically goes straight ahead and can turn left or right.

Thought:

There are several bodies of greedy snakes. Each body has a common feature, which is to move with the previous body. Here, the snake’s body is abstracted and expressed by a snackbody class. Each body segment creates a snackbody object, and then operates this object to realize functions. The snake head can be regarded as a special snake body. There should be a manager to manage all snake bodies, so there is a snackcontroller class to manage. Each snake has three attributes: front, self and oldpos.

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

public class SnackBody
{
  Public snackbody front; // indicates the previous snake body
  Public transform self; // indicates the current snake body
  Vector3 oldpos; // previous position of current snake body

  public SnackBody(SnackBody tmpFront, Transform tmpSelf)
  {
    front = tmpFront;
    self = tmpSelf;
    oldPos = tmpSelf.position;
  }

  //This section of the snake has moved forward, update oldpos
  public void Reflash()
  {
    oldPos = this.self.position;
  }

  //Follow the snake ahead
  public void FollowFront()
  {
    self.position  = front.oldPos ; // update snake position
    front.Reflash(); 
  }
  
  public virtual void MoveForward(){}
  }

public class SnackHead : SnackBody
{
  //Inherit base class
  public SnackHead(SnackBody tmpFront,Transform tmpSelf):base(tmpFront,tmpSelf) 
  {
  }
  public override void MoveForward()
  {
    self.Translate(-self.forward, Space.World);
  }

  /// <summary>
  ///Move right
  /// </summary>
  public void TurnRight()
  {
    Vector3 tmpAngle = self.localEulerAngles;
    tmpAngle.y += 90;
    self.localEulerAngles = tmpAngle;
  }

  /// <summary>
  ///Move left
  /// </summary>
  public void TurnLeft()
  {
    Vector3 tmpAngle = self.localEulerAngles;
    tmpAngle.y -= 90;
    self.localEulerAngles = tmpAngle;
  }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SnackController : MonoBehaviour {

  List < snackbody > allbodies; // stores all snake bodies

  public Transform snackHead;
  public Transform bodyOne;
  public Transform bodyTwo;

  // Use this for initialization
  void Start () {

    allBodys = new List<SnackBody>();

    SnackHead tmpHead = new SnackHead(null, snackHead);
    allBodys.Add(tmpHead);

    SnackBody tmpOneBody = new SnackBody(tmpHead, bodyOne);
    allBodys.Add(tmpOneBody);

    SnackBody tmpTwoBody = new SnackBody(tmpOneBody, bodyTwo);
    allBodys.Add(tmpTwoBody);

    StartCoroutine(MoveFront());
  }

  IEnumerator MoveFront()
  {
    While (true) // use an endless loop to keep the snake moving
    {
      allBodys[0].MoveForward();

      yield return new WaitForSeconds(0.5f);

      for(int i=1;i<allBodys.Count; i++)
      {
        allBodys[i].FollowFront();
        yield return new WaitForSeconds(0.2f);
      }
    }
  }
 
 // Update is called once per frame
 void Update () {
 
    if(Input.GetKeyDown(KeyCode.A))
    {
      ((SnackHead)allBodys[0]).TurnLeft();
    }

    if (Input.GetKeyDown(KeyCode.D))
    {
      ((SnackHead)allBodys[0]).TurnRight();
    }
  }
}

More interesting classic game implementation topics, to share with you:

C + + classic games summary

Python classic games summary

Python Tetris game collection

JavaScript classic games are always playing

Java classic games summary

JavaScript classic games summary

The above is the whole content of this article, I hope to help you in your study, and I hope you can support developeppaer more.