c# - 在 C# 中创建已知长度的数组

标签 c# unity-game-engine

我目前正在学习使用 C# 在 Unity 中制作游戏的教程。在其中,他们向我们展示了如何使用

在屏幕中央显示分数
using UnityEngine;
using UnityEngine.UI;

public class Score : MonoBehaviour
{
    public Transform player;
    public Text scoreText;

    // Update is called once per frame
    void Update()
    {
        scoreText.text = player.position.z.ToString("0");
    }
}

但是,我想更进一步,弄清楚如何在左上角制作高分列表。我唯一的经验是使用 python,所以我知道我会创建一个大小为 10 的空数组,然后每次游戏重新启动时,如果分数大于上述列表中任何最高的 10 分,则将其插入相应的位置(保持元素有序),并且从列表中删除最低值(以保持仅包含 10 个元素的列表)。但是,我对 C# 的语法感到困惑。

目前,我对代码的思考过程(将在我的重启语句中进行),如果是 python 的话就是这样的

##this is how I would write it in python I believe
array = np.zeros[10]
for i in range(0, len(array)):
    if player.position.z.ToString("0") < array[-i]
          continue
    else:
          array[-i-1] = player.position.z.ToString("0")

显然,player.position 语句来自 C#。我想知道是否有人可以帮助我翻译这个思维过程。看来我需要声明一个字符串数组才能使用它,但我不确定。

感谢您的帮助

最佳答案

据我了解,您需要一个包含 10 个元素的数组,用于存储前 10 个分数。因此,每个高于现有前 10 名的新分数都会被放置在该前 10 名的正确位置。

类似的东西

current Top10 => 2, 3, 5, 7, 8, 9, 12, 15, 18, 20

newScore = 16

new Top10 => 3, 5, 7, 8, 9, 12, 15, 16, 18, 20

选项 1:

string[] scoreArray = new string[10];

//Initializing the score array
for (int i = 0; i < 10; i++)
{
    scoreArray[i] = 0; 
}

//Somewhere in your code a new score will be assigned to this variable
int newScore;

//Checking potentially higher score

boolean replaceFlag = false;
for (int i = 0; i < 10; i++)
{
    if(newScore > scoreArray[i]) 
    {
        replaceFlag = true;
    }else{
        //newScore lower than lowest stored score
        if(i == 0)
        {
            //Stop the loop
            i = 11;
        }else if(replaceFlag){

            //The rest of the elements in the array move one position back
            for(int j = 0 ; j < i-1; j++ )
            {
                scoreArray[j] = scoreArray[j+1];
            }
            //Finally we insert the new score in the correct place
            scoreArray[i-1] = newScore;         
        }
    }
}

选项 2:使用列表

//Create and initialize list of scores
List<int> listScores = new List<int>{ 0,0,0,0,0,0,0,0,0,0};



// If later, at some point we have the following scores 2, 3, 5, 7, 8, 9, 12, 15, 18, 20

//When you get a new score (our list will have)
listScores.Add(newScore);

//After inserting 2, 3, 5, 7, 8, 9, 12, 15, 18, 20, 16

//Ordering the list in descending order
listScores.Sort()
//Now we have 2, 3, 5, 7, 8, 9, 12, 15, 16, 18, 20,


//Drop last element of the list to keep the fixed size of 10.
listScores.RemoveAt(0)
//Now we have 3, 5, 7, 8, 9, 12, 15, 16, 18, 20

//In case you want to have the list in descending order
listScores.Sort((a, b) => -1* a.CompareTo(b));

关于c# - 在 C# 中创建已知长度的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55015142/

相关文章:

c# - 自承载 WCF 服务 : How to access the object(s) implementing the service contract from the hosting application?

c# - 如何将 RoutePattern 添加到 Endpoint 对象

android - 如何链接 Ant 中的外部 jar?

c# 在发送键之间暂停

c# - 在 Azure 中设置 MySQL 后端以进行 Xamarin 离线同步

c# - PlayFab - 为纸牌游戏创建自定义验证服务器

c# - 仅在 Z 轴上旋转游戏对象 C#

C# vuforia.image.pixels 覆盖到 iOS CIImage 或 CGImage

c# - 继承窗体时找不到类型为 ""的构造函数

c# - 通过 Unity 启动时,Visual Studio Code .NET 框架无法工作