Beispiel #1
0
        static void Main(string[] args)
        {
            Console.Title = "SnakeGame :: Guila767";
            Console.SetWindowSize(nWidht, nHeight);
            Console.ForegroundColor = ConsoleColor.Green;
            wHandle = _Console.CreateConsoleScreenBuffer(_Console.GENERIC_READ | _Console.GENERIC_WRITE, 0, IntPtr.Zero, _Console.CONSOLE_TEXTMODE_BUFFER, IntPtr.Zero);
            _Console.SetConsoleActiveScreenBuffer(wHandle);
            uint BytesWritten;

            // Game initialize
            SnakeGame = new Game(ref ScreenBuffer, new Size(nWidht, nHeight));

            // Game Loop
            while (true)
            {
                // Check The Console Size
                if (_Console.GetAsyncKeyState((int)ConsoleKey.M) != 0)
                {
                    Console.SetWindowSize(nWidht, nHeight);  // Doesn't work
                }
                // Update The Game and ScreenBuffer
                SnakeGame.UpdateGame();

                // Check if the Snake Die
                if (SnakeGame.Snake_Status == Game.SnakeStatus.Death)
                {
                    ScreenBuffer = new char[nHeight * nWidht];
                    ScreenBuffer = _Console.WriteText(ScreenBuffer, (nWidht * ((nHeight / 2) - 1) - (nWidht / 2)) - (Properties.Resources.Lose1.Length / 2), Properties.Resources.Lose1);
                    ScreenBuffer = _Console.WriteText(ScreenBuffer, (nWidht * (nHeight / 2) - (nWidht / 2)) - ((Properties.Resources.Lose2.Length + SnakeGame.Score.ToString().Length + 1) / 2), Properties.Resources.Lose2 + $" {SnakeGame.Score}");
                    _Console.WriteConsoleOutputCharacter(wHandle, ScreenBuffer, (UInt32)ScreenBuffer.Length, new COORD(0, 0), out BytesWritten);
                    while (_Console.GetAsyncKeyState(Buttons.VK_RETURN) == 0)
                    {
                        ;
                    }
                    ScreenBuffer = new char[nHeight * nWidht];
                    SnakeGame    = new Game(ref ScreenBuffer, new Size(nWidht, nHeight));
                    continue;
                }

                // Easy way to dalay the game, probaly not the best one
                if (SnakeGame.SnakeDirection == Game.Direction.Right || SnakeGame.SnakeDirection == Game.Direction.Left)
                {
                    Thread.Sleep(50);
                }
                else
                {
                    Thread.Sleep(66); // 66.64ms
                }
                // Write The buffer in the screen
                _Console.WriteConsoleOutputCharacter(wHandle, ScreenBuffer, (UInt32)ScreenBuffer.Length, new COORD(0, 0), out BytesWritten);
            }
        }