C# For循环,该循环逐步将更多对象添加到列表()中

C# For循环,该循环逐步将更多对象添加到列表()中,c#,list,for-loop,monogame,C#,List,For Loop,Monogame,我很难找到一种方法来在for循环中创建一个等式,该等式将根据角色所在的级别在列表collectibleList中生成许多对象。现在,我的列表只为每个级别创建一个可收藏的。我猜这是因为I

我很难找到一种方法来在for循环中创建一个等式,该等式将根据角色所在的级别在列表
collectibleList
中生成许多对象。现在,我的列表只为每个级别创建一个
可收藏的
。我猜这是因为
I
的限制。但是我不知道我应该使用哪种绑定,或者如何在等式中实现
I
,以便根据
currentLevel
将更多
收藏品添加到我的列表中

    // Set up each level the player encounters
    public void NextLevel()
    {
        collectibleList.Clear();
        currentLevel++;
        timer = 10;
        player.LevelScore = 0;
        player.Position = new Rectangle(GraphicsDevice.Viewport.Width/2, GraphicsDevice.Viewport.Height/2, player.Position.Width, player.Position.Height);

        // Random number generator that will help generate a random position of the collectible sprite
        Random rng = new Random();
        for (int i = 0; i < currentLevel; i++)
        {
            Collectible collectible = new Collectible(rng.Next(0, GraphicsDevice.Viewport.Width), rng.Next(0, GraphicsDevice.Viewport.Height), 70, 91, true);
            collectible.ObjectSprite = collectibleSprite;
            collectibleList.Add(collectible);
        }
    }
//设置玩家遇到的每个关卡
公共无效下一级()
{
collectibleList.Clear();
currentLevel++;
定时器=10;
player.LevelScore=0;
player.Position=新矩形(graphicsdevelope.Viewport.Width/2、graphicsdevelope.Viewport.Height/2、player.Position.Width、player.Position.Height);
//随机数生成器,有助于生成可收集精灵的随机位置
随机rng=新随机();
对于(int i=0;i
由于您是基于currentLevel(一次一个)进行迭代,因此列表中每个级别都会有一个可收集的条目

您需要的是另一个因素来确定每个迭代要添加多少收藏品-然后您可以在主循环中放置内部循环,以生成每个级别所需的收藏品


i、 E2级收藏品,5级收藏品,等等。

你在寻找一个好的递增整数函数,对吗?有很多选择

f(n)=n
f(n)=2*n+1
f(n)=n+
⌊<代码>日志(n)
⌋, 等等

n越大,f(n)将越大


获取给定n的数字的另一种方法是将一个随机数添加到f(n-1)

你所说的“创建公式”是什么意思?每个级别需要多少个对象?计算所需的数字,并将其存储在变量中。然后在for循环中使用该变量而不是
currentLevel
。我同意@Blorgbeard的观点:问题/任务的定义很模糊,问题需要澄清。谢谢大家,@Blorgbeard我想我是在寻找一个依赖于
I
的等式,这样每个关卡都有更多的收藏品,这取决于关卡。比如
collectibleList.Add(collectible+2i)
或其他东西,但显然这不是有效的。我仍然无法理解你的问题。那么@Blorgbeard的方法呢?据我所知,根据等级的不同,目前每个等级都有更多的收藏品。每层一个。更多级别=更多收藏品。如果你想要一个不同的等式,用
i
i
或其他什么替换
i
。但是如果我想要
n
依赖于
currentLevel
,并且
f(n)
是添加到
currentList
的对象数,我该如何循环呢?@crin,你可以假设
currentLevel
与我回答中的
n
相同。要添加
f(currentLevel)
项,您只需更改(int i=0;i
我觉得很愚蠢,因为我不认为这对我来说应该很难理解,但是
f
是什么呢?@crin,它意味着n的函数。