我在C#winforms中制作一个简单的游戏。 我尝试制作的是一种使用黑色和灰色瓷砖的轻敲瓷砖游戏。 目标是击中黑色瓷砖但是有一个捕获,黑色瓷砖的每一秒都会改变。
我已经按照我想要的方式工作,但现在是时候调整了!虽然这样做我遇到了一个问题,但我似乎无法弄明白。 这就是我想要做的事情:
我有一个由图片框组成的3 * 4网格。第一行是随机生成的1个黑色和3个灰色瓷砖。这需要保持这种方式一秒钟,然后第一行瓷砖需要移动到第二行,第一行需要再次随机生成。然后第二个rown上的tile需要移动到第三行,第一行上的tile需要再移动到第二行,第一行需要再次生成。
我做了这个工作但不是我想要的方式。
因为我的所有图片框都有相同的颜色组合。任何人都可以帮我解决这个问题吗?还是把我推向正确的方向?我会提供我的代码,因为我不知道现在要添加到我的帖子中。
我很抱歉链接,但我无法加载图片!
这是我对瓷砖的方法:
public void RandomPanel()
{
if (i == 1)
{
i++;
Random rnd = new Random();
int temp = rnd.Next(1, 5);
switch (temp)
{
case 1:
TilelLoc = 1;
PbRow3_1.BackgroundImage = _2048Tiles.Properties.Resources.BlackTile;
PbRow3_2.BackgroundImage = _2048Tiles.Properties.Resources.GrayTile;
PbRow3_3.BackgroundImage = _2048Tiles.Properties.Resources.GrayTile;
PbRow3_4.BackgroundImage = _2048Tiles.Properties.Resources.GrayTile;
break;
case 2:
TilelLoc = 2;
PbRow3_2.BackgroundImage = _2048Tiles.Properties.Resources.BlackTile;
PbRow3_1.BackgroundImage = _2048Tiles.Properties.Resources.GrayTile;
PbRow3_3.BackgroundImage = _2048Tiles.Properties.Resources.GrayTile;
PbRow3_4.BackgroundImage = _2048Tiles.Properties.Resources.GrayTile;
break;
case 3:
TilelLoc = 3;
PbRow3_3.BackgroundImage = _2048Tiles.Properties.Resources.BlackTile;
PbRow3_1.BackgroundImage = _2048Tiles.Properties.Resources.GrayTile;
PbRow3_2.BackgroundImage = _2048Tiles.Properties.Resources.GrayTile;
PbRow3_4.BackgroundImage = _2048Tiles.Properties.Resources.GrayTile;
break;
case 4:
TilelLoc = 4;
PbRow3_4.BackgroundImage = _2048Tiles.Properties.Resources.BlackTile;
PbRow3_1.BackgroundImage = _2048Tiles.Properties.Resources.GrayTile;
PbRow3_2.BackgroundImage = _2048Tiles.Properties.Resources.GrayTile;
PbRow3_3.BackgroundImage = _2048Tiles.Properties.Resources.GrayTile;
break;
default:
break;
}
}
else if (i == 2)
{
PbRow2_1.BackgroundImage = PbRow3_1.BackgroundImage;
PbRow2_2.BackgroundImage = PbRow3_2.BackgroundImage;
PbRow2_3.BackgroundImage = PbRow3_3.BackgroundImage;
PbRow2_4.BackgroundImage = PbRow3_4.BackgroundImage;
i++;
}
else if (i == 3)
{
i = 1;
PbRow1_1.BackgroundImage = PbRow2_1.BackgroundImage;
PbRow1_2.BackgroundImage = PbRow2_2.BackgroundImage;
PbRow1_3.BackgroundImage = PbRow2_3.BackgroundImage;
PbRow1_4.BackgroundImage = PbRow2_4.BackgroundImage;
}
}
答案 0 :(得分:1)
有几个问题。您的i
变量不起作用,因为您希望每次调用此方法时随机化第1行的切片,之后,颜色应该从一个滚动到另一个。这要求您首先将以下行设置为其上方行的值,然后为第一行提供新的随机性:
private int counter = 0;
Random rnd = new Random();
public void RandomPanel() {
++counter;
if (counter > 2) {
PbRow1_1.BackgroundImage = PbRow2_1.BackgroundImage;
PbRow1_2.BackgroundImage = PbRow2_2.BackgroundImage;
PbRow1_3.BackgroundImage = PbRow2_3.BackgroundImage;
PbRow1_4.BackgroundImage = PbRow2_4.BackgroundImage;
}
if (counter > 1) {
PbRow2_1.BackgroundImage = PbRow3_1.BackgroundImage;
PbRow2_2.BackgroundImage = PbRow3_2.BackgroundImage;
PbRow2_3.BackgroundImage = PbRow3_3.BackgroundImage;
PbRow2_4.BackgroundImage = PbRow3_4.BackgroundImage;
}
int temp = rnd.Next(0, 4);
Control[] boxes = new Control[] { PbRow3_1, PbRow3_2, PbRow3_3, PbRow3_4 };
for (int i = 0; i < boxes.Length; ++i) {
if (i == temp) {
boxes[i].BackgroundImage = _2048Tiles.Properties.Resources.BlackTile;
} else {
boxes[i].BackgroundImage = _2048Tiles.Properties.Resources.GrayTile;
}
}
}