Making an RPG map in C# winform

I’m making an RPG game in windows form. Everything was going perfectly until I got stuck on the map part.

The way the map works is that there’s a “Map” control that contains a series of “Tile” Controls, each of these tiles contains a few attributes that are loaded from a file, which are: Collision, Event and Fight (bools that tell you if that tile is one of those things, for example, if stepping on it has chance of triggering a battle, the battle bool will be true) and the PathW, PathA, PathS and PathD (strings containing the path of the files for each direction), plus the background image attribute.

When the users presses W for example, using the Form1.KeyPress() event, the program calls the MoveW() method of the Map object, which will use Set and Get methods on every Tile to change the path of the tile to the path of the tile above it.

Given that making each tile manually would be almost impossible I made another program that’s basically a map maker, which makes the tile files automatically, you input how wide and how tall, in tiles, the map should be and the program makes those tiles, you can click on them and edit the attributes about them, like Collision and Battle. After you’re done, you press save and the program makes all the files with the adjacent tiles path for you.

Now, this makes perfect logical sense to me, even because i learned that this is how a lot of real RPGs work, but it does not work correctly: When you press S to go down the map gets correctly moved down by a tile, but if you press W after it, it will jump from 2 to 5 tiles (so if you are on Tile46 for example, you press S and go to tile47, press W and you go to Tile41) BUT, if you keep pressing W afer that, it will correctly go up by 1 tile each time you press it, until you press S again and it will skip another few tiles, and the same happens viceversa and with left and right too.

Also, when going up and only when going up, sometimes the tiles get set as “null”, which I handled by making them completely black, this shouldn’t happen on random tiles tho, but only on tiles that are out of bounds.

Also also, the images aren’t placed correctly, they get multiplied randomly almost as if they were looping from tile to tile.

This is how the file containing the Tile data looks like for a 40×20 Tiles map, and it is correct in everything, this is the top-left tile, so obviously the tile above it and at its left are “null”, while the tile below is Tile1 and the tile to its right is Tile20, which, again, is correct.

../../Data/imgMappa/TeAcque/0_img.png
False
False
False
null
null
../../Data/Mappa/TeAcque/TeAcque_Tile_1.txt
../../Data/Mappa/TeAcque/TeAcque_Tile_20.txt

This is the rest of the code on how the movement works:

This is the Tile class

    public Tile()
    {
        InitializeComponent();
    }

    string PathImg;
    bool Collision;
    bool Battle;
    bool Event;
    string PathW = "";
    string PathA = "";
    string PathS = "";
    string PathD = "";
    public void Reload(string @Path)
    {
        try
        {
        StreamReader Origine = new StreamReader(@Path);
        BackgroundImage = Image.FromFile(@Origine.ReadLine());
        Collision = Convert.ToBoolean(Origine.ReadLine());
        Battle = Convert.ToBoolean(Origine.ReadLine());
        Event = Convert.ToBoolean(Origine.ReadLine());
        PathW = @Origine.ReadLine();
        PathA = @Origine.ReadLine();
        PathS = @Origine.ReadLine();
        PathD = @Origine.ReadLine();
        }
        catch (Exception)
        {
            BackgroundImage = null;
            BackColor = Color.Black;
            Collision=false;
            Battle=false;
            Event=false;
        }
    }


    public string GetPathW() { return PathW; }
    public string GetPathA() { return PathA; }
    public string GetPathS() { return PathS; }
    public string GetPathD() { return PathD; }
    public bool GetCollisione() { return Collision; }
    public bool GetScontro() { return Battle; }
    public bool GetEvento() { return Event; }
}

