Advertisement
Guest User

Simple level loading

a guest
Oct 28th, 2011
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.70 KB | None | 0 0
  1. ///This method would load in data from a text file for level
  2. protected void LoadLevel()
  3. {
  4.     using (StreamReader sr = new StreamReader("Content\\Levels\\Level.txt"))
  5.     {
  6.         string line = sr.ReadLine();
  7.  
  8.         while (line != null)
  9.         {
  10.             tiles = line.Split(',');
  11.             foreach (string tile in tiles)
  12.             {
  13.                 //This would be a grass
  14.                 if (tile == "0")
  15.                 {
  16.                     //Create object here
  17.                     //ii and jj are the x,y position of our level in this case
  18.                     //Then add to list
  19.                     var obj = new object();
  20.                     obj.Position = new Vector2(ii, jj);
  21.                     obj.Texture = grassTexture;
  22.                     tileList.Add(obj);
  23.                 }
  24.                 if (tile == "1")
  25.                 {
  26.                     //Create object here
  27.                     //ii and jj are the x,y position of our level in this case
  28.                     //Then add to list
  29.                     var obj = new object();
  30.                     obj.Position = new Vector2(ii, jj);
  31.                     obj.Texture = dirtTexture;
  32.                     tileList.Add(obj);
  33.                 }
  34.                 ii += 50;
  35.             }
  36.             ii = 0;
  37.             jj += 50;
  38.         }
  39.     }
  40. }
  41.  
  42. ///You would draw the level like this
  43. foreach(obj o in tileList)
  44. {
  45.     spriteBatch.Draw(o.Texture, o.Position, Color.White);
  46. }
  47.  
  48. ///The text file would be in this format
  49. 0,0,1,0,0,0,0,0,0,0
  50. 0,0,1,0,0,0,0,0,0,0
  51. 0,0,1,0,0,0,0,0,0,0
  52. 0,0,1,0,0,0,0,0,0,0
  53. 0,0,1,1,1,1,1,1,1,1
  54. 0,0,0,0,0,0,0,0,0,0
  55. 0,0,0,0,0,0,0,0,0,0
  56. 0,0,0,0,0,0,0,0,0,0
  57. 0,0,0,0,0,0,0,0,0,0
  58. 0,0,0,0,0,0,0,0,0,0
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement