我正在努力改进我从本教程http://xnatd.blogspot.com/完成的塔防gtame我现在希望从文本文件加载级别但不太确定我将如何使用streamreader,任何提示?下面是我的关卡类的源代码;
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);
}
}
}
}
}
}
答案 0 :(得分:3)
由于C#是编译的,不能像脚本语言那样做(不是你应该这样做),你应该使用streamreader来读取文件中的数据(格式化可能是分隔文本:csv或tsv)< / p>
假设您正在加载类似于您所拥有的地图的内容,那么地图文件可能看起来像
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
然后您将在其中循环浏览文件并读取每一行 读入每一行时,您可以将行拆分为“,”以创建一个1d数组,然后可以将该数组分配给2d数组中的索引。为每一行循环以创建整个2d地图数组
如果您需要帮助在C#中拆分字符串,请查看this;请注意,示例代码在空格“”处拆分,并且您应该使用s.Split(',');对于csv
答案 1 :(得分:0)
使用我将使用的小文件:
File.ReadAllLines,在读完之后,它会循环使用foreach循环。