public static void Func() { while (k == 0) { if ((food.location.x == snake.body[0].x && food.location.y == snake.body[0].y)) { wall.Level(wall, snake); if (snake.body.Count > 1) { snake.body.Add(new Point(snake.body[snake.body.Count - 1].x, snake.body[snake.body.Count - 1].y)); } else { snake.body.Add(new Point(5, 5)); } food.SetRandomPosition(wall, snake); food.Draw(); } if (direction == 1) { snake.Move(0, 1); } if (direction == 2) { snake.Move(0, -1); } if (direction == 3) { snake.Move(1, 0); } if (direction == 4) { snake.Move(-1, 0); } if (snake.CollisionWithWall(snake, wall)) { snake.RecordSave(username, snake.body.Count); k = 1; } food.Draw(); snake.Draw(); wall.wallpoint(); wall.Draw(); Thread.Sleep(n); } }
public static void PlayGame() { while (true) { if (direction == 1) { snake.Move(-1, 0); } if (direction == 2) { snake.Move(1, 0); } if (direction == 3) { snake.Move(0, -1); } if (direction == 4) { snake.Move(0, 1); } Console.SetCursorPosition(0, 0); Console.WriteLine("High score : " + " " + points + " Score: " + score + " " + "Level" + " " + level); Console.SetCursorPosition(0, 1); Console.WriteLine("If you want to save current score, enter [Probel]"); Console.SetCursorPosition(0, 2); Console.WriteLine("If you want to quit, enter [Escape] "); if (snake.CollisionWithWall(wall) || snake.Collision()) { Console.Clear(); Console.SetCursorPosition(7, 7); StreamReader sr = new StreamReader(@"C:\Users\Compag\Desktop\PP-2\week5\SnakeGame\Snake\gameover.txt"); string s = sr.ReadToEnd(); Console.WriteLine(s); Console.WriteLine("High Score : " + " " + points + "Your Score is " + score); Console.ReadKey(); Console.Clear(); speed = 400; snake = new Snake(); level = 1; wall = new Wall(level); score = 0; while (!food.foodonwall(wall) || !food.foononsnake(snake)) { food.SetRandomPos(); Console.SetCursorPosition(food.x, food.y); Console.Write("$"); } } if (snake.Eat(food)) { snake.AddBody(); food.SetRandomPos(); score += 10; points = Math.Max(points, score); Console.SetCursorPosition(food.x, food.y); Console.Write("$"); while (!food.foodonwall(wall) || !food.foononsnake(snake)) { food.SetRandomPos(); Console.SetCursorPosition(food.x, food.y); Console.Write("$"); } } if (score == level * 50) { Console.Clear(); level++; wall = new Wall(level); speed = Math.Max(1, speed - 100); while (!food.foodonwall(wall) || !food.foononsnake(snake)) { food.SetRandomPos(); Console.SetCursorPosition(food.x, food.y); Console.Write("$"); } } // Console.Clear(); snake.Draw(); food.ShowFood(); wall.WallDraw(); Thread.Sleep(speed); } }
static void Main(string[] args) { Console.Clear(); Console.SetCursorPosition(35, 7); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("Welcome To Snake Game"); Console.SetCursorPosition(20, 8); Console.WriteLine("Reach 110 score to complete level and complete 10 levels to finish game"); Console.SetCursorPosition(25, 9); Console.WriteLine("Please, Enter Your Name"); Console.SetCursorPosition(30, 10); string line = Console.ReadLine();//user's name Console.CursorVisible = false; Console.Clear(); snake.Head(); snake.Draw(); food.Draw(); wall.Draw(); s.Draw(score, level); Thread thread = new Thread(F); thread.Start(); while (true) { Console.ForegroundColor = ConsoleColor.Black; ConsoleKeyInfo key = Console.ReadKey(); if (key.Key == ConsoleKey.DownArrow && last != 2 && last != 1) { snake.Move(0, 1); last = 1; success = 1; } if (key.Key == ConsoleKey.UpArrow && last != 1 && last != 2) { snake.Move(0, -1); last = 2; success = 1; } if (key.Key == ConsoleKey.LeftArrow && last != 4 && last != 3) { snake.Move(-1, 0); last = 3; success = 1; } if (key.Key == ConsoleKey.RightArrow && last != 3 && last != 4) { snake.Move(1, 0); last = 4; success = 1; }//head's move if (key.Key == ConsoleKey.F2) { snake.Ser(); food.Ser(); s.Ser(score); s.Ser1(level); wall.Ser(); } if (key.Key == ConsoleKey.F3) { snake = snake.Des(); wall = wall.Des(); score = int.Parse(s.Des()); level = int.Parse(s.Des1()); food = food.Des(); Console.Clear(); food.Draw(); wall.Draw(); s.Draw(score, level); success = 1; Console.ReadKey(); } if (success == 1) { snake.Draw(); food.Draw(); if (snake.body.Count < 2) { wall.Draw(); } } success = 0; if (snake.CollisionWithFood(food)) { score = score + 10; // every food = 10 point if (score >= 110) { level++; if (level > 10) { Console.SetCursorPosition(30, 5); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("Congratulations!!!"); Console.SetCursorPosition(30, 6); Console.WriteLine("You completed My Game"); Console.ReadKey(); return; } speed = 300; score = 0; last = 0; Console.Clear(); wall = new Wall(level); snake = new Snake(); snake.Head(); food = new Food(wall, snake); snake.Draw(); s.Draw(score, level);// if level up } food = new Food(wall, snake); food.Draw(); s.Draw(score, level);// score , level, and food point refresh } if (snake.CollisionWithBody() || snake.CollisionWithWall(wall)) { thread.Abort(); string ss = ""; Console.ForegroundColor = ConsoleColor.DarkYellow; Console.Clear(); Console.SetCursorPosition(30, 7); Console.WriteLine("GAME OVER"); Console.SetCursorPosition(28, 8); Console.WriteLine("Your score is " + ((level - 1) * 110 + score)); Console.SetCursorPosition(27, 9); Console.WriteLine("press R to restrart or Q to quit"); int maxscore = 0; if (!File.Exists(line + ".txt")) { File.WriteAllText(line + ".txt", "0"); } ss = File.ReadAllText(line + ".txt"); Console.SetCursorPosition(28, 10); Console.WriteLine("Your Best score is " + ss); maxscore = int.Parse(ss); if (maxscore < score) { File.WriteAllText(line + ".txt", score.ToString()); }//Highscore score = 0; level = 1;//if Game Over speed = 300; while (true) { Console.ForegroundColor = ConsoleColor.Black; ConsoleKeyInfo kki = Console.ReadKey(); if (kki.Key == ConsoleKey.R) { thread = new Thread(F); thread.Start(); break; } if (kki.Key == ConsoleKey.Q) { return; }//Quit or Restart } wall = new Wall(level); snake = new Snake(); snake.Head(); food = new Food(wall, snake); Console.Clear(); s.Draw(score, level);//start game from the beginning snake.Draw(); wall.Draw(); food.Draw(); } Console.ForegroundColor = ConsoleColor.Black; } }
static void game(object all1) { int a = 0, b = 40, c = 18, da = 1, lll = 3; int g = snake.Random1(a, b); int g1 = snake.Random2(a, c); while (true ) { if (direction == 3) { snake.Move(0, -1); } if (direction == 1) { snake.Move(-1, 0); } if (direction == 2) { snake.Move(1, 0); } if (direction == 4) { snake.Move(0, 1); } if (snake.CollisionWithWall(wall) || snake.Collision()) { Console.Clear(); Console.SetCursorPosition(5, 5); Console.WriteLine("GAME OVER!!!!"); Console.ReadKey(); snake = new Snake(); level = 1; wall = new Wall(level); } Console.Clear(); if (da % lll == 0) { lll *= 2; level++; wall = new Wall(level); } if (snake.CollisionWithFood(g, g1)) { da++; g = snake.Random1(a, b); g1 = snake.Random2(a, c); } if (wall.FoodInWall(g, g1) == true && snake.FoodInMe(g, g1) == true ) { snake.DrawFood(g, g1); } else { g = snake.Random1(a, b); g1 = snake.Random2(a, c); snake.DrawFood(g, g1); } snake.Draw(direction); wall.Draw(); Thread.Sleep(100); } }
public static void Main(string[] args) { string username = Console.ReadLine(); StreamWriter save = new StreamWriter(@"/Users/macbook/Documents/PP2/Savage.txt"); int level = 1; int nextlevel = 0; int score = 1; Snake snake = new Snake(); Wall wall = new Wall(level); Food food = new Food(); Console.BackgroundColor = ConsoleColor.Black; Console.CursorVisible = false; while (true) { ConsoleKeyInfo keyInfo = Console.ReadKey(); if (keyInfo.Key == ConsoleKey.UpArrow) { snake.Move(0, -1); } if (keyInfo.Key == ConsoleKey.DownArrow) { snake.Move(0, 1); } if (keyInfo.Key == ConsoleKey.RightArrow) { snake.Move(1, 0); } if (keyInfo.Key == ConsoleKey.LeftArrow) { snake.Move(-1, 0); } if (snake.CollisionWithWall(wall)) { Console.Clear(); Console.SetCursorPosition(10, 10); Console.WriteLine("GAME OVER!!!!"); Console.ReadKey(); snake = new Snake(); wall = new Wall(level); } if (snake.CollisionWithFood(food)) { food.RandomFood(); nextlevel++; } if (nextlevel == 3) { score++; level++; wall = new Wall(level); nextlevel = 0; snake = new Snake(); food = new Food(); } if (snake.CollisionWithWall(wall)) { save.WriteLine(username + " " + score * nextlevel); save.Close(); } Console.Clear(); snake.Draw(); wall.Draw(); food.Setup(); //Console.WriteLine(score*nextlevel); } }