Exemplo n.º 1
0
        private void loop()
        {
            uint prevS = s.length;

            while (true)
            {
                if (Console.KeyAvailable)
                {
                    ConsoleKey key  = Console.ReadKey(false).Key;
                    Direction  dnew = Direction.UP;
                    switch (key)
                    {
                    case ConsoleKey.UpArrow:
                        dnew = Direction.UP;
                        break;

                    case ConsoleKey.RightArrow:
                        dnew = Direction.RIGHT;
                        break;

                    case ConsoleKey.DownArrow:
                        dnew = Direction.DOWN;
                        break;

                    case ConsoleKey.LeftArrow:
                        dnew = Direction.LEFT;
                        break;

                    default: break;
                    }

                    if ((dnew == Direction.UP && s.direction == Direction.DOWN) ||
                        (dnew == Direction.DOWN && s.direction == Direction.UP) ||
                        (dnew == Direction.LEFT && s.direction == Direction.RIGHT) ||
                        (dnew == Direction.RIGHT && s.direction == Direction.LEFT))
                    {
                        ;
                    }
                    else
                    {
                        s.direction = dnew;
                    }
                }
                if (prevS != s.length)
                {
                    prevS = s.length;
                    message("Score: " + s.length);
                }

                ConsoleColor[] prev = prevCC();

                Point tail = s.move();
                if (tail != null)
                {
                    setCC(ConsoleColor.Black);
                    writeCP(tail.x, tail.y);
                }

                setCC(ConsoleColor.Green);
                Point head = s.head;
                writeCP(head.x, head.y);
                setCC(prev);

                for (int i = 0; i < mice.Count; i++)
                {
                    if (head.x == mice[i].x && head.y == mice[i].y)
                    {
                        s.addLength(2);
                        mice.RemoveAt(i);
                    }
                }

                if (mice.Count < 3)
                {
                    addMouse();
                }

                if (s.head.x <= 1 || s.head.x >= width - 2 ||
                    s.head.y <= 1 || s.head.y >= height - 2)
                {
                    reset("You hit the wall!");
                }
                else if (s.selfCollision)
                {
                    reset("You ran into your self!");
                }

                Thread.Sleep(50);
            }
        }