private static void StartGame() { Graphics.initGraphics(Console.BufferWidth, Console.BufferHeight); Walls walls = new Walls(79, 25, '+'); walls.Draw(); Point point = new Point(4, 5, '*'); Snake snake = new Snake(point, 4, Direction.RIGHT); snake.Draw(); FoodCreator foodCreator = new FoodCreator(Console.BufferWidth, Console.BufferHeight, '$'); Point food = foodCreator.GenerateFood(); food.Draw(); int count = 0; while (game) { if (Console.KeyAvailable) { ConsoleKeyInfo key = Console.ReadKey(true); switch (key.Key) { case ConsoleKey.RightArrow: snake.Turn(Direction.RIGHT); break; case ConsoleKey.LeftArrow: snake.Turn(Direction.LEFT); break; case ConsoleKey.UpArrow: snake.Turn(Direction.UP); break; case ConsoleKey.DownArrow: snake.Turn(Direction.DOWN); break; } } snake.Move(); if (snake.isHit(food)) { snake.Grow(); food = foodCreator.GenerateFood(); food.Draw(); count++; } if (snake.isHit(walls.GetPoints()) || snake.isDead(snake.GetPoints())) { snake.Clear(); food.Clear(); GameOver(count); } Thread.Sleep(100); } Console.ReadKey(); }
static void Main(string[] args) { int windowWidth = 110, windowHeight = 36; Map map = new Map(windowWidth, windowHeight); Snake snake = new Snake('O'); Food food = new Food(windowWidth, windowHeight); ConsoleKeyInfo key = new ConsoleKeyInfo(); map.Draw(); long Score = 0; bool isInc = false; while (snake.GetAlive() == true) { if (Console.KeyAvailable == true) { key = Console.ReadKey(true); } snake.IsAlive(windowWidth, windowHeight); snake.Update(key); //isInc = food.Draw(snake.GetHeadX(), snake.GetHeadY()); isInc = food.Draw(snake.GetCoordsX(), snake.GetCoordsY()); if (isInc) { snake.IncLength(); Score += 20; } if (snake.IsIntersected() == true) { snake.SetAlive(false); } map.PrintScore(Score); Score++; Thread.Sleep(80); if (snake.GetAlive() == false) { break; } } snake.Clear(); map.Draw(); map.GameOverPrint(windowWidth, windowHeight); Console.ReadKey(); Console.SetCursorPosition(windowWidth / 4, (windowHeight / 5) + 20); }
public void MoveSnake() { while (isAlive) { snake.Move(); if (snake.IsCollisionWithObject(food)) { snake.body.Add(new Point(1, 22)); DrawScore(); if (snake.body.Count < 9 && snake.body.Count % 4 == 0) { time = time - 70; wall.Clear(); wall.NextLevel(); wall.Draw(); DrawLevel(); snake.InitialPosition(); while (food.IsCollisionWithObject(snake) || food.IsCollisionWithObject(wall)) { food.Generate(); } food.Draw(); } else { while (food.IsCollisionWithObject(snake) || food.IsCollisionWithObject(wall)) { food.Generate(); } food.Draw(); } } if (snake.IsCollisionWithSnake(snake)) { isAlive = false; } if (snake.IsCollisionWithObject(wall)) { isAlive = false; } if (isAlive == false) { if (File.Exists(Nickname + ".xml")) { File.Delete(Nickname + ".xml"); } Console.Clear(); Console.ForegroundColor = ConsoleColor.Red; Console.SetCursorPosition(23, 10); Console.WriteLine("GAME OVER"); Console.SetCursorPosition(22, 12); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("YOUR SCORE:" + ((snake.body.Count * 10) - 10)); Console.ReadKey(); break; } snake.Clear(); snake.Draw(); Thread.Sleep(time); } }