Square.cs:
Public class Square : monobehaviour
{
Public int row, col;
Public rect rect;
Public Texture Texture;
<summary>
1 Up 2 right 3 down 4 left
</summary>
Public int nextdir = 1;
<summary>
0 background 1 Snake body 2 food
</summary>
Public int state = 0;
Public Square () {}
Public Square (int row, int col, rect rect, Texture Texture)
{
this. Row = row;
this. Col = col;
this. Rect = rect;
this. Texture = texture;
}
}
SnakeEating.cs:
Public class snakeeating : monobehaviour
{
Public int row, col; //number of rows and columns
Public Texture Black, white, yellow; //Three kinds of background map
Public float speed = 0.5f; //Move time interval seconds
Private Square [] squares; //Two-D arrays
Private bool isgameover = true; //Game status
List<Square> snake; //Snake node collection
void Start ()
{
Startgame ();
}
<summary>
Start the game
</summary>
void Startgame ()
{
int w = (screen. Height/row); //Because the general screen width is greater than the height, the side length of each block is calculated by dividing the height by the number of rows.
int x = (screen. WIDTH/2-(W * col)/2); //Set the center of the x-axis of the whole block
int y = (screen. HEIGHT/2-(w * row)/2); //Set the center of the y-axis of the whole block
Squares = new Square[row][]; //Initialize two-dimensional arrays
RectTemprect =New Rect(x, Y, W, W);//Initialize the position and size of the first block
for(inti = 0; i < row; i++)
{
Squares[I] =New Square[Col];//Instantiate an array of rows
for(intj = 0; J < Col; J + +)
{
SquaresI [j] =New Square(I, J, Temprect, Black);//instantiation of a node
Temprect.x + = W;//x Axis plus the width of a block
}
Temprect.x = x;//x axis Back to the left
Temprect.y + = W;//y Axis plus a block width to the next line
}
Snake = new List<Square> (); //Initialize the collection of snakes
Square head = squares[(int) row/2][(int) COL/2]; //Set head node as a square in the center
Head.texture = white;//White
Head.state = 1;//Snake body
Head.nextdir = Random. Range (1, 5); //randomly give direction of advance
Snake. ADD (head); //Add to collection
Isgameover = false; //The game is not over
Startcoroutine ("Snakemove"); //Start the collaboration program
}
IEnumeratorSnakemove ()
{
Setfood ();
while(!isgameover)
{
Squarehead = snake[0];//Get head node
intNewRow = head.row, newcol = Head.col;//Gets the row and column subscript of the head node.
Switch(Snake[0].nextdir)//Find subscript of next node according to the direction of head node
{
Case1:
NewRow-= 1;
Break;
Case2:
Newcol + = 1;
Break;
Case3:
NewRow + = 1;
Break;
Case4:
Newcol-= 1;
Break;
}
//If the subscript of the next node exceeds the bounds of the two-dimensional array, the game is over .
if(NewRow < 0 | | newRow >= Row | | Newcol < 0 | | newcol >= COL)
{
Isgameover = true;
Break;
}
Else if(Squares[NewRow] [newcol].state = 1)//If the next node is a snake body then the description eats itself up so the game is over
{
Isgameover =true;
Break;
}
Else
{
Snake. Insert (0, Squares[newrow][newcol]);//Add the next node to the beginning of the collection
Snake[0].texture = white;//Set its color
Snake[0].nextdir = Head.nextdir;//Set direction to the direction of the previous head node
if(Snake[0].state! = 2)//If the new head node is not food then the last node is set to black with a status of 0 (blank), removed from the list
{
SquareEnd = Snake[snake. COUNT-1];
End.texture = black;
end.state = 0;
Snake. RemoveAt (Snake. COUNT-1);
}
Else
{
Setfood ();//If it is food, re-set a food
}
Snake[0].state = 1;//Set the head node status to 1 (snake body)
}
yield return new waitforseconds (speed); //wait for speed seconds
Speed-= time . deltatime/50; //shortening the time to move increases the speed of the
}
}
///<summary>
///decorate food
////Find a node on the random column
///</summary>
public Span style= "color: #0000ff;" >void Setfood ()
{
int foodrow = ra Ndom . Range (0, row);
int foodcol = random . Range (0, col);
while (Squares[foodrow][foodcol].state! = 0)
{
Foodrow = Random. Range (0, row);
Foodcol = Random. Range (0, col);
}
Squares[foodrow][foodcol].state = 2; //Snake body
Squares[foodrow][foodcol].texture = yellow; //Yellow color
}
void Update()
{
if(!isgameover)
{
if(Input. Getkeydown (Keycode.uparrow))
{
if(Snake. Count > 1)
{
if(Snake[1].row = = Snake[0].row-1 && Snake[1].col = = Snake[1].col)//Judging to prevent walking in the opposite direction
return;
}
Snake[0].nextdir = 1;//Set head node next direction 1 on 2 right 3 lower 4 left
}
if(Input. Getkeydown (Keycode.rightarrow))
{
if(Snake. Count > 1)
{
if(Snake[1].col = = Snake[0].col + 1 && snake[1].row = = Snake[1].row)
return;
}
Snake[0].nextdir = 2;
}
if(Input. Getkeydown (Keycode.downarrow))
{
if(Snake. Count > 1)
{
if(Snake[1].row = = Snake[0].row + 1 && snake[1].col = = Snake[1].col)
return;
}
Snake[0].nextdir = 3;
}
if(Input. Getkeydown (Keycode.leftarrow))
{
if(Snake. Count > 1)
{
if(Snake[1].col = = Snake[0].col-1 && Snake[1].row = = Snake[1].row)
return;
}
Snake[0].nextdir = 4;
}
}
}
void Ongui ()
{
if (!isgameover)
{
Draw each item in a two-dimensional array
for (int i = 0; i < row; i++)
{
for (int j = 0; J < Col; J + +)
{
Square square = squares[i][j];
GUI. Drawtexture (Square.rect, square.texture);
}
}
}
}
}
Ongui Draw Snake in Unity