public void Move(int off, Snake.Directions direction) { if (direction == Snake.Directions.LEFT) { x -= off; } if (direction == Snake.Directions.RIGHT) { x += off; } if (direction == Snake.Directions.UP) { y -= off; } if (direction == Snake.Directions.DOWN) { y += off; } }
static void Main(string[] args) { Console.SetBufferSize(80, 40); Wall walls = new Wall(50, 25); walls.Draw(); Console.SetCursorPosition(60, 10); int sc = 0; Console.WriteLine("Score: " + sc); Point p = new Point(5, 5, '*'); Snake snake = new Snake(5, p, Snake.Directions.RIGHT); snake.Draw(); FoodCreator foodCreator = new FoodCreator(25, 18, '$'); Point food = foodCreator.CreateFood(); Console.ForegroundColor = ConsoleColor.DarkRed; food.Draw(); Console.ForegroundColor = ConsoleColor.White; while (true) { if(walls.isHit(snake) || snake.isHitTail()) { break; } if (snake.Eat(food)) { sc += 1; Console.SetCursorPosition(60, 10); Console.WriteLine("Score: " + sc); food = foodCreator.CreateFood(); Console.ForegroundColor = ConsoleColor.DarkRed; food.Draw(); Console.ForegroundColor = ConsoleColor.White; Point[] arr = snake.plist.ToArray(); for(int i = 0; i < arr.Length; i++) { if(food.isHit(arr[i])) { food.sym = '*'; food.Draw(); food = foodCreator.CreateFood(); Console.ForegroundColor = ConsoleColor.DarkRed; food.Draw(); Console.ForegroundColor = ConsoleColor.White; } } } else snake.Move(); Thread.Sleep(100); if (Console.KeyAvailable) { ConsoleKeyInfo cki = new ConsoleKeyInfo(); cki = Console.ReadKey(); snake.HandleKey(cki); } } Console.SetCursorPosition(18, 10); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("GAME OVER"); Console.SetCursorPosition(12, 12); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("PRESS ANY KEY TO EXIT"); Console.ReadLine(); }