0

I am having trouble finding of a way to go about creating an equation inside my for loop that will generate a number of objects in a List collectibleList depending on the level that the character is on. As it is now, my list only creates one collectible per level. This, I'm guessing, is because of the i < currentLevel bound. But I don't know what kind of bound I should use or how to implement i in an equation so that more collectibles can be added to my list depending on the 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);
        }
    }
crin
  • 73
  • 1
  • 2
  • 13
  • 2
    What do you mean by "create a formula"? How many objects do you want per level? Calculate the number you want, store it in a variable. Then use that variable instead of `currentLevel` in the for loop. – Blorgbeard Mar 06 '16 at 19:32
  • I agree with @Blorgbeard : the issue/task is vaguely defined, and the question need clarification. Thanks and regards, – Alexander Bell Mar 06 '16 at 19:35
  • @Blorgbeard I guess I was looking for an equation that's dependent on `i` so that each level has more collectibles depending on the level. Like `collectibleList.Add(collectible + 2i)` or something but obviously that's not valid – crin Mar 06 '16 at 19:36
  • I still can't understand your question. What about @Blorgbeard's approach? – wingerse Mar 06 '16 at 19:40
  • As far as I can tell, each level *currently* has more collectibles depending on the level. It's one per level. More levels = more collectibles. If you want a different equation, replace `i < currentLevel` with `i < currentLevel * 4` or `i < Math.Pow(currentLevel, 2)` or whatever. – Blorgbeard Mar 06 '16 at 19:41
  • @Blorgbeard Ah, that makes sense. I guess I was over-complicating things. I just did `i < currentLevel * rng.Next(1, 50)` which is pretty shaky... since level 1 could have more collectible objects than level 3 depending on the rng. But I think it'll be ok. Thank you – crin Mar 06 '16 at 19:50
  • Calculate this number *before* the loop like @Blorgbeard suggested, not on each iteration. Also, @IvanGritsenko suggested that you *add* the random number to the previous level count, because that would ensure you would get an increasing number on each level. What you're doing with `rng.Next(1, 50)` will not ensure progressively harder levels. Perhaps you wanted something like `currentLevel * 20 + rng.Next(-5, 5)`? That way the random number would only add slight variation. – vgru Mar 07 '16 at 08:16

2 Answers2

0

Since you are iterating based on currentLevel (one at a time) you will get single collectible entry per level in the list.

What you need is another factor to determine how many collectibles to add per iteration - then you can place inner loop within the main loop to generate as many collectibles as you want per level.

i.e 2 collectibles for level 1, 5 collectibles for level 2 etc.

0

You are looking for a good increasing integer function, right? There are pretty many options to choose from.

f(n) = n, f(n) = 2*n + 1, f(n) = n +log(n)⌋, etc.

The bigger n is the bigger f(n) will be.

Another way to get the number for a given n is just to add a random number to f(n - 1).

Ivan Gritsenko
  • 4,166
  • 2
  • 20
  • 34
  • But if I wanted `n` to be dependent on the `currentLevel` and `f(n)` to be the number of objects added to `currentList`, how would I make a loop of that? – crin Mar 06 '16 at 19:56
  • @crin, You may assume that `currentLevel` is the same as `n` in my answer. To add `f(currentLevel)` items you just change the loop `for (int i = 0; i < f(currentLevel); i++)` – Ivan Gritsenko Mar 06 '16 at 20:00
  • I feel stupid because I don't think this should be that hard for me to understand, but what is `f`, then? – crin Mar 06 '16 at 20:23
  • @crin, it mean **function** of n. – Ivan Gritsenko Mar 06 '16 at 20:24