Esempio n. 1
0
 /*public static void save(Snake snake, Food food)
 {
     XmlSerializer xs1 = new XmlSerializer(typeof(Snake));
     FileStream fs1 = new FileStream("snake.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
     try
     {
         xs1.Serialize(fs1, snake);
         fs1.Close();
     }
     catch (Exception e) { }
     finally
     {
         fs1.Close();
     }
     XmlSerializer xs2 = new XmlSerializer(typeof(Food));
     FileStream fs2 = new FileStream("food.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
     try
     {
         xs2.Serialize(fs2, food);
         fs2.Close();
     }
     catch (Exception e) { }
     finally
     {
         fs2.Close();
     }
 }
 public static void load(Snake snake, Food food)
 {
     if (!File.Exists("snake.txt") || !File.Exists("food.txt")) return;
     XmlSerializer xs1 = new XmlSerializer(typeof(Snake));
     FileStream fs1 = new FileStream("snake.txt", FileMode.Open, FileAccess.Read);
     try
     {
         snake = xs1.Deserialize(fs1) as Snake;
         fs1.Close();
     }
     catch (Exception e) { }
     finally
     {
         fs1.Close();
     }
     XmlSerializer xs2 = new XmlSerializer(typeof(Food));
     FileStream fs2 = new FileStream("food.txt", FileMode.Open, FileAccess.Read);
     try
     {
         food = xs2.Deserialize(fs2) as Food;
         fs2.Close();
     }
     catch (Exception e) { }
     finally
     {
         fs2.Close();
     }
     Console.Clear();
     snake.Draw();
     food.Draw();
 }*/
 static void Main(string[] args)
 {
     Console.SetWindowSize(50, 50);
     Snake mysnake = new Snake();
     mysnake.Initialize();
     Food myfood = new Food(mysnake.body);
     ConsoleKeyInfo pressedKey;
     while (true)
     {
         myfood.Draw();
         pressedKey = Console.ReadKey();
         switch (pressedKey.Key)
         {
             case ConsoleKey.UpArrow:
                 if (mysnake.Move(0, -1, myfood.location))
                 {
                     myfood = new Food(mysnake.body);
                 }
                 break;
             case ConsoleKey.RightArrow:
                 if (mysnake.Move(1, 0, myfood.location))
                 {
                     myfood = new Food(mysnake.body);
                 }
                 break;
             case ConsoleKey.DownArrow:
                 if (mysnake.Move(0, 1, myfood.location))
                 {
                     myfood = new Food(mysnake.body);
                 }
                 break;
             case ConsoleKey.LeftArrow:
                 if (mysnake.Move(-1, 0, myfood.location))
                 {
                     myfood = new Food(mysnake.body);
                 }
                 break;
             case ConsoleKey.F1:         //new game
                 Console.Clear();
                 mysnake = new Snake();
                 mysnake.Initialize();
                 myfood = new Food(mysnake.body);
                 break;
             case ConsoleKey.F2:         //save (serialize)
                 if (File.Exists("snake.txt")) File.Delete("snake.txt");
                 if (File.Exists("food.txt")) File.Delete("food.txt");
                 XmlSerializer xs1 = new XmlSerializer(typeof(Snake));
                 FileStream fs1 = new FileStream("snake.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
                 try
                 {
                     xs1.Serialize(fs1, mysnake);
                     fs1.Close();
                 }
                 catch (Exception e) { }
                 finally
                 {
                     fs1.Close();
                 }
                 xs1 = new XmlSerializer(typeof(Food));
                 fs1 = new FileStream("food.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
                 try
                 {
                     xs1.Serialize(fs1, myfood);
                     fs1.Close();
                 }
                 catch (Exception e) { }
                 finally
                 {
                     fs1.Close();
                 }
                 break;
             case ConsoleKey.F3:         //load (deserialize)
                 if (File.Exists("snake.txt") && File.Exists("food.txt"))
                 {
                     XmlSerializer xs2 = new XmlSerializer(typeof(Snake));
                     FileStream fs2 = new FileStream("snake.txt", FileMode.Open, FileAccess.Read);
                     try
                     {
                         mysnake.Clear();
                         mysnake = xs2.Deserialize(fs2) as Snake;
                         fs2.Close();
                     }
                     catch (Exception e) { }
                     finally
                     {
                         fs2.Close();
                     }
                     xs2 = new XmlSerializer(typeof(Food));
                     fs2 = new FileStream("food.txt", FileMode.Open, FileAccess.Read);
                     try
                     {
                         myfood.Clear();
                         myfood = xs2.Deserialize(fs2) as Food;
                         fs2.Close();
                     }
                     catch (Exception e) { }
                     finally
                     {
                         fs2.Close();
                     }
                     mysnake.Draw();
                     myfood.Draw();
                 }
                 break;
             case ConsoleKey.Escape:
                 return;
             default:
                 break;
         }
     }
 }