예제 #1
0
 // Private to prevent user from creating instance (singleton)
 private GameManager()
 {
     State = new GameState();
     SnakePosition = new List<Coord>();
     SnakeDirection = new Direction();
     DollarPosition = new Coord();
     randomGenerator = new Random();
 }
예제 #2
0
        /**
            function Init with the initialization of all the main variables
        */
        public void Init()
        {
            gameOver = false;
            pause = false;
            inUse = false;
            dir = new Direction();
            dir.newDir = 2;
            dir.lastDir = 2;
            boardWidth = Console.WindowWidth;
            boardHeight = Console.WindowHeight;
            snake = new List<Coord>();
            for(int i = 0; i<4; i++) {
                snake.Add(new Coord(10, 10));
            }

            Console.CursorVisible = false;
            Console.Title = "Westerdals Oslo ACT - SNAKE";
            Console.ForegroundColor = ConsoleColor.Green;
            Console.SetCursorPosition(10, 10);
            Console.Write("@");
            rand = new Random();
            egg = new Coord();
            while (true) {
                egg.X = rand.Next(0, boardWidth);
                egg.Y = rand.Next(0, boardHeight);
                bool spot = true;
                foreach (Coord i in snake)
                    if (i.X == egg.X && i.Y == egg.Y) {
                        spot = false;
                        break;
                    }
                if (spot) {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.SetCursorPosition(egg.X, egg.Y);
                    Console.Write("$");
                    break;
                }
            }
        }
예제 #3
0
        // Move snake 1 step on x or y axis
        public void MoveSnake(Direction direction)
        {
            // Add 4 snake elements when game has been started
            if (SnakePosition.Count < 4)
                AddSnakeElement();

            SnakeDirection = direction;
            int addX = 0;
            int addY = 0;

            // Move position 1 pixel to direction
            if (direction == Direction.Down)
                addY = 1;
            else if (direction == Direction.Up)
                addY = -1;
            else if (direction == Direction.Left)
                addX = -1;
            else if (direction == Direction.Right)
                addX = 1;

            // Get position of head and tail
            Coord headPosition = SnakePosition.ElementAt(0);
            Coord tailPosition = SnakePosition.ElementAt(SnakePosition.Count - 1);

            // Add x or y pixel to head
            int newX = headPosition.x + addX;
            int newY = headPosition.y + addY;

            // Set first body element position
            int bodyX = headPosition.x;
            int bodyY = headPosition.y;

            // Remove last element (tail)
            int tailX = tailPosition.x;
            int tailY = tailPosition.y;
            Console.SetCursorPosition(tailX, tailY);
            Console.Write(" ");
            SnakePosition.RemoveAt(SnakePosition.Count - 1);

            // Add the new head to first element
            SnakePosition.Insert(0, new Coord(newX, newY));
            if (newX >= 0 && newY >= 0
                && newX < Board.BoardWidth
                && newY < Board.BoardHeight)
            {
                Console.SetCursorPosition(newX, newY);
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.Write("@");
                // Overwrite character of second element to body character
                Console.SetCursorPosition(bodyX, bodyY);
                Console.Write("0");
            }

            CheckIfSnakeHitElements();
        }