hi guys thanks for your reply
I speak French
sorry
I send you the code:
Game1:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;
using Android.Content.Res;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace processXml
{
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private SpriteFont _font;
//gestion scores
private long _score=0;
private long score2=0;
private long score3=0;
// private ScoreManager _scoreManager;
private float _timer;
private Random random;
public string recup;
public static string _fileName = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
private List<Score> HighScores { get; set; }
private List<Score> CurrentScore { get; set; }
private List<Score> Scores { get; set; }
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
random = new Random();
Scores = new List<Score>();
//load scores
/* highScores = new List<Score>
{
new Score("Dave:", _score),
};*/
//classer ordre decroissant
// HighScores = HighScores.OrderByDescending(c => c.Value).ToList();
//créer dossier si il n'est pas existant
/* if (!Directory.Exists(_fileName))
Directory.CreateDirectory(_fileName);*/
//use serializer XML
/* XmlSerializer xs = new XmlSerializer(typeof(List<Score>));
//Write in XML File
using(StreamWriter sw = new StreamWriter($"{_fileName}/test.xml"))
{
xs.Serialize(sw, HighScores);
}
//init highScores
// HighScores = null;
//Read file XML
using (StreamReader sr = new StreamReader($"{_fileName}/test.xml"))
{
HighScores = (List<Score>)xs.Deserialize(sr);
}*/
//takes the first 5elements
// HighScores = Scores.Take(5).ToList();
// HighScores = null;
//call read write xml
// WriteXml();
HighScores = null;
// ReadXml();
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
_font = Content.Load<SpriteFont>("Font");
_timer = 5;
/* HighScores = new List<Score>
{
new Score("player1:",_score),
new Score("player2",score2),
new Score("player3",score3),
};*/
ReadXml();
}
//read xml
public void ReadXml()
{
//use serializer XML
XmlSerializer xs = new XmlSerializer(typeof(List<Score>));
//Read file XML
using (StreamReader sr = new StreamReader($"{_fileName}/test.xml"))
{
HighScores = (List<Score>)xs.Deserialize(sr);
}
}
//write xml
public void WriteXml()
{
//use serializer XML
XmlSerializer xs = new XmlSerializer(typeof(List<Score>));
using (StreamWriter sw = new StreamWriter($"{_fileName}/test.xml"))
{
xs.Serialize(sw, HighScores);
}
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
_score = random.Next(0, 100); // Returns a random number from 0-99
score2 = random.Next(0, 100);
score3 = random.Next(0, 100);
_timer -= (float)gameTime.ElapsedGameTime.TotalSeconds;
// If the round is over
if (_timer <= 0)
{
HighScores = new List<Score>
{
new Score("Player:",_score),
new Score("Player2:",score2),
new Score("player3",score3),
};
HighScores = HighScores.OrderByDescending(c => c.Value).ToList();
_timer = 5;
// _score = 0;
WriteXml();
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
_spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.NonPremultiplied);
_spriteBatch.DrawString(_font, "Score: " + _score, new Vector2(10, 10), Color.Red);
_spriteBatch.DrawString(_font, "Time: " + _timer.ToString("N2"), new Vector2(10, 30), Color.Red);
_spriteBatch.DrawString(_font, "Highscores:\n" + string.Join("\n", HighScores.Select(c => c.PlayerName + ": " + c.Value).ToArray()), new Vector2(50, 50), Color.Red);
// _spriteBatch.DrawString(_font, "coucou" + recup, new Vector2(50, 50), Color.White);
_spriteBatch.End();
base.Draw(gameTime);
}
}
}
TestClass: for save score xml
using System;
namespace processXml
{
[Serializable]
public class TestClass
{
public string Name { get; set; }
public long Score { get; set; }
//constructor
public TestClass()
{
}
public TestClass(string name, long score)
{
Name = name;
Score = score;
}
}
}
thanks in advance