Exemplo n.º 1
0
        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);
            }
        }