0
\$\begingroup\$

I'm trying to create random level platforms like this:

 public Level(int platfromsInLevel, Texture2D sprite, int screenWidth)
    {
        rnd = new Random();
        this.screenWidth = screenWidth;

        for (int i = 0; i < platfromsInLevel; i++)
        {
            if (i == 0) 
            {
                Platform platform = new Platform((new Vector2(0, 0)), (new Vector2(0, 0)), sprite);
                Platformlist.Add(platform);
            }
            else
            {
                int CurrentX = (int)Platformlist[i - 1].getPos().X + Platformlist[i - 1].getTex().Width;
                int CurrentY = (int)Platformlist[i - 1].getPos().Y + Platformlist[i - 1].getTex().Height;

                int xRand = rnd.Next(CurrentX + 100,(CurrentX + 100) + 150);
                int yRand = rnd.Next(CurrentY + 200, (CurrentY + 200) + 200);

                if (xRand <= screenWidth || xRand >= 0)
                {
                    Platform platform = new Platform((new Vector2(0, 0)), (new Vector2(xRand, yRand)), sprite);
                    Platformlist.Add(platform);
                }                          
            }
        }
    }

Can anyone help with the random logic? I only want them to appear on the screen that is visible on the X axis, but the y axis is infinite it can go as high as needed. The width of the screen is stored in screenWidth. I also don't think my random logic is taking into account the whole screen width, it only adds positive values. Finally I've tried to add some logic that says a platform can only be a certain height and distance away from another one whilst taking into account the previous platforms position, but its not really working.

Anyone got any ideas to tidy this up a little bit?

\$\endgroup\$
  • 1
    \$\begingroup\$ What do you mean with "not really working"? What behavior do you expect and what behavior do you see instead? \$\endgroup\$ – Philipp Nov 25 '13 at 19:34
  • \$\begingroup\$ Platforms don't stay in the bounds of the screen width and they go up in a negative pattern, always going further to the left. It would be good to define a min and a max value for the distance they can be away from each other on the x axis. Same for the y. But I dont know how to do that taking into account the current width/height and X,Y pos of the previous platform. \$\endgroup\$ – Lewis Nov 25 '13 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.