Ejemplo n.º 1
0
        public static void Choice(IPixel head, Direction.Movement currentMovement)
        {
            switch (currentMovement)
            {
            case Direction.Movement.Up:
            {
                head.YPosition--;
                break;
            }

            case Direction.Movement.Down:
            {
                head.YPosition++;
                break;
            }

            case Direction.Movement.Left:
            {
                head.XPosition--;
                break;
            }

            case Direction.Movement.Right:
            {
                head.XPosition++;
                break;
            }
            }
        }
Ejemplo n.º 2
0
        void IPixel.Play(ref int score, ref bool gameover, IPixel head, ref IPixel berry)
        {
            do
            {
                Clear();
                gameover |= head.XPosition == WindowWidth - 1 || head.XPosition == 0 || head.YPosition == WindowHeight - 1 || head.YPosition == 0;
                head.Score(ref score, head, ref berry);
                for (int i = 0; i < body.Count; i++)
                {
                    head.DrawPixel(body[i]);
                    gameover |= body[i].XPosition == head.XPosition && body[i].YPosition == head.YPosition;
                }

                if (gameover)
                {
                    break;
                }

                head.DrawPixel(head);
                berry.DrawPixel(berry);
                const int milliseconds = 500;
                Stopwatch stopWatch    = Stopwatch.StartNew();
                while (stopWatch.ElapsedMilliseconds <= milliseconds)
                {
                    currentMovement = Direction.ReadMovement(currentMovement);
                }

                body.Add(new Pixel(head.XPosition, head.YPosition, ConsoleColor.Green));
                Direction.Choice(head, currentMovement);
                if (body.Count > score)
                {
                    body.RemoveAt(0);
                }
            }while (true);
        }