How do you set up a record table in the game?



  • I created a classic C# snake game with count levels, etc., but I wanted to make a record table and I couldn't make it, because they told me to do it on xml, but I never worked with him.

    How can that be realized? It is necessary to establish a name and account in five fields, and to replace a name and an account with a bigger account.



  • Here's my example. http://www.json.org/json-ru.html If you're fit, of course you're using. https://msdn.microsoft.com/ru-ru/library/system.web.script.serialization.javascriptserializer(v=vs.110).aspx It's asking! System.Web.Extensions !

    //Ваш класс записи рекорда
    public class MyRecord
    {
      public string Name { get; set;}
      public int Record { get; set;}
    } 
    

    How to write record sheets into text variables

    //Запись в текстовую переменную forWriteToFile 
    List<MyRecord> allRecords = new List<MyRecord>();
    ...
    //Тут заполнение allRecords 
    ...
    System.Web.Script.Serialization.JavaScriptSerializer serializer = 
      new System.Web.Script.Serialization.JavaScriptSerializer();
    string forWriteToFile = serializer.Serialize(allRecords );
    

    How to read the record sheets from the text variable.

    //Чтение из текстовой переменной forWriteToList
    System.Web.Script.Serialization.JavaScriptSerializer serializer = 
      new System.Web.Script.Serialization.JavaScriptSerializer();
    List<MyRecord> allRecords =  serializer.DeserializeObject<List<MyRecord>>(forWriteToList);
    

    UPD: Example of editing

    string JSON = Прочитать_Всё_Из_Файла("Рекорды.txt");//тут прочитали из файла в текстовую переменную JSON
    System.Web.Script.Serialization.JavaScriptSerializer serializer = 
      new System.Web.Script.Serialization.JavaScriptSerializer();
    List<MyRecord> allRecords = serializer.Deserialize<List<MyRecord>>(JSON);
    allRecords.RemoveAt(1);//Удалили вторую запись
    foreach(MyRecord record in allRecords)
      record.Record++;//Увеличили все рекорды на единицу 
    allRecords.Add(new MyRecord(){Name="Вася", Record=1000});//Добавили запись
    JSON = serializer.Serialize(allRecords); //Записали обратно в переменную JSON
    Записать_В_Файл("Рекорды.txt", JSON);//Далее напишите запись текста в файл
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2