简单的贪吃蛇游戏,目前存在的缺陷:没有死亡判定,没有障碍物,没有UI,可以按和移动方向相反的按键
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Control1 : MonoBehaviour
{
Vector3 oldPosition;
// Start is called before the first frame update
void Start()
{
InvokeRepeating("snakeMove", 0.1f, 0.5f);
}
// Update is called once per frame
void Update()
{
}
private void snakeMove()
{
oldPosition = transform.position;
float H = Input.GetAxis("Horizontal");
float V = Input.GetAxis("Vertical");
if (H < 0)
{
transform.position = transform.position + new Vector3(-2, 0, 0);
GameObject.Find("SnakeBody1").transform.GetComponent
GameObject.Find("SnakeBody1").transform.GetComponent
}
if (H > 0)
{
transform.position = transform.position + new Vector3(2, 0, 0);
GameObject.Find("SnakeBody1").transform.GetComponent
GameObject.Find("SnakeBody1").transform.GetComponent
}
if (V < 0)
{
transform.position = transform.position + new Vector3(0, 0, -2);
GameObject.Find("SnakeBody1").transform.GetComponent
GameObject.Find("SnakeBody1").transform.GetComponent
}
if (V > 0)
{
transform.position = transform.position + new Vector3(0, 0, 2);
GameObject.Find("SnakeBody1").transform.GetComponent
GameObject.Find("SnakeBody1").transform.GetComponent
}
}
}
移动的实现代码



