XNA,订购高分榜

时间:2013-11-01 19:38:38

标签: c# list xna xna-4.0

真的不知道怎么说这个问题,对于模糊的标题感到抱歉。 行。我已经为当前得分创建了两个整数,并为5个得分创建了位置。现在我想将当前得分放在正确位置的高分列表中,以便从低到高排序。

实施例。我得到了7分。现在我想把它放到记分牌中,首先尝试,我把它放在1号。但是在此之后我得到了8.现在我想将8移动到第一个位置,并且7朝向第二位置。有没有人知道如何做到这一点?

在此之前我唯一知道的是如何将当前得分放入高分列表/字符串中。我不知道如何订购它们。 这就是我之前所拥有的:

yourScore = "Your Time: " + Convert.ToString(currentTime * 60);
score1 = "1. " + Convert.ToString(currentTime * 60);

2 个答案:

答案 0 :(得分:3)

我会使用通用列表。

List<int> highScores = new List<int>();

highScores.Add(1);
highScores.Add(3);

highScores.OrderBy(i => i); // it is ascending. You could OrderByDescending...

(我以为你是在C#下)

答案 1 :(得分:2)

这与XNA无关。亚历山大建议,我建议使用List。他的评论极好地描述了List如何运作。您可能还想查看其文档。

List<int> highScores = new List<int>();

要添加新的高分,您可以执行以下操作:

highScores.Add(4523); // Someone just made a score of 4523.
highScores.Sort();    // This will sort the high scores, putting the lowest high score at position 0.
highScores.Reverse(); // This will reverse the list, putting the highest high score at position 0.

如果您想在屏幕上显示高分表,您可以这样做:

for(int i = 0; i < highScores.Count; i++)
{
   int order = i + 1;
   int score = highScores[i];
   screenPosition = new Vector2(0, i * 20);
   spriteBatch.DrawString(yourFont, order + ". " + score, screenPosition, Color.Black);
}

该代码将高分放在位置(0,0),位置(0,20)的第二好成绩等。