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

问题描述 投票:2回答:4

我目前正在学习使用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#。我想知道是否有人可以帮我翻译思考过程。在我可以使用之前,我似乎需要声明一个字符串数组,但我不确定。

谢谢你的帮助

c# unity3d
4个回答
1
投票

据我所知,你想要一个包含10个元素的数组,你可以存储前10个分数。因此,每个高于现有前10名的新分数都被放置在前10名的正确位置。

就像是

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

newScore = 16

新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));

1
投票

如果Score是数组,则将分数添加到此Score数组变量中,然后在您想要更新高分时对该数组进行排序和反转。

private void UpdateScore(int[] ScoreArray)
{
   Array.Sort(ScoreArray);
   Array.Reverse(ScoreArray);
} 

1
投票

您将创建一个已知的数组大小为10,如下所示:

string[] aStringArray = new string[10];

还要在顶部添加;

using System.Collections.Generic;

Arrays - C# Programming Guide


0
投票

数组必须有长度,如果你想要未知的大小,那么使用List

List<int> player = new List<int>();
player.Add(1);
© www.soinside.com 2019 - 2024. All rights reserved.