Square.cs: Public classsquare:monobehaviour{ Public intRow, col; Publicrect rect; PublicTexture Texture; /// <summary> ///1 up 2 right 3 down 4 left/// </summary> Public intNextdir =1; /// <summary> ///0 Background 1 Snake body 2 food/// </summary> Public intState =0; PublicSquare () {} PublicSquare (intRowintcol, rect rect, Texture Texture) { This. Row =Row; This. Col =Col; This. Rect =rect; This. Texture =texture; }} SnakeEating.cs: Public classsnakeeating:monobehaviour{ Public intRow, col;//Number of rows and columns PublicTexture black, white, yellow;//three kinds of background map Public floatSpeed =0.5f;//time interval to move seconds PrivateSquare[][] squares;//two-dimensional arrays Private BOOLIsgameover =true;//Game StatusList<Square> Snake;//node collection of snakes voidStart () {startgame (); }/// <summary> ///Start the game/// </summary> voidStartgame () {intW = (Screen.height/row);//Since 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. intx = (Screen.width/2-(W * col)/2);//Center The X-axis of the block that sets the whole inty = (Screen.height/2-(w * row)/2);//Center The Y-axis of the block that sets the wholeSquares=NewSquare[row][];//initialize a two-dimensional arrayRect Temprect=NewRect (x, Y, W, W);//Initialize the position and size of the first block for(inti =0; i < row; i++) {Squares[i]=NewSquare[col];//instantiate an array of rows for(intj =0; J < Col; J + +) {Squares[i][j]=NewSquare (i, J, Temprect, Black);//Instantiate a nodeTemprect.x + = W;//the x-axis plus the width of a block} temprect.x= x;//x-axis back to the leftTemprect.y + = W;//the y-axis plus the width of a block reaches the next line} Snake=NewList<square> ();//Initialize the collection of snakesSquare head = squares[(int) Row/2][(int) Col/2];//set the head node as a square in the centerHead.texture = white;//WhiteHead.state =1;//Snake BodyHead.nextdir = Random.range (1,5);//give the direction of the move randomlySnake. ADD (head);//Add to CollectionIsgameover=false;//The game is not overStartcoroutine ("Snakemove");//Start a collaboration program} IEnumerator Snakemove () {Setfood (); while(!isgameover) {Square Head= snake[0];//get the head node. intNewRow = head.row, newcol = Head.col;//gets the row and column subscript of the head node. Switch(snake[0].nextdir)//Locate the subscript of the next node based on the direction of the head junction{ Case 1: NewRow-=1; Break; Case 2: Newcol+=1; Break; Case 3: NewRow+=1; Break; Case 4: 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]);//joins the next node to the beginning of the collectionsnake[0].texture = white;//set the color of itsnake[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{Square End= Snake[snake. Count-1]; End.texture=Black; End.state=0; Snake. RemoveAt (Snake. Count-1); }Else{Setfood ();//if it's food, re-set a food .} snake[0].state =1;//Set the head node status to 1 (snake body)}yield return NewWaitforseconds (speed);//wait for speed secondsSpeed-= time.deltatime/ -;//Shorten time to move and increase speed}}/// <summary> ///Decorate Food///Find the node on a random row///determine if the node is not the state (0 blanks, 1 snake body, 2 food) is not 0 then continue to randomly select rows and columns .///set the found node's state to 2 and the color to yellow/// </summary> Public voidSetfood () {intFoodrow = Random.range (0, Row); intFoodcol = 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 BodySquares[foodrow][foodcol].texture = yellow;//Yellow color}voidUpdate () {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 the head node to the next direction 1 on 2 right 3 down 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; }}}voidOngui () {if(!isgameover) {//draw each item in a two-dimensional array for(inti =0; i < row; i++) { for(intj =0; J < Col; J + +) {Square square=Squares[i][j]; Gui. Drawtexture (Square.rect, square.texture); } }}}
Ongui Draw Snake in Unity