static void main(string [] args) { int level = 1; Snake snake = new Snake(); Wall wall = new wall; while (true) { ConsoleKeyInfo keyinfo = Console.ReadKey(); if (keyinfo.Key == ConsoleKey.DownArrow) { snake.move(0, 1); } if (keyinfo.Key == ConsoleKey.UpArrow) { snake.move(0, -1); } if (keyinfo.Key == ConsoleKey.RightArrow) { snake.move(1, 0); } if (keyinfo.Key == ConsoleKey.LeftArrow) { snake.move(-1, 0); } if (keyinfo.Key = ConsoleKey.R) { level = 1; snake = new Snake; wall = new Wall(level); } 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); } if (snake.cnt % 400 == 0) { level++; wall = new Wall(level); } Console.Clear(); snake.draw(); wall.draw(); } }
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); } }
public static void Thread_func() { while (true) { if (direction == 1) { snake.Motion(1, 0); } if (direction == 2) { snake.Motion(-1, 0); } if (direction == 3) { snake.Motion(0, 1); } if (direction == 4) { snake.Motion(0, -1); } if (a.Key == ConsoleKey.R) { level = 1; snake = new Snake(); wall = new Wall(level); eat = new food(); while (!eat.testsnake(snake) || (!eat.testwall(wall))) { n = rnd.Next(2, 75); m = rnd.Next(2, 24); eat = new food(n, m); } } if (a.Key == ConsoleKey.S) { FileStream fs = new FileStream("data.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite); FileStream fs_1 = new FileStream("data1.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite); List <int> list = new List <int>(); list.Add(level); list.Add(n); list.Add(m); XmlSerializer xml = new XmlSerializer(typeof(Snake)); XmlSerializer xml_1 = new XmlSerializer(typeof(List <int>)); xml.Serialize(fs, snake); xml_1.Serialize(fs_1, list); fs.Close(); fs_1.Close(); } if (a.Key == ConsoleKey.E) { FileStream fs = new FileStream("data.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite); FileStream fs_1 = new FileStream("data1.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite); XmlSerializer xml = new XmlSerializer(typeof(Snake)); XmlSerializer xml_1 = new XmlSerializer(typeof(List <int>)); snake = xml.Deserialize(fs) as Snake; List <int> list = xml_1.Deserialize(fs_1) as List <int>; fs.Close(); fs_1.Close(); level = list[0]; n = list[1]; m = list[2]; wall = new Wall(level); eat = new food(n, m); } if (snake.CollisionWithFood(eat)) { snake.AddMember(); eat = new food(); while (!eat.testsnake(snake) || (!eat.testwall(wall))) { n = rnd.Next(2, 75); m = rnd.Next(2, 24); eat = new food(n, m); } } if (snake.CollisionWithWall(wall) || snake.Collision()) { Console.Clear(); Console.SetCursorPosition(5, 5); Console.WriteLine("GAME OVER!!!!"); Console.ReadKey(); snake = new Snake(); level = 1; eat = new food(); while (!eat.testsnake(snake) || (!eat.testwall(wall))) { n = rnd.Next(2, 75); m = rnd.Next(2, 24); eat = new food(n, m); } wall = new Wall(level); } if (snake.UpLevel(level)) { direction = 1; level++; snake = new Snake(); wall = new Wall(level); eat = new food(); while (!eat.testsnake(snake) || (!eat.testwall(wall))) { n = rnd.Next(2, 75); m = rnd.Next(2, 24); eat = new food(n, m); } } Console.Clear(); wall.Draw(); snake.Draw(); eat.Showfood(n, m); Console.SetCursorPosition(0, 27); Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("Your Level: " + level + " You must dial " + level * 5); Console.SetCursorPosition(0, 28); Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("Score: " + (snake.a.Count - 1)); if (snake.UpLevel(level)) { speed = Math.Max(100, speed - 50); } Thread.Sleep(speed); } }