【问题标题】:read label movement at Maze c#在迷宫 c# 中读取标签移动
【发布时间】:2016-12-02 08:52:44
【问题描述】:

这是我的迷宫生成器 -

Maze.cs 和 Cell.cs:http://pastebin.com/F4DBARr1

frmMaze.cs:http://pastebin.com/uLVVwD37

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
//       MazeSolution.Cell Cells = new MazeSolution.Cell masWall[x][y];

        //capture up arrow key
        if (keyData == Keys.Up  )
        {
            label1.Top += -2;
            return true;
        }

        //capture down arrow key
        if (keyData == Keys.Down)
        {
            label1.Top += 2;
            return true;
        }
        //capture left arrow key
        if (keyData == Keys.Left)
        {
            label1.Left += -2;
            return true;
        }
        //capture right arrow key
        if (keyData == Keys.Right)
        {
            label1.Left += 2;
            return true;
        }
        return base.ProcessCmdKey(ref msg, keyData);

    }

这是一个带有移动标签的代码

我有标签(播放器) 我想创建一个数组 Player (x, y) x = y = 0 此数组可比单元格数组 Cell (i, j) 如果键盘上的箭头是墙底的话,再按一下,如果没有的话

IF cell[i, j].Wall[0] = 0 然后 y + 1 那么 Player,将在 (0.1)

有必要记住数组的值直到 - player(max, max)

这可能应该绕一个圈子完成

ps 我英语不好

【问题讨论】:

  • 您的问题是什么?并将源代码的相关部分复制粘贴到问题中。
  • 你真的希望陌生人阅读 1000+ LOC 吗?请提供一个重现问题的最小示例,并清楚地指出您尝试过的内容以及失败的地方...
  • 据我所知,您正在寻找与迷宫墙壁的碰撞,如果玩家没有撞到墙壁,则移动玩家。您还想跟踪玩家在迷宫中的移动吗?
  • @AlexanderFarber 我想使用其他类的数组 cell(x,y).Wall(),但不明白如何正确使用它
  • @TroyMac1ure 是的!

标签: c# arrays windows winforms


【解决方案1】:

在移动 label1 之前,您应该检查是否有墙壁碰撞。也许将您的位置 (X,Y) 更改存储到 Point 变量中,然后使用它来调用 bool CheckCollision() 函数,如果没有发生碰撞,则更新您的 label1 位置。

首先在你的 frmMaze 类中声明这个:

    Point PlayerPosition;

然后使用这个函数进行移动:

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        //       MazeSolution.Cell Cells = new MazeSolution.Cell masWall[x][y];

        int direction = -1;

        //capture up arrow key
        if (keyData == Keys.Up)
        {
            direction = 0;
        }

        //capture down arrow key
        if (keyData == Keys.Down)
        {
            direction = 2;
        }
        //capture left arrow key
        if (keyData == Keys.Left)
        {
            direction = 1;
        }
        //capture right arrow key
        if (keyData == Keys.Right)
        {
            direction = 3;
        }

        if (direction != -1 && CheckCollision(PlayerPosition, direction))
        {
            switch (direction)
            {
                case 0:
                    PlayerPosition.Offset(0, -1);
                    break;
                case 1:
                    PlayerPosition.Offset(-1, 0);
                    break;
                case 2:
                    PlayerPosition.Offset(0, 1);
                    break;
                case 3:
                    PlayerPosition.Offset(1, 0);
                    break;
            }
        }

        label1.Location = new Point(22 + PlayerPosition.X * Cell.kCellSize, 22 + PlayerPosition.Y * Cell.kCellSize);

        if (direction != -1)
            return true;

        return base.ProcessCmdKey(ref msg, keyData);

    }

    private bool CheckCollision(Point pos, int direction)
    {            
        Cell c = TheMaze.Cells[pos.X][pos.Y];
        if (c.Walls[direction] == 1)
            return false;
        else
            return true;
    }

在 Maze.cs 文件中,将单元格声明更改为公开(在第 35 行附近):

    public Cell[][] Cells = null;

然后您可以使用 TheMaze.cells[x,y] 访问每个单元格(或者它可能是 [y,x])。

【讨论】:

  • bool CheckCollision(Point p) { return } 这个函数?
  • 是的。同样要开始我更改上面的代码,以便玩家一次移动 1 个单元格而不是 2 个像素。
  • 是的,我想过
  • (CheckCollision(Point.Add(label1.Location, new Size(change)))) - 4 而不是 3)
  • 你不妨做一个变量:Point PlayerPosition;这将跟踪玩家所在的单元格。然后您的 label1.Location 可以通过将 PlayerPosition 乘以 kCellSize 来更新。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多