How to create scrolling tiles in Visual Studio C# Forms

272 views Asked by At

I want to make a tiles based scrolling game in Visual Studio C# using forms. I know its not the best platform for this but those are set parameters. I suppose the easiest way to think of the end program is like pokemon 2d top down world scrolling.

I can create a 2d array of picture boxes and allocated images to them based on a text 2d array of tile ids. I can scroll the picture boxes and when they reach a certain place, jump back to the original location and display a shifted tile.

My issue is adding controls, it only adds a column rather than the full grid. I have tried using tables but with the same problem.

Has anyone done this type of large world scroller using VSC# and forms? Are there any tutorials or suggestions? Thanks.

EDIT - Code added'

public Form1()
{
        InitializeComponent();

        TableLayoutPanel wholescreen = new TableLayoutPanel();

        wholescreen.Location = new System.Drawing.Point(0,0);
        wholescreen.Size = new System.Drawing.Size(200,200);
        wholescreen.RowCount = 2;
        wholescreen.ColumnCount = 2;
        Controls.Add(wholescreen);

        PictureBox item = new PictureBox();
        item.Size = new System.Drawing.Size(50, 50);
        item.ImageLocation = "C:\\Users\\i.price\\Documents\\1.png";

        for (int x = 0; x < 2; x++)
        {
            for (int y = 0; y < 2; y++)
            {
                item.Left = x * 50;
                item.Top = y * 50;
                wholescreen.Controls.Add(item,x,y);
            }
        }
}

'

another way i tried....'

    int WIDTH = 3;
    int HEIGHT = 3;

    PictureBox[] grid = new PictureBox[9];
    //PictureBox[,] grid = new PictureBox[3, 3];
    //int[,] level1 = new int[2, 2] { { 1, 2 }, { 3, 4 } };

    int playerx = 0;

    public Form1()
    {
        InitializeComponent();

        int y = 0;
        int x = 0;
        for (int cntr = 0; cntr < HEIGHT*WIDTH; cntr++)
        {
            if ((cntr % HEIGHT) == 0)
            {
                x++;
                y = 0;
            }
            grid[cntr] = new PictureBox();
            grid[cntr].Left = x * 50;
            grid[cntr].Top = y * 50;
            grid[cntr].ImageLocation = "C:\\Users\\i.price\\Documents\\1.png";
            Controls.Add(grid[cntr]);
        }
    }
'
1

There are 1 answers

1
damichab On

I think you are just creating one square and moving it around. Try...

        private void Method2()
        {
            TableLayoutPanel wholescreen = new TableLayoutPanel();

            wholescreen.BackColor = Color.AliceBlue;
            wholescreen.Location = new System.Drawing.Point(0, 0);
            wholescreen.Size = new System.Drawing.Size(200, 200);
            wholescreen.RowCount = 2;
            wholescreen.ColumnCount = 2;
            Controls.Add(wholescreen);

            PictureBox item;
            //  item.ImageLocation = "C:\\Users\\i.price\\Documents\\1.png";

            for (int x = 0; x < 2; x++)
            {
                for (int y = 0; y < 2; y++)
                {


                    item = new PictureBox();
                    item.Size = new System.Drawing.Size(50, 50);


                    item.BackColor = Color.Blue;
                    
                    //item.Left = 0;
                    //item.Top = 0;

                    wholescreen.Controls.Add(item, x, y);

                }
            }
        }