winforms 我想在制作迷宫游戏时实现回放功能

3mpgtkmj  于 2022-11-16  发布在  其他
关注(0)|答案(1)|浏览(141)

红方:播放器图片框;
蓝色方块:终点;
黑色方块:墙;
我在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;
  
    }
qacovj5a

qacovj5a1#

  • 将每个步骤(或移动)添加到步骤列表中。
  • 添加Replay方法,该方法执行此列表中的每个步骤,执行该步骤,然后等待一段时间。
  • 在每个游戏之前清除“步骤”列表(例如在CreateNewMaze中)。
  • 例如,按空格键即可开始播放。

因此,将此代码添加到您的表单:

class Step
    {
        public int dx;
        public int dy;
    }

    List<Step> steps = new List<Step>();

    async void Replay(int delay = 50)
    {
        int firstX = 1; // Initial player position, maybe you'll have to change it
        int firstY = 1; 

        PlayerPictureBox.Left = 25 + firstX * TILESIZE;
        PlayerPictureBox.Top = 10 + firstY * TILESIZE;

        var copy = steps.ToArray(); // Let's make a copy of steps array, to avoid possible conflicts with working array.
        foreach (var step in copy)
        {
            PlayerPictureBox.Left += step.dx * TILESIZE;
            PlayerPictureBox.Top += step.dy * TILESIZE;

            await Task.Delay(delay);
        }
    }

像这样修改CreateNewMaze:

public void CreateNewMaze()
    {
        // Add the following line
        steps.Clear();

        maze = new bool[XTILES, YTILES];
        //...

修改ProcessCmdKey,如下所示:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        int directionX = 0;
        int directionY = 0;
        switch (keyData)
        {
            // Add the following:
            case Keys.Space:
                Replay();
                return true;
            // ...

下面是这样的:

// ...
        if (isRoad)
        {
            // Add the following line
            steps.Add(new Step { dx = directionX, dy = directionY });

            PlayerPictureBox.Left += directionX * TILESIZE;
            PlayerPictureBox.Top += directionY * TILESIZE;
        }

我建议使用不同的PictureBox示例来回放这些步骤,以便用户可以在回放运行时继续播放。

相关问题