简单的贪吃蛇游戏,目前存在的缺陷:没有死亡判定,没有障碍物,没有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<controlBody>().setPosition(oldPosition);
GameObject.Find("SnakeBody1").transform.GetComponent<controlBody>().bodyMove();
}
if (H > 0)
{
transform.position = transform.position + new Vector3(2, 0, 0);
GameObject.Find("SnakeBody1").transform.GetComponent<controlBody>().setPosition(oldPosition);
GameObject.Find("SnakeBody1").transform.GetComponent<controlBody>().bodyMove();
}
if (V < 0)
{
transform.position = transform.position + new Vector3(0, 0, -2);
GameObject.Find("SnakeBody1").transform.GetComponent<controlBody>().setPosition(oldPosition);
GameObject.Find("SnakeBody1").transform.GetComponent<controlBody>().bodyMove();
}
if (V > 0)
{
transform.position = transform.position + new Vector3(0, 0, 2);
GameObject.Find("SnakeBody1").transform.GetComponent<controlBody>().setPosition(oldPosition);
GameObject.Find("SnakeBody1").transform.GetComponent<controlBody>().bodyMove();
}
}
}
移动的实现代码
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/sharpzhang/article/details/123646992