簡體   English   中英

如何在 Visual Studio C# 窗體中創建滾動磁貼

[英]How to create scrolling tiles in Visual Studio C# Forms

我想在 Visual Studio C# 中使用表單制作基於磁貼的滾動游戲。 我知道它不是最好的平台,但這些是設置參數。 我想想到最終程序的最簡單方法就像 pokemon 2d 自上而下的世界滾動。

我可以創建一個二維圖片框數組,並根據圖塊 ID 的文本二維數組為它們分配圖像。 我可以滾動圖片框,當它們到達某個位置時,跳回原始位置並顯示移動的圖塊。

我的問題是添加控件,它只添加一列而不是完整的網格。 我試過使用表格,但有同樣的問題。

有沒有人使用 VSC# 和表單做過這種類型的大世界卷軸? 有什么教程或建議嗎? 謝謝。

編輯 - 添加了代碼'

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);
            }
        }
}

'

我試過的另一種方式......'

    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]);
        }
    }
'

我認為您只是在創建一個正方形並四處移動。 嘗試...

        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);

                }
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM