标签:
Square.cs : public class Square : MonoBehaviour { public int row, col; public Rect rect; public Texture texture; /// <summary> /// 1向上 2向右 3向下 4向左 /// </summary> public int nextDir = 1; /// <summary> /// 0背景 1蛇身 2食物 /// </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;//行数和列数 public Texture black, white, yellow;//三种背景图 public float speed = 0.5f;//移动的时间间隔 秒 private Square[][] Squares;//二维数组 private bool isGameOver = true;//游戏状态 List<Square> snake;//蛇的节点集合 void Start() { StartGame(); } /// <summary> /// 开始游戏 /// </summary> void StartGame() { int w = (Screen.height / row);//由于一般屏幕宽度大于高度,所以用高度除以行数得出每一个方块的边长 int x = (Screen.width / 2 - (w * col) / 2);//设置整体的方块的x轴居中 int y = (Screen.height / 2 - (w * row) / 2);//设置整体的方块的Y轴居中 Squares = new Square[row][];//初始化二维数组 Rect tempRect = new Rect(x, y, w, w);//初始化第一个方块的位置和大小 for (int i = 0; i < row; i++) { Squares[i] = new Square[col];//实例化一行数组 for (int j = 0; j < col; j++) { Squares[i][j] = new Square(i, j, tempRect, black);//实例化一个节点 tempRect.x += w;//x轴加上一个方块的宽度 } tempRect.x = x;//x轴回到最左边 tempRect.y += w;//y轴加上一个方块的宽度到达下一行 } snake = new List<Square>();//初始化蛇的集合 Square head = Squares[(int)row / 2][(int)col / 2];//设置头结点为正中央的一个方块 head.texture = white;//白色 head.state = 1;//蛇身 head.nextDir = Random.Range(1, 5);//随机给出前进的方向 snake.Add(head);//添加到集合 isGameOver = false;//游戏没有结束 StartCoroutine("snakeMove");//启动协同程序 } IEnumerator snakeMove() { SetFood(); while (!isGameOver) { Square head = snake[0];//获取头结点 int newRow = head.row, newCol = head.col;//获取头结点的行列下标 switch (snake[0].nextDir)//根据头结点的方向找到下一个节点的下标 { case 1: newRow -= 1; break; case 2: newCol += 1; break; case 3: newRow += 1; break; case 4: newCol -= 1; break; } //如果下一个节点的下标超出了二维数组的边界则游戏结束 if (newRow < 0 || newRow >= row || newCol < 0 || newCol >= col) { isGameOver = true; break; } else if (Squares[newRow][newCol].state == 1)//如果下一个节点是蛇身则说明吃到自己了,因此游戏结束 { isGameOver = true; break; } else { snake.Insert(0, Squares[newRow][newCol]);//将下一个节点加入集合的开头 snake[0].texture = white;//设置它的颜色 snake[0].nextDir = head.nextDir;//设置方向为上一个头结点的方向 if (snake[0].state != 2)//如果新的头结点不是是食物则将最后一个节点设置为黑色,状态为0(空白),从列表中移除 { Square end = snake[snake.Count - 1]; end.texture = black; end.state = 0; snake.RemoveAt(snake.Count - 1); } else { SetFood();//如果是食物则重新设置一个食物 } snake[0].state = 1;//设置头结点的状态为1(蛇身) } yield return new WaitForSeconds(speed);//等待speed秒 speed -= Time.deltaTime / 50;//缩短移动的时间提高速度 } } /// <summary> /// 布置食物 /// 找到一个随机行列的上的节点 /// 判断结点如果不是的state(0空白,1蛇身,2食物)不是0则继续随机选择行列 /// 设置找到的节点的state为2,颜色设置为黄色 /// </summary> public void SetFood() { int foodRow = Random.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;//蛇身 Squares[foodRow][foodCol].texture = yellow;//黄颜色 } 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)//判断用于防止反方向行走 return; } snake[0].nextDir = 1;//设置头结点的下一个方向 1上 2右 3下 4左 } 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) { //绘制二维数组中每一项 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); } } } } }
标签:
原文地址:http://www.cnblogs.com/yoatl/p/4743513.html