コード例 #1
0
ファイル: MySnake.cs プロジェクト: PetkoKostov/Csharp
        // --------------------------------------------------------------------------------
        static void Main()
        {
            welcome();
            Console.Clear();
            Console.BufferHeight = Console.WindowHeight;

            position[] directions = new position[]
            {
                new position(0, 1),     //0 -> right    >
                new position(0, -1),    //1 -> left     <
                new position(1, 0),     //2-> down      v
                new position(-1, 0)     //3 -> up       ^
            };

            double speed = new double();

            if (level == 1)
            {
                Level1 lev_one = new Level1();
                speed = 100;
                play(lev_one, directions, speed);
            }
            else
            {
                Level2 lev_two = new Level2();
                speed = 70;
                play(lev_two, directions, speed);
            }
        }
コード例 #2
0
ファイル: MySnake.cs プロジェクト: PetkoKostov/Csharp
        public static void play(Level2 lev_two, position[] directions, double speed)
        {
            lev_two.ap.Display();
            DateTime startTime = DateTime.Now;

            while (true)
            {
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo userInput = Console.ReadKey();
                    lev_two.get_direction(userInput);
                }
                position oldHead = new position();
                position next    = new position();
                position newHead = new position();
                oldHead = lev_two.Snake.OldHead();
                next    = directions[lev_two.Direction];
                // GAME OVER logic
                lev_two.print_walls();
                newHead = new position(oldHead.row + next.row,
                                       oldHead.col + next.col);
                if (lev_two.check_snake_dead(newHead))
                {
                    lev_two.snake_dead(startTime, 2);
                }
                Console.SetCursorPosition(oldHead.col, oldHead.row);
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.Write("*");
                //include the new head in the queue
                lev_two.Snake.Put(newHead);
                Console.SetCursorPosition(newHead.col, newHead.row);
                lev_two.show_direction();
                if (lev_two.snake_eating(newHead))
                {
                    // make the snake eat
                    lev_two.apple = new Apple(lev_two.Snake);
                    lev_two.apple.Display();
                    //speed -= 0.01; // if we want to speed on every success
                }
                else
                {
                    position last = new position();
                    last = lev_two.Snake.RemoveLast();
                    lev_two.apple.Display();
                    Console.SetCursorPosition(last.col, last.row);
                    Console.Write(" ");
                }
                speed -= 0.01;
                Thread.Sleep((int)speed);
            }
        }