红方:播放器图片框;
蓝色方块:终点;
黑色方块:墙;
我在PictureBox中组织了一个迷宫。
如果玩家到达到达点,
我想实现一个重放功能,再次显示他们找到的路径,我该怎么做?
public void CreateNewMaze()
{
mazeTiles = new PictureBox[XTILES, YTILES];
for (int i = 0; i < XTILES; i++)
{
for (int j = 0; j < YTILES; j++)
{
mazeTiles[i, j] = new PictureBox();
int xPosition = (i * TILESIZE) + 25;
int yPosition = (j * TILESIZE) + 10;
mazeTiles[i, j].SetBounds(xPosition, yPosition, TILESIZE, TILESIZE);
this.Controls.Add(mazeTiles[i, j]);
}
}
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
int directionX = 0;
int directionY = 0;
switch (keyData)
{
case Keys.Left: directionX = -1;
EndGame();
break;
case Keys.Right: directionX = 1;
EndGame();
break;
case Keys.Up: directionY = -1;
EndGame();
break;
case Keys.Down: directionY = 1;
EndGame();
break;
default: return base.ProcessCmdKey(ref msg, keyData);
}
int x = directionX + (PlayerPictureBox.Left - 25) / TILESIZE;
int y = directionY + (PlayerPictureBox.Top - 10) / TILESIZE;
bool isRoad = mazeTiles[x, y].BackColor != Color.Black;
if (isRoad)
{
PlayerPictureBox.Left += directionX * TILESIZE;
PlayerPictureBox.Top += directionY * TILESIZE;
}
return true;
}
1条答案
按热度按时间qacovj5a1#
因此,将此代码添加到您的表单:
像这样修改CreateNewMaze:
修改ProcessCmdKey,如下所示:
下面是这样的:
我建议使用不同的PictureBox示例来回放这些步骤,以便用户可以在回放运行时继续播放。