static void Pseudoclear() { Pixel.drawPixel(head_clear.x, head_clear.y, empty); Pixel.drawPixel(Berry.x, Berry.y, empty); if (body_clear.x != 0 || body_clear.y != 0) { Pixel.drawPixel(body_clear.x, body_clear.y, empty); } }
private static void DrawWalls() { //izris zgornjega in spodnjega zidu zidu for (int x = 0; x < ScreenWidth - 1; x++) { Pixel wall = new Pixel(x, 1); Pixel.drawPixel(wall); wall = new Pixel(x, ScreenHeight - 2); Pixel.drawPixel(wall); } //izris levega in desnega zidu for (int y = 1; y < Console.WindowHeight - 1; y++) { Pixel wall = new Pixel(0, y); Pixel.drawPixel(wall); wall = new Pixel(ScreenWidth - 2, y); Pixel.drawPixel(wall); } }
static void Game() { while (!GameOver) { Console.SetCursorPosition(0, 0); Console.Write(String.Format("{0,0} {1,30}", $"P1 score: { Convert.ToString(ScoreP1)}", $"P2 score: { Convert.ToString(ScoreP2)}")); Pseudoclear(); Pixel.drawPixel(SnakeHeadP1); Pixel.drawPixel(SnakeHeadP2); Pixel.drawPixel(Berry1); Pixel.drawPixel(Berry2); Pixel.drawPixel(Berry3); Pixel.drawPixel(Berry4); foreach (Pixel segment in SnakeBodyP1) { Pixel.drawPixel(segment); } foreach (Pixel segment in SnakeBodyP2) { Pixel.drawPixel(segment); } GetMovementDirection(); if (curent_directionP1 != Moving_directions.None) { Pixel bodySegment = new Pixel(SnakeHeadP1.x, SnakeHeadP1.y, SegmentColorP1); SnakeBodyP1.Add(bodySegment); if (SnakeBodyP1.Count > ScoreP1 + StartingSize) { body_clearP1 = new Pixel(SnakeBodyP1[0]); SnakeBodyP1.RemoveAt(0); } } if (curent_directionP2 != Moving_directions.None) { Pixel bodySegment = new Pixel(SnakeHeadP2.x, SnakeHeadP2.y, SegmentColorP2); SnakeBodyP2.Add(bodySegment); if (SnakeBodyP2.Count > ScoreP2 + StartingSize) { body_clearP2 = new Pixel(SnakeBodyP2[0]); SnakeBodyP2.RemoveAt(0); } } Move(); BerryCollisionDetection(); foreach (Pixel segment in SnakeBodyP1) { if (segment.x == SnakeHeadP1.x && segment.y == SnakeHeadP1.y) { GameOver = true; winner = Winner.P2; break; } } foreach (Pixel segment in SnakeBodyP2) { if (segment.x == SnakeHeadP1.x && segment.y == SnakeHeadP1.y) { GameOver = true; winner = Winner.P2; break; } } foreach (Pixel segment in SnakeBodyP1) { if (segment.x == SnakeHeadP2.x && segment.y == SnakeHeadP2.y) { GameOver = true; winner = Winner.P1; break; } } foreach (Pixel segment in SnakeBodyP2) { if (segment.x == SnakeHeadP2.x && segment.y == SnakeHeadP2.y) { GameOver = true; winner = Winner.P1; break; } } if (SnakeHeadP2.x <= 1 || SnakeHeadP2.x == ScreenWidth - 2 || SnakeHeadP2.y == 1 || SnakeHeadP2.y == ScreenHeight - 2) { GameOver = true; winner = Winner.P1; } if (SnakeHeadP1.x <= 1 || SnakeHeadP1.x == ScreenWidth - 2 || SnakeHeadP1.y == 1 || SnakeHeadP1.y == ScreenHeight - 2) { GameOver = true; winner = Winner.P2; } if (SnakeHeadP1.x == SnakeHeadP2.x && SnakeHeadP1.y == SnakeHeadP2.y) { GameOver = true; winner = Winner.Draw; } } Game_Over(); }
static void Game() { while (!GameOver) { Console.SetCursorPosition(0, 0); Console.Write($"Score: { Convert.ToString(Score)}"); Pseudoclear(); Pixel.drawPixel(SnakeHead); Pixel.drawPixel(Berry); foreach (Pixel segment in SnakeBody) { Pixel.drawPixel(segment); } GetMovementDirection(); if (curent_direction != Moving_directions.None) { Pixel bodySegment = new Pixel(SnakeHead.x, SnakeHead.y, SegmentColor); SnakeBody.Add(bodySegment); if (SnakeBody.Count > Score + StartingSize) { body_clear = new Pixel(SnakeBody[0]); SnakeBody.RemoveAt(0); } } switch (curent_direction) { case Moving_directions.Up: head_clear = new Pixel(SnakeHead); SnakeHead.y--; break; case Moving_directions.Left: head_clear = new Pixel(SnakeHead); SnakeHead.x -= 2; break; case Moving_directions.Down: head_clear = new Pixel(SnakeHead); SnakeHead.y++; break; case Moving_directions.Right: head_clear = new Pixel(SnakeHead); SnakeHead.x += 2; break; } if (SnakeHead.x == Berry.x && SnakeHead.y == Berry.y) { Score++; do { Berry = new Pixel(rng.Next(2, ScreenWidth - 2), rng.Next(2, ScreenHeight - 2), ConsoleColor.Red); } while (Berry.x % 2 != 0.0); } foreach (Pixel segment in SnakeBody) { if (segment.x == SnakeHead.x && segment.y == SnakeHead.y) { GameOver = true; break; } } if (SnakeHead.x <= 1 || SnakeHead.x == ScreenWidth - 2 || SnakeHead.y == 1 || SnakeHead.y == ScreenHeight - 2) { GameOver = true; } /// } Game_Over(); }