// Tạo vị trí ngẫu nhiên cho food
private void RandomizePosition()
{
bool samePosition = false;
Vector3 check = new Vector3();
Bounds bounds = this.gridArea.bounds;
while(true)
{
// random vị trí cho food
float x = Random.Range(bounds.min.x , bounds.max.x);
float y = Random.Range(bounds.min.y, bounds.max.y);
check = new Vector3(Mathf.Round(x),Mathf.Round(y),0f);
// kiểm tra vị trí của food vừa random có trùng với từng phần đuôi con rắn ko
// Nếu trùng (samePosition = true) thì tiếp tục lặp random tiếp.
//Nếu không (samePosition = false) thì thoát vòng lặp
// snake._segment là list chứa position của từng phần co rắn
for(int i = 0 ; i < snake._segment.Count ; i++)
{
if(check == snake._segment[i].position)
{
samePosition = true;
break;
}
}
if(samePosition == false) break;
}
this.transform.position =check;
}