コード例 #1
0
ファイル: Game.cs プロジェクト: NikolayRogchev/Snake-Game
        static void Main(string[] args)
        {
            InitialSetup.InitializeWindow();
            SnakePlayer  snakePlayer  = new SnakePlayer("Nikolay");
            AppleHandler appleHandler = new AppleHandler();
            Timer        timer        = new Timer(x => appleHandler.GenerateApple(), null, 1000, 3000);

            Print.PrintWindow(snakePlayer);
            while (true)
            {
                if (!snakePlayer.IsAlive)
                {
                    Print.PrintEndGame(snakePlayer);
                    break;
                }
                Print.ClearSnake(snakePlayer);
                if (!Console.KeyAvailable)
                {
                    snakePlayer.Move(appleHandler);
                }
                else
                {
                    ConsoleKeyInfo keyPressed = Console.ReadKey();
                    snakePlayer.Move(keyPressed, appleHandler);
                }
                Print.PrintApples(appleHandler);
                Print.PrintSnake(snakePlayer);
                Thread.Sleep(100);
            }
        }
コード例 #2
0
 public static void PrintApples(AppleHandler appleHandler)
 {
     foreach (Apple apple in appleHandler.Apples)
     {
         Console.SetCursorPosition(apple.Position.X, apple.Position.Y);
         Console.Write(apple.Symbol);
     }
 }
コード例 #3
0
        private void UpdatePlayerPosition(Direction direction, AppleHandler appleHandler)
        {
            Direction = direction;
            Point newHeadPosition = new Point(HeadPosition.X, HeadPosition.Y);

            switch (direction)
            {
            case Direction.Left:
                newHeadPosition.X--;
                break;

            case Direction.Right:
                newHeadPosition.X++;
                break;

            case Direction.Up:
                newHeadPosition.Y--;
                break;

            case Direction.Down:
                newHeadPosition.Y++;
                break;

            default:
                break;
            }

            if (HasCollidedWithWall())
            {
                Die();
                return;
            }

            HeadPosition = newHeadPosition;
            if (HasCollidedWithApple(appleHandler.Apples))
            {
                EatApple(HeadPosition, appleHandler);
            }
            else
            {
                Body.Insert(0, newHeadPosition);
                Body.RemoveAt(Body.Count() - 1);
            }
        }
コード例 #4
0
        public void Move(ConsoleKeyInfo keyPressed, AppleHandler appleHandler)
        {
            Direction newDirection = SwitchDirection(keyPressed);

            UpdatePlayerPosition(newDirection, appleHandler);
        }
コード例 #5
0
 public void Move(AppleHandler appleHandler)
 {
     UpdatePlayerPosition(this.Direction, appleHandler);
 }
コード例 #6
0
 private void EatApple(Point position, AppleHandler appleHandler)
 {
     Body.Insert(0, position);
     appleHandler.RemoveApple(position);
 }