在C#

时间:2019-03-06 03:38:06

标签: c# unity3d

我目前正在关注有关使用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#。我想知道是否有人可以帮助我转变思考过程。看来我需要先声明一个字符串数组,然后才能使用它,但我不确定。

感谢您的帮助

4 个答案:

答案 0 :(得分:1)

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

string[] aStringArray = new string[10];

也将此添加到顶部;

using System.Collections.Generic;

Arrays - C# Programming Guide

答案 1 :(得分: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));

答案 2 :(得分:1)

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

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

答案 3 :(得分:0)

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

List<int> player = new List<int>();
player.Add(1);