3 Replies - 24882 Views - Last Post: 18 March 2013 - 03:18 PM

#1 HexCypher  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 03-March 12

Randomly Generated Background

Posted 17 March 2013 - 01:11 PM

So I've added my main character and I've gotten the background set up, so now I want to add some code that will randomly generate a background.

For some reason I can't get the search function on these forums to work, so sorry if this has been answered before.

I don't even know where to begin because it just seems so overwhelming to me, so any point in any direction will be helpful.

Thanks guys!

P.S. For some reason the replies to my previous post aren't saving. I have no idea why, but I've been trying to thank people for helping me, but it wont stick.

Seems to me like broken forums.

Is This A Good Question/Topic? 0
  • +

Replies To: Randomly Generated Background

#2 LiberLogic969  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 34
  • View blog
  • Posts: 85
  • Joined: 03-September 12

Re: Randomly Generated Background

Posted 17 March 2013 - 02:59 PM

Assuming your background is just a Texture2D you could make a List<Texture2D> to hold all the possible background images you would like. You then use the Random class to get a random int that points to an index in the list.


//Fields 
List<Texture2D> backgroundTextures;
Random random;

//Load
public void LoadContent(ContentManager content)
{
   //Create the Random object
   random = new Random();

   // Assumes all your background textures are called "backgroundAsset" + a numeric value
   // in order to ease loading. EX: backgroundAsset6
   string bgAsset = "backgroundAsset";

   // The amount of textures you will load.
   int maxTextures = 10; 

   // Initialize the List object
   backgroundTextures = new List<Texture2D>(maxTextures);
   
   //This loop will allow us to load a texture into each element of the list
   for (int i = 0; i < maxTextures; i++)
   {
       // Load the texture to the list. 
       backgroundTextures[i] = content.Load<Texture2D>(bgAsset + i.ToString());
   }
}

// Method that will return a random texture from the list
public Texture2D GetRandomBackground()
{
   // Call random.Next which will return a random int value between
   // 0 and 9 in this case (Remember list indexing is Zero-Based)since 
   // we have 10 textures
   int index = random.Next(0, backgroundTextures.Count - 1);

   // Return the Texture2D that the index points to
   return backgroundTextures[index]; 
}



This post has been edited by LiberLogic969: 17 March 2013 - 03:08 PM

Was This Post Helpful? 1
  • +
  • -

#3 HexCypher  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 03-March 12

Re: Randomly Generated Background

Posted 18 March 2013 - 02:37 PM

Awesome, thanks!

I was wondering today if there's a way that I could separate the background into sectors, say top left, top mid, top right etc. and then use your idea.

Randomly pick through a list of sectors which will compile a completely random map each load.

Sounds like a lot of work o.O
Was This Post Helpful? 0
  • +
  • -

#4 LarcaCode  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 21
  • Joined: 30-July 12

Re: Randomly Generated Background

Posted 18 March 2013 - 03:18 PM

Maybe this could also be interesting or useful for you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1