static void Main() { Console.WindowHeight = 16; Console.WindowWidth = 32; Pixel berry = new Pixel(Console.WindowWidth / 3, Console.WindowHeight / 3, ConsoleColor.Cyan); Pixel snake = new Pixel(Console.WindowWidth / 2, Console.WindowHeight / 2, ConsoleColor.Red); DrawBorders(); while (true) { ClearConsole(Console.WindowWidth, Console.WindowHeight); gameOver |= (snake.XPos == Console.WindowWidth - 1 || snake.XPos == 0 || snake.YPos == Console.WindowHeight - 1 || snake.YPos == 0); berry.Draw(); snake.Draw(); if (gameOver) { break; } var sw = Stopwatch.StartNew(); while (sw.ElapsedMilliseconds <= 200) { ReadKeyboard(); } } }
static void Main(string[] args) { WindowWidth = 32; WindowHeight = 16; SetBufferSize(WindowWidth, WindowHeight); int startingLength = 5; var head = new Pixel(WindowWidth / 2, WindowHeight / 2, ConsoleColor.Green); var body = new List <Pixel> (); var berry = new Berry(); Direction currentMovement = Direction.Right; Border.Draw(); Intro.Draw(); while (!Console.KeyAvailable) { } Intro.Clear(); Border.Draw(); berry.Spawn(); while (true) { var headOld = new Pixel(head.XPos, head.YPos); var sw = Stopwatch.StartNew(); while (sw.ElapsedMilliseconds <= 100) { currentMovement = ReadMovement(currentMovement); } body.Add(headOld); body.ForEach(x => x.Clear()); switch (currentMovement) { case Direction.Up: head.MoveUp(); break; case Direction.Down: head.MoveDown(); break; case Direction.Left: head.MoveLeft(); break; case Direction.Right: head.MoveRight(); break; } if (body.Count > Score.score + startingLength) { body.RemoveAt(0); } headOld.Clear(); body.ForEach(x => x.Draw()); head.Draw(); if (berry.WasEaten(head)) { Score.Increase(); berry.Spawn(); } Score.Draw(); if (body.Any(x => x.Touches(head) == true)) { break; } if (head.XPos == 0 || head.XPos == WindowWidth - 1 || head.YPos == 0 || head.YPos == WindowHeight - 1) { break; } } Score.Gameover(); head.Explode(); SetCursorPosition(1, 1); ReadKey(); }