This is the Map Class

    public partial class Map : UserControl
    {
        int WidthInTiles = 0;
        int HeightInTiles = 0;
        Tile[] Tiles;
        int Ntile;
        public Map()
        {
            InitializeComponent();

            WidthInTiles = Width / 100;
            HeightInTiles = Height / 100;

            Ntile = WidthInTiles * HeightInTiles;

             Tiles = new Tile[Ntile];

            int SizeTiles = 100;

            int Conta = 0;

            for (int i = 0; i < WidthInTiles; i++)
            {
                for (int k = 0; k < HeightInTiles; k++)
                {
                    Tiles[Conta] = new Tile();
                    Tiles[Conta].Size = new Size(SizeTiles, SizeTiles);
                    Tiles[Conta].Location = new Point(SizeTiles * i, SizeTiles * k);
                    Tiles[Conta].Name = "Tile_" + i + "_" + k;
                    Controls.Add(Tiles[Conta]);
                    Tiles[Conta].BorderStyle = BorderStyle.FixedSingle;
                    Tiles[Conta].BackgroundImageLayout = ImageLayout.Stretch;
                    Conta++;
                }
            }
        }

        public string Reload(string CurrentZone)
        {
            string a = "";
            for (int i = 0; i < Ntile; i++)
            {
                
                    Tiles[i].Reload(@"../../Data/Mappa/" + CurrentZone + "/" + CurrentZone + "_Tile_" + i + ".txt");
                if (i == 46)
                {
                    a = "../../Data/Mappa/" + CurrentZone + "/" + CurrentZone + "_Tile_" + i + ".txt";
                }
            }
            return a;
        }

        public string MoveW()
        {
            string a="";
            for (int i = 0; i < Ntile; i++)
            {
                    Tiles[i].Reload(Tiles[i].GetPathW());
                if (i == 46)
                {
                    a = Tiles[i].GetPathW();
                }
            }
            return a;
        }

        public string MoveA()
        {
            string a = "";
            for (int i = 0; i < Ntile; i++)
            {
                Tiles[i].Reload(Tiles[i].GetPathA());
                if (i == 46)
                {
                    a = Tiles[i].GetPathA();
                }
            }
            return a;
        }

        public string MoveS()
        {
            string a = "";
            for (int i = 0; i < Ntile; i++)
            {
                Tiles[i].Reload(Tiles[i].GetPathS());
                if (i == 46)
                {
                    a = Tiles[i].GetPathS();
                }
            }
            return a;
        }

       public string MoveD()
        {
            string a="";
            for (int i = 0; i < Ntile; i++)
            {
                    Tiles[i].Reload(Tiles[i].GetPathD());
                if (i == 46)
                {
                    a = Tiles[i].GetPathD();    
                }
            }
            return a;
        }

    }

This is the KeyPress Event

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{

    if (e.KeyChar == 'w')
    {
        label3.Text = mappa1.MoveW();
        return;
    }
    if (e.KeyChar == 'a')
    {
        label3.Text = mappa1.MoveA();
        return;
    }
    if (e.KeyChar == 's')
    {
        label3.Text = mappa1.MoveS();
        return;
    }
    if (e.KeyChar == 'd')
    {
        label3.Text = mappa1.MoveD();
        return;
    }

}

Note that the Reload() Method gets called in the constructor of Form1 and the “CurrentZone” parameter is static for now, always accessing to the same bunch of files.

Here are afew other files of the Tiles for reference:

Tile47:

../../Data/imgMappa/TeAcque/0_img.png
False
False
False
../../Data/Mappa/TeAcque/TeAcque_Tile_46.txt
../../Data/Mappa/TeAcque/TeAcque_Tile_27.txt
../../Data/Mappa/TeAcque/TeAcque_Tile_48.txt
../../Data/Mappa/TeAcque/TeAcque_Tile_67.txt

Tile 120

../../Data/imgMappa/TeAcque/0_img.png
False
False
False
null
../../Data/Mappa/TeAcque/TeAcque_Tile_100.txt
../../Data/Mappa/TeAcque/TeAcque_Tile_121.txt
../../Data/Mappa/TeAcque/TeAcque_Tile_140.txt

Tile 99:

../../Data/imgMappa/TeAcque/1_img.png
False
False
False
../../Data/Mappa/TeAcque/TeAcque_Tile_98.txt
../../Data/Mappa/TeAcque/TeAcque_Tile_79.txt
../../Data/Mappa/TeAcque/TeAcque_Tile_100.txt
../../Data/Mappa/TeAcque/TeAcque_Tile_119.txt

I tried checking the Tile files manually, but they all look perfectly correct.

New contributor

Redcube is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật