Beispiel #1
0
        public void Move(Direction direction)
        {
            if (((int)direction + 2) % 4 == (int)previousMove)
            {
                direction = previousMove;
            }
            else
            {
                previousMove = direction;
            }
            Square head  = Snake.First.Value;
            int    headX = head.X;
            int    headY = head.Y;

            switch (direction)
            {
            case Direction.UP:
                headY -= 1;
                if (headY < 0)
                {
                    Gameover = true;
                    return;
                }
                break;

            case Direction.LEFT:
                headX -= 1;
                if (headX < 0)
                {
                    Gameover = true;
                    return;
                }
                break;

            case Direction.DOWN:
                headY += 1;
                if (headY >= Y)
                {
                    Gameover = true;
                    return;
                }
                break;

            case Direction.RIGHT:
                headX += 1;
                if (headX >= X)
                {
                    Gameover = true;
                    return;
                }
                break;
            }
            Square newHead = new Square(headX, headY);

            if (Snake.Contains(newHead))
            {
                Gameover = true;
                return;
            }
            Snake.AddFirst(newHead);
            if (newHead.Equals(Food))
            {
                Hunger = X * Y;
                Score += 1;
                if (X * Y - Snake.Count == 0)
                {
                    Gameover = true; return;
                }
                CreateFood(random);
            }
            else
            {
                Snake.RemoveLast();
            }

            --Hunger;
            if (Hunger <= 0)
            {
                Gameover = true;
            }
            ++LifeTime;
        }