Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6905863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:14:10+00:00 2026-05-27T08:14:10+00:00

i am working on improving this tower defence gtame that i finished from this

  • 0

i am working on improving this tower defence gtame that i finished from this tutorial http://xnatd.blogspot.com/ i now wish to load the levels from a text file but not quite sure how i would do this using a streamreader, any tips? heres the source code for my level class;

public class Level
{
    protected int temperature;
    protected int levelNo;
    private Queue<Vector2> waypoints = new Queue<Vector2>();
    public Queue<Vector2> Waypoints
    {
        get
        {
            return waypoints;
        }
    }

    int[,] map = new int[,]                    
    {



    {0,0,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,},
    {0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,},
    {0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,},
    {0,0,0,0,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,},
    {0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,},
    {0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,},
    {0,0,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,},
    {0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,},
    {0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,},
    {0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,},
    {0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,},
    {0,0,0,0,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,},
    {0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,},
    {0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,},
    {0,0,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,},
    {0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,},

    };
    public int Temperature                                                                                    // get the width of the level (how many numbers are in a row)
    {
        get
        {
            return temperature;
        }
    }
    public int LevelNo                                                                                    // get the width of the level (how many numbers are in a row)
    {
        get
        {
            return levelNo;
        }
          set
        {
            levelNo = value;
        }
    }
    public int Width                                                                                    // get the width of the level (how many numbers are in a row)
    {
        get
        {
            return map.GetLength(1);
        }
    }
    public int Height                                                                                   // get the height of our level (how many numbers are there in a column)
    {
        get
        {
            return map.GetLength(0);
        }
    }
    public int GetIndex(int cellX, int cellY)                                                           // return the index of the requested cell.
    {
        if (cellX < 0 || cellX > Width - 1 || cellY < 0 || cellY > Height - 1)
        {
            return 0;
        }
        else
        {
            return map[cellY, cellX];
        }
    }
    public List<Texture2D> tileTextures = new List<Texture2D>();                                        // list to contain all the textures
    /// <summary>
    /// CONSTRUCTOR NEW LEVEL        
    /// </summary>
    public Level()
    {
        SetWayPoints(map);
        this.temperature = 1000;
        this.levelNo = 2;
    }
    private void SetWayPoints(int[,] map)
    {
        int currentPosVal = map[0, 0];
        int lPos = 0;
        int rPos = 0;
        int uPos = 0;
        int dPos = 0;
        int storedXPos = 99;
        int storedYPos = 99;
        int endstoredXPos = 99;
        int endstoredYPos = 99;
        int lastY = 0;
        int lastX = 0;
        //Search top ROW for start
        for (int i = 0; i < Width; i++)
        {
            currentPosVal = map[0, i];
            if (currentPosVal == 1)
            {
                storedXPos = i;
                storedYPos = 0;
                waypoints.Enqueue(new Vector2(storedXPos, storedYPos) * 32);
                lastX = storedXPos;
                lastY = storedYPos;
                break;
            }
        }
        //if start not set
        if (storedXPos == 99 && storedXPos == 99)
        {
            //look in 1st coloum for start
            for (int i = 0; i < Height; i++)
            {
                currentPosVal = map[i, 0];
                if (currentPosVal == 1)
                {
                    storedXPos = 0;
                    storedYPos = i;
                    waypoints.Enqueue(new Vector2(storedXPos, storedYPos) * 32);
                    lastX = storedXPos;
                    lastY = storedYPos;
                    break;
                }
            }
        }
        //search end COLOUM for end
        for (int i = 0; i < Height; i++)
        {
            currentPosVal = map[i, Width - 1];
            if (currentPosVal == 1)
            {
                endstoredXPos = Width - 1;
                endstoredYPos = i;
            }
        }
        //If end not set look in bottom row for end
        if (endstoredXPos == 99 && endstoredYPos == 99)
        {
            for (int i = 0; i < Width; i++)
            {
                currentPosVal = map[7, i];
                if (currentPosVal == 1)
                {
                    endstoredXPos = i;
                    endstoredYPos = Height - 1;
                }
            }
            if (endstoredXPos == 99 && endstoredYPos == 99)
            {

            }
        }
        // start midlle loop
        while (true)
        {
            lPos = 0;
            rPos = 0;
            uPos = 0;
            dPos = 0;

            //If current pos is not down the left hand edge
            if (storedXPos > 0) { lPos = map[storedYPos, storedXPos - 1]; }

            //If current pos square is not down the right hand edge
            if (storedXPos < Width - 1) { rPos = map[storedYPos, storedXPos + 1]; }

            //If current pos square is not in the top row
            if (storedYPos > 0) { uPos = map[storedYPos - 1, storedXPos]; }

            //If current pos square is not in the bottom row
            if (storedYPos < Height - 1) { dPos = map[storedYPos + 1, storedXPos]; }

            if (lPos == 1 && (lastX != storedXPos - 1 || lastY != storedYPos))
            {
                lastX = storedXPos;
                lastY = storedYPos;
                storedXPos--;
                waypoints.Enqueue(new Vector2(storedXPos, storedYPos) * 32);

            }
            else if (rPos == 1 && (lastX != storedXPos + 1 || lastY != storedYPos))
            {
                lastX = storedXPos;
                lastY = storedYPos;
                storedXPos++;
                waypoints.Enqueue(new Vector2(storedXPos, storedYPos) * 32);
            }
            else if (dPos == 1 && (lastX != storedXPos || lastY != storedYPos + 1))
            {
                lastX = storedXPos;
                lastY = storedYPos;
                storedYPos++;
                waypoints.Enqueue(new Vector2(storedXPos, storedYPos) * 32);
            }
            else if (uPos == 1 && (lastX != storedXPos || lastY != storedYPos - 1))
            {
                lastX = storedXPos;
                lastY = storedYPos;
                storedYPos--;
                waypoints.Enqueue(new Vector2(storedXPos, storedYPos) * 32);

            }
            if (storedXPos == endstoredXPos && storedYPos == endstoredYPos)
            {
                break;
            }
        }
    }
    public void AddTexture(Texture2D texture)                                                           // method adds a texture to our texture list. 
    {
        tileTextures.Add(texture);
    }
    //Reads number from array, store its value in textureIndex, Use textureIndex to get the texture from tileTextures, 
    public void Draw(SpriteBatch batch)                                                                     //Draw appropiate texture, Repeat through all elements of the array
    {
        int textureIndex;
        Texture2D texture;
        for (int x = 0; x < Width; x++)
        {
            for (int y = 0; y < Height; y++)
            {
                if (levelNo == 0)
                {
                    textureIndex = map[y, x];
                    if (textureIndex == -1)
                        continue;
                    texture = tileTextures[textureIndex];
                    batch.Draw(texture, new Rectangle(x * 32, y * 32, 32, 32), Color.White);
                }
                if (levelNo > 0)
                {
                    textureIndex = map[y, x];

                    textureIndex += (levelNo * 2);
                   if (textureIndex == -1)
                    continue;
                texture = tileTextures[textureIndex];
                batch.Draw(texture, new Rectangle(x * 32, y * 32, 32, 32), Color.White);
                }
            }
        }
    }
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:14:10+00:00Added an answer on May 27, 2026 at 8:14 am

