public void Move(SnakeMoveDirection newDirection) { // if (newDirection == SnakeMoveDirection.None) // { // throw new ArgumentException("Snake Move Error: directon cannot be none"); // } SnakeMoveDirection availableDirection = GetAvailableNewDirection(newDirection); Point newHeadNode = GetNewHeadNode(availableDirection); AddNewHeadNode(newHeadNode); }
/// <summary> /// 获取有效的移动方向 /// </summary> /// <returns></returns> SnakeMoveDirection GetAvailableNewDirection(SnakeMoveDirection newDirection) { SnakeMoveDirection currentDirection = GetCurrentDirection(); if (newDirection == SnakeMoveDirection.None || (currentDirection == SnakeMoveDirection.Left && newDirection == SnakeMoveDirection.Right) || (currentDirection == SnakeMoveDirection.Right && newDirection == SnakeMoveDirection.Left) || (currentDirection == SnakeMoveDirection.Up && newDirection == SnakeMoveDirection.Down) || (currentDirection == SnakeMoveDirection.Down && newDirection == SnakeMoveDirection.Up) ) { return currentDirection; } else { return newDirection; } }
// private void panelScene_Paint(object sender, PaintEventArgs e) // { // _game.Draw(e.Graphics); // } // // private void panelScene_Resize(object sender, EventArgs e) // { // _game.DisplayRectangle = panelScene.ClientRectangle; // Invalidate(); // } private void FormSnakeGame_KeyDown(object sender, KeyEventArgs e) { switch (e.KeyCode) { case Keys.Left: _snakeDirection = SnakeMoveDirection.Left; break; case Keys.Right: _snakeDirection = SnakeMoveDirection.Right; break; case Keys.Up: _snakeDirection = SnakeMoveDirection.Up; break; case Keys.Down: _snakeDirection = SnakeMoveDirection.Down; break; } }
/// <summary> /// 依据移动方向,获取新的头部节点 /// </summary> /// <param name="direction"></param> /// <returns></returns> Point GetNewHeadNode(SnakeMoveDirection direction) { Point firstNode = BodyNodes.First(); int x = firstNode.X; int y = firstNode.Y; switch (direction) { case SnakeMoveDirection.Left: --x; break; case SnakeMoveDirection.Right: ++x; break; case SnakeMoveDirection.Up: --y; break; case SnakeMoveDirection.Down: ++y; break; } return new Point(x, y); }
public void SnakeMove(SnakeMoveDirection direction) { _snake.Move(direction); }