예제 #1
0
        public void keyControl()
        {
            if (Console.KeyAvailable)
            {
                var ch = Console.ReadKey(true).Key;
                switch (ch)
                {
                case ConsoleKey.UpArrow:
                    direction = snakeDirection.Up;
                    break;

                case ConsoleKey.DownArrow:
                    direction = snakeDirection.Down;
                    break;

                case ConsoleKey.LeftArrow:
                    direction = snakeDirection.Left;
                    break;

                case ConsoleKey.RightArrow:
                    direction = snakeDirection.Right;
                    break;
                }
            }
        }
예제 #2
0
        //Sets the snake direction based on keyboard input
        public void ChangeSnakeDirection()
        {
            var keyState = Keyboard.GetState();

            if (keyState.IsKeyDown(Keys.Up) && currentDirection != snakeDirection.down)
            {
                currentDirection = snakeDirection.up;
            }

            if (keyState.IsKeyDown(Keys.Down) && currentDirection != snakeDirection.up)
            {
                currentDirection = snakeDirection.down;
            }

            if (keyState.IsKeyDown(Keys.Left) && currentDirection != snakeDirection.right)
            {
                currentDirection = snakeDirection.left;
            }

            if (keyState.IsKeyDown(Keys.Right) && currentDirection != snakeDirection.left)
            {
                currentDirection = snakeDirection.right;
            }
        }