在一个有趣的项目上工作,我要编写多人贪吃蛇的代码。
我在 Unity 中创建了两个预制件,分别称为“SnakeBodyHorizontal”和“SnakeBodyVertical”。目标是使用正确的预制件根据方向更改蛇的一段。如果蛇身向下/向上移动,我使用 Snake body horizontal。如果蛇身向左/向右移动,我想使用垂直预制件。默认蛇身是水平的。
我的问题:我正在努力使所有蛇体部分始终根据它们移动的方向保持正确的方向,如示例图像所示。
我的剧本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Snake : MonoBehaviour
{
private Vector2 _direction = Vector2.right;
private List<Transform> _segments = new List<Transform>();
private KeyCode lastHitKey = KeyCode.D;
public Transform segmentPrefab;
public int initialSize = 3;
//sprites for snake head
public Sprite SnakeHeadUp;
public Sprite SnakeHeadRight;
public Sprite SnakeHeadLeft;
public Sprite SnakeHeadDown;
private void Start()
{
ResetState();
}
// Update: Updated Once Per Time (Edit -> Project Settings -> Time (0.09) to change difficulty!)
// This function is controlling the direction our snake moves.
void Update()
{
if(Input.GetKeyDown(KeyCode.W) && lastHitKey != KeyCode.S)
{
this.gameObject.GetComponent<SpriteRenderer>().sprite = SnakeHeadUp;
lastHitKey = KeyCode.W;
_direction = Vector2.up;
} else if (Input.GetKeyDown(KeyCode.S) && lastHitKey != KeyCode.W)
{
this.gameObject.GetComponent<SpriteRenderer>().sprite = SnakeHeadDown;
lastHitKey = KeyCode.S;
_direction = Vector2.down;
} else if (Input.GetKeyDown(KeyCode.A) && lastHitKey != KeyCode.D)
{
this.gameObject.GetComponent<SpriteRenderer>().sprite = SnakeHeadLeft;
lastHitKey = KeyCode.A;
_direction = Vector2.left;
} else if(Input.GetKeyDown(KeyCode.D) && lastHitKey != KeyCode.A)
{
this.gameObject.GetComponent<SpriteRenderer>().sprite = SnakeHeadRight;
lastHitKey = KeyCode.D;
_direction = Vector2.right;
}
}
//This transforms the position of our snake head, rounded to a whole number. This should force our snake to move in a grid.
//if a segment has the same x value as the previous segment, it must be cos turnt!!! then change segments sprite
public void FixedUpdate()
{
for (int i = _segments.Count - 1; i > 0; i--){
_segments[i].position = _segments[i - 1].position;
}
this.transform.position = new Vector3(
Mathf.Round(this.transform.position.x) + _direction.x,
Mathf.Round(this.transform.position.y) + _direction.y,
0.0f
);
}
//Function handles the logic of iterating in reverse from a list of prefabs of our snake segments, to create a new snake segment.
private void Grow()
{
Transform segment = Instantiate(this.segmentPrefab);
segment.position = _segments[_segments.Count - 1].position; //each segment inherits position
_segments.Add(segment);
}
//Function handles the logic of resetting the state of our game upon collision with objects other than food
private void ResetState(){
//emptying the snakelist
for(int i = 1; i < _segments.Count; i++)
{
Destroy(_segments[i].gameObject);
}
_segments.Clear();
_segments.Add(this.transform);
//resetting our snake from 1 back to initialsize (3)
for(int i = 1; i < this.initialSize; i++)
{
_segments.Add(Instantiate(this.segmentPrefab));
//initalizing the original segments
_segments[i].position = new Vector3(
-50.0f,
0.0f,
0.0f
);
}
this.transform.position = new Vector3(
-5.0f,
0.0f,
0.0f
);
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Food")
{
Grow();
}
else if (other.tag == "Obstacle")
{
ResetState();
}
}
}
我试图旋转我正在使用的预制精灵,这取决于以前的数组 x 值是否与当前数组不同,如果 X 值不相同,那么它已经垂直移动。然而,这个实现非常有问题。所以更好的建议会很好