初学者RPG地图系统

时间:2018-10-14 10:42:06

标签: c# arrays multidimensional-array ascii console-application

不幸的是,我在我制作的RPG上遇到了障碍。

我已经制作了要显示给玩家的地图(以.txt文件格式),但是我希望能够在游戏过程中更改地图并使用坐标来实现碰撞检测和门户等功能。为此,我需要将地图存储在二维数组中。问题是我从未使用过二维数组(我仍然是初学者),所以我不知道如何使用它们。

我的问题是:如何使程序一次读取一个.txt文件的字符,以及如何将这些字符存储在二维数组中? (数组的大小为32 x 128)

您可以在这里找到.txt文件:map1.txt

2 个答案:

答案 0 :(得分:1)

尝试以下代码

sing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.txt";
        static void Main(string[] args)
        {
            StreamReader reader = new StreamReader(FILENAME);
            string line = "";
            List<string> data = new List<string>();
            while((line = reader.ReadLine()) != null)
            {
                data.Add(line);
            }

            //read line 5 character 6
            char c5_6 = data[4][5];
        }
    }
}

答案 1 :(得分:0)

一次读取一个字符:

using (StreamReader sr = new StreamReader(path)) 
{
    while (sr.Peek() >= 0) 
    {
        Console.Write((char)sr.Read());
    }      
}
相关问题