如何在Visual Studio C#窗体中创建滚动磁贴



我想在Visual Studio C#中使用表单制作一个基于瓦片的滚动游戏。我知道这不是最好的平台,但这些都是设定的参数。我想最简单的方式来思考最终程序是像pokemon 2d自上而下的世界滚动。

我可以创建一个图片框的2d阵列,并根据瓷砖ID的文本2d阵列为其分配图像。我可以滚动图片框,当它们到达某个位置时,跳回原始位置并显示移动的互动程序。

我的问题是添加控件,它只添加了一列,而不是整个网格。我试过使用表格,但遇到了同样的问题。

有人用VSC#和表单做过这种类型的大世界滚动器吗?有什么教程或建议吗?谢谢

EDIT-添加了""代码

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

最新更新