//Kiem tra an moi public static bool CheckEat(Struct.DIEM pointHeader, Struct.DIEM pointFood) { if (pointHeader.x==pointFood.x && pointHeader.y==pointFood.y) { Console.SetCursorPosition(pointHeader.x, pointHeader.y); Console.Write("X"); return true; } return false; }
//Check collision public static bool CheckCollision(Struct.DIEM point) { if (point.x < 40 || point.x>90) { return true; } if(point.y < 2 || point.y > 27) { return true; } return false; }
// check collision trailer public static bool CheckCollisionTrailer(List<Struct.DIEM> Snake, Struct.DIEM pointHeader) { List<Struct.DIEM> SnakeTrailer = new List<Struct.DIEM>(); for (int i = 1; i< Snake.Count; i++) { SnakeTrailer.Add(Snake[i]); } int inS = SnakeTrailer.IndexOf(pointHeader); if (inS > 0) { return true; } else return false; }
//ve lại snake static void RePaintSnake(Struct.DIEM Header, Struct.DIEM Neck, Struct.DIEM Trailer) { HeaderSnake(Header); FragmentSnake(Neck); DeleteTrailerSnake(Trailer); }
// control snake static string ControlSnake(int level, List <Struct.DIEM> Snake, string DirectionSnake, ref Struct.DIEM randomPoint, ref bool checkEat, ref int score) { Random randomXY = new Random(); Struct.DIEM oldFood; oldFood.x = 0; oldFood.y = 0; ConsoleKeyInfo input; var startTime = DateTime.UtcNow; int count = 1; bool check = true; level = (11 - level) * 25; int stage = randomXY.Next(6, 12); do { while (!Console.KeyAvailable) { if (DateTime.UtcNow - startTime > TimeSpan.FromMilliseconds(level)) { Snake = MoveSnake(Snake, DirectionSnake, score); if (Snake.Count == 0) { return("GAMEOVER"); } startTime = DateTime.UtcNow; count++; } if (count == stage && checkEat == true) { randomPoint.x = randomXY.Next(40, 90); randomPoint.y = randomXY.Next(2, 27); checkEat = false; if (randomPoint.x != 0) { RandomPointPanit(randomPoint); } count = 1; } if (!checkEat && count % 9 == 0) { if (randomPoint.x != 0) { RandomPointPanit(randomPoint); } } if (MiniGameOver.CheckEat(Snake[0], randomPoint) && !checkEat) { oldFood.x = randomPoint.x; oldFood.y = randomPoint.y; checkEat = true; Snake.Add(randomPoint); score++; Console.SetCursorPosition(23, 3); Console.BackgroundColor = ConsoleColor.White; Console.Write(score); count = 1; } if (checkEat && randomPoint.x != 0) { //FragmentSnake(randomPoint); HeaderSnakeEatting(Snake[0]); FragmentSnake(Snake[1]); } } input = Console.ReadKey(true); if (input.Key == ConsoleKey.UpArrow || input.Key == ConsoleKey.LeftArrow) { check = false; } if (input.Key == ConsoleKey.DownArrow || input.Key == ConsoleKey.RightArrow) { check = false; } if (input.Key.ToString() == DirectionSnake) { check = true; } if (input.Key == ConsoleKey.Escape) { check = false; break; } switch (DirectionSnake) { case "UpArrow": if (input.Key.ToString() == "DownArrow" || input.Key.ToString() == "UpArrow") { check = true; } break; case "DownArrow": if (input.Key.ToString() == "UpArrow" || input.Key.ToString() == "DownArrow") { check = true; } break; case "LeftArrow": if (input.Key.ToString() == "RightArrow" || input.Key.ToString() == "LeftArrow") { check = true; } break; case "RightArrow": if (input.Key.ToString() == "LeftArrow" || input.Key.ToString() == "RightArrow") { check = true; } break; } } while (check); return(input.Key.ToString()); }
//random point panit static void RandomPointPanit(Struct.DIEM point) { Console.SetCursorPosition(point.x, point.y); Console.Write("$"); }
//---Delete trailer snake static void DeleteTrailerSnake(Struct.DIEM point) { Console.SetCursorPosition(point.x, point.y); Console.Write(" "); }
//--Header snake static void HeaderSnake(Struct.DIEM point) { Console.SetCursorPosition(point.x, point.y); Console.Write("X"); }
//Check Collision // fragment snake static void FragmentSnake(Struct.DIEM point) { Console.SetCursorPosition(point.x, point.y); Console.Write("O"); }