Пример #1
0
 static void InitUi(int x, int y)
 {
     Console.CursorVisible = false;
     ConsoleSize.SetWindowSize(x + 2, y + 7);
     Console.SetCursorPosition(0, 0);
     ConsoleWrite.WriteRowOf('#');
     for (int i = 0; i < y; i++)
     {
         Console.Write("#");
         for (int j = 0; j < x; j++)
         {
             Console.Write(" ");
         }
         Console.Write("#");
     }
     ConsoleWrite.WriteRowOf('#');
     ConsoleWrite.WriteCentered("Snake");
     ShowScore(y, 0);
 }
Пример #2
0
        static void PlayTron(int sizeX, int sizeY, int speed)
        {
            //Initialize playing area
            InitUi(sizeX, sizeY);
            //Create snake from snake.cs and set it's bounds so it dies when is leaves the play area
            var player1 = new Snake(sizeX, sizeY, (sizeX + 2) / 4, (sizeY + 2) / 2, 1);
            var player2 = new Snake(sizeX, sizeY, ((sizeX + 2) / 4) * 3, (sizeY + 2) / 2, 1);

            player1.Length = int.MaxValue;
            player2.Length = int.MaxValue;
            //Create a new thread to take in and pass controls to the snake, start it.
            var player1Control = new Thread(() => Controls(player1,
                                                           Key.W,
                                                           Key.S,
                                                           Key.A,
                                                           Key.D));
            var player2Control = new Thread(() => Controls(player2,
                                                           Key.Up,
                                                           Key.Down,
                                                           Key.Left,
                                                           Key.Right));

            player1Control.SetApartmentState(ApartmentState.STA);
            player2Control.SetApartmentState(ApartmentState.STA);
            player1Control.Start();
            player2Control.Start();
            //--STAT--
            int      killedByWall  = 0;
            DateTime sinceLastTick = DateTime.Now;

            Thread.Sleep(500);
            while (!player1.Dead && !player2.Dead)
            {
                if (DateTime.Now - sinceLastTick > TimeSpan.FromMilliseconds(3000 / speed))
                {
                    sinceLastTick = DateTime.Now;
                    player1.Tick();
                    player2.Tick();
                }
                if (player1.Dead || player2.Dead)
                {
                    killedByWall = 1;
                    break;
                }
                //If snake 1's head is in player 1 or 2, kill it.
                if (player2.IsTouching(player1.Head.Key, player1.Head.Value))
                {
                    player1.Dead = true;
                }
                //If snake 2's head is in player 1, kill it.
                if (player1.IsTouching(player2.Head.Key, player2.Head.Value))
                {
                    player2.Dead = true;
                }
            }
            //Stop controls thread.
            player1Control.Abort();
            player2Control.Abort();
            Console.SetCursorPosition(1, sizeY + 3);
            int player1Wins = player2.Dead && !player1.Dead ? 1 : 0;

            Console.Clear();
            Console.CursorTop = Console.WindowHeight / 2;
            ConsoleWrite.WriteCentered("Game Over");
            ConsoleWrite.WriteCentered(player1.Dead && player2.Dead ?
                                       "Both Players Died!" :
                                       "Player " + (player1.Dead ? "1" : "2") + " Died");
            Thread.Sleep(2000);
            PostStats(new [] { 2, player1Wins, killedByWall });
        }
Пример #3
0
        static void Play(int sizeX, int sizeY, int speed, bool loop)
        {
            int score = 0;

            //Initialize playing area
            InitUi(sizeX, sizeY);
            //Create snake from snake.cs and set it's bounds so it dies when is leaves the play area
            var snake = new Snake(sizeX, sizeY)
            {
                Loop = loop
            };
            //Create a new thread to take in and pass controls to the snake, start it.
            var controlThread = new Thread(() => Controls(snake,
                                                          Key.Up,
                                                          Key.Down,
                                                          Key.Left,
                                                          Key.Right));

            controlThread.SetApartmentState(ApartmentState.STA);
            controlThread.Start();
            //Make new apples until Apple is not inside of snake
            var apple = new Apple(sizeX, sizeY);

            while (snake.IsTouching(apple.X, apple.Y))
            {
                apple = new Apple(sizeX, sizeY);
            }

            DateTime sinceLastTick = DateTime.Now;

            Thread.Sleep(500);
            int ticks = 0;

            while (!snake.Dead)
            {
                if (DateTime.Now - sinceLastTick > TimeSpan.FromMilliseconds(1000 / speed))
                {
                    sinceLastTick = DateTime.Now;
                    snake.Tick();
                }
                //If snake isn't touching apple, skip back to start of while loop
                if (!snake.IsTouching(apple.X, apple.Y))
                {
                    continue;
                }
                //If it is touching apple
                snake.Length++;
                //Add to the score based on a basic algorithm taking into account size of play area and speed
                score += (int)Math.Ceiling((speed ^ 2 * 1000) / Math.Sqrt(sizeX * sizeY / 2));
                //Update the score's display value
                ShowScore(sizeY, score);
                while (snake.IsTouching(apple.X, apple.Y))
                {
                    //Write snake body to Apples previous location
                    Console.SetCursorPosition(apple.X, apple.Y);
                    Console.Write("O");
                    //Spawn new apple
                    apple = new Apple(sizeX, sizeY);
                }
                ticks += 1;
            }
            //Stop controls thread.
            controlThread.Abort();
            Console.CursorTop = Console.WindowHeight / 2;
            ConsoleWrite.WriteCentered("Game Over");
            PostStats(new [] { loop ? 1 : 0, score, snake.Length - 4, ticks });
        }