예제 #1
0
        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);
        }
예제 #2
0
        /// <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;
            }
        }
예제 #3
0
 //         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;
     }
 }
예제 #4
0
        /// <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);
        }
예제 #5
0
파일: Game.cs 프로젝트: HungryAnt/SnakeGame
 public void SnakeMove(SnakeMoveDirection direction)
 {
     _snake.Move(direction);
 }