    Since C# is compiled and can’t do evals like scripted languages can (not that you should be doing that anyway) you should probably use streamreader to read the data from file (formatted perhaps as delimited text: csv or tsv)

    Assuming you’re loading something similar to the map you have up there then a map file could look something like

    0,0,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0
    0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0
    0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0
    0,0,0,0,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0
    0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0
    0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0
    0,0,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0
    0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0
    0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0
    0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0
    0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0
    0,0,0,0,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0
    0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0
    0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0
    0,0,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0
    0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1
    

    where you would then loop through the file and read each line
    As each line is read in, you can then split the line by “,” to create a 1d array which can then be assigned to an index in the 2d array. Loop for each line to create the entire 2d map array

    Take a look at this if you need help splitting strings in C#; note that the sample code splits at spaces ” ” and that you should use s.Split(‘,’); for csv

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working from this example: http://jqueryui.com/demos/tabs/ and trying to get the tabs to
I'm working on ASP.net MVC3 Web application that is facing scalability issue. For improving
We are working on improving our DAL which is written in LINQ that talks
I'm currently working on some R&D for improving the process and practice that we
I am working on improving our glossary functionality in a custom CMS that is
It's been a couple of days that I'm working on improving NHibernate Insert performance.
I am working on improving a stream reader class that uses a BinaryReader .
I am pretty new to C++11 and am now working on improving my C++
I'm working on improving SEO on an old website that a terrible developer coded.
I am working on improving the performance of my app. I am confused about

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.