public Snake(int left, int top, int length, Player player) { int index = (Console.WindowWidth - 2) * (Console.WindowHeight - 5); coords = new List <Point>(); this.player = player; if (player.Score > player.Highscore) { player.Highscore = player.Score; } player.Score = 0; player.PrintScore(); ConsoleColor colorBefore = Console.ForegroundColor; Console.ForegroundColor = player.Color; Dir = Direction.Up; collisionSnake = (CollisionObject)player.PlayerNumber; Lenght = length; IsDead = false; while (coordinates[left, top] == CollisionObject.Star) { Star.Print(); } coords.Add(new Point(left, top)); coordinates[left, top] = CollisionObject.SnakeHead; Console.SetCursorPosition(left, top); Console.Write(HeadChar); for (int i = top + 1; i <= top + Lenght; i++) { while (coordinates[left, i] == CollisionObject.Star) { Star.Print(); } coords.Add(new Point(left, i)); coordinates[left, i] = collisionSnake; Console.SetCursorPosition(left, i); Console.Write(BodyChar); } Console.ForegroundColor = colorBefore; }
static void Main(string[] args) { Console.OutputEncoding = Encoding.UTF8; Console.CursorVisible = false; Console.Title = "Snake - Multiplayer"; Console.ForegroundColor = defaultForegroundColor; Console.BackgroundColor = defaultBackgroundColor; if (!File.Exists(configPath)) { Utils.PrintHighlight("", "Recommended font is Consolas with fontsize 16!", "\nPress any key to continue...", ConsoleColor.Cyan); Console.ReadKey(true); } AppendConfig(); Console.ForegroundColor = defaultForegroundColor; Console.BackgroundColor = defaultBackgroundColor; SetWindowSize(); Star.CoordinateSystem = Snake.CoordinateSystem = coords = new CollisionObject[Console.WindowWidth, Console.WindowHeight - 3]; Star.Coordinates = new Point(randy.Next(1, Console.WindowWidth - 1), randy.Next(1, Console.WindowHeight - 4)); Console.Clear(); string[] playerNames = { "Player 1", "Player 2" }; if (askForName) { for (int i = 0; i < playerNames.Length; i++) { Utils.PrintHighlight("", "Player " + (i + 1), " Name: ", playerColor[i]); playerNames[i] = Utils.Read(maxNameLength, readColor); Console.WriteLine(); if (playerNames[i].Length == 0) { playerNames[i] = "Player " + (i + 1); } } } Console.Clear(); Player p1 = new Player(playerNames[0], 1, playerColor[0], p1ControlKeys); Player p2 = new Player(playerNames[1], 2, playerColor[1], p2ControlKeys); //Console.WriteLine("Press any key to start..."); //Console.ReadKey(true); DrawFrame(); p1.PrintScore(0, Console.WindowHeight - 3); p2.PrintScore(Console.WindowWidth - maxNameLength - 1, Console.WindowHeight - 3); p1.Snake = new Snake(Console.WindowWidth / 4, (Console.WindowHeight - 8) / 2, startLength, p1); p2.Snake = new Snake(Console.WindowWidth / 2 + Console.WindowWidth / 4, (Console.WindowHeight - 8) / 2, startLength, p2); Star.Print(); new Thread(() => { while (true) { if (Console.KeyAvailable) { ConsoleKeyInfo key = Console.ReadKey(true); if (keyBuffer.Count > 0) { if (keyBuffer[keyBuffer.Count - 1] != key) { keyBuffer.Add(key); } } else { keyBuffer.Add(key); } } Thread.Sleep(1); } }).Start(); while (true) { int time = Environment.TickCount; if (keyBuffer.Count > 0) { if (keyBuffer[0].Key == ConsoleKey.Escape || keyBuffer[0].Key == ConsoleKey.P) { Pause(); time = Environment.TickCount; } for (int i = 0; i < 4; i++) { int oppositeDir = (i < 2) ? i + 2 : i - 2; if (keyBuffer[0].Key == p1.ControlKeys[i]) { if (p1.Snake.Dir != (Direction)oppositeDir) { p1.Snake.Dir = (Direction)i; } } if (keyBuffer[0].Key == p2.ControlKeys[i]) { if (p2.Snake.Dir != (Direction)oppositeDir) { p2.Snake.Dir = (Direction)i; } } } keyBuffer.RemoveAt(0); } Star.Coordinates = new Point(randy.Next(1, Console.WindowWidth - 1), randy.Next(1, Console.WindowHeight - 4)); if (p1.Snake.CanRespawn) { p1.Snake = new Snake(Console.WindowWidth / 4, (Console.WindowHeight - 8) / 2, startLength, p1); } else if (!p1.Snake.IsDead) { Snake.HandleCollision(p1.Snake, p2.Snake); if (!p1.Snake.IsDead) { p1.Snake.Move(); } } if (p2.Snake.CanRespawn) { p2.Snake = new Snake(Console.WindowWidth / 2 + Console.WindowWidth / 4, (Console.WindowHeight - 8) / 2, startLength, p2); } else if (!p2.Snake.IsDead) { Snake.HandleCollision(p2.Snake, p1.Snake); if (!p2.Snake.IsDead) { p2.Snake.Move(); } } while (time + tickSpeed >= Environment.TickCount) { Thread.Sleep(1); } } }