在另一个用户控件内移动用户控件

5
我正在尝试编写一个俄罗斯方块克隆游戏。在做了一些研究后,我发现有一个示例使用小用户控件来形成方块,并使用包含网格的大用户控件。 我已经写好的所有内容似乎都可以正常运行(方块被生成并放置在网格上,如果我更改代码,甚至可以将它们放置在其他位置),但是在程序运行时无法让方块移动。示例使用改变每个方块的control.left属性来实现这一点。我也尝试过这样做,并进行了调试,虽然属性会改变,但方块并不会移动。
我已经搜索了大约四个小时。我是一名初学者程序员,所以我知道可能只是一些愚蠢的错误,但我找不到是什么错误。
以下是我编写的方法:
//Class TetrisGame.cs
public void MoveRight()
        {
            blok.MoveBlock("x", 1);
        }
//Class Shape.cs
public void MoveBlock(string pos, int Amount)
        {
            if (pos == "x")
            {
                for (int i = 0; i < this.Shape().Count; i++)
                {
                    ((Blokje)this.Shape()[i]).MoveSide(1);
                }
            }
            if (pos == "y")
            {
                for (int i = 0; i < this.Shape().Count; i++)
                {
                    ((Blokje)this.Shape()[i]).MoveDown(1);
                }
            }
//And, the code that should actually move the block in Block.cs:
        public void MoveSide(int Step)
        {
            this.Left += (Step * 20);//Blocks are 20*20 pixels so should move in steps of 20 pixels
        }

Shape实际上是一个ArrayList,其中只包含4个块。Block.cs是一个部分类,因为它是用户控件(小正方形)的代码背后,Shape.cs由块组成形状,而tetrisgame仅仅是游戏逻辑。

Keypress事件:

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                if (e.KeyChar == 'q')//left
                {
                    if (!paused)
                    {
                        Game.MoveLeft();
                    }
                }
                else if (e.KeyChar == 'd')//right
                {
                    if (!paused)
                    {
                        Game.MoveRight();
                    }
                }
                else if (e.KeyChar == 'p')//pause
                {
                    if (paused)
                    {
                        tmrGame.Start();
                    }
                    else
                    {
                        tmrGame.Stop();
                    }
                }
                else if (e.KeyChar == 'z')//rotate
                {
                    if (!paused)
                    {
                        Game.Rotate();
                    }
                }
                else if (e.KeyChar == 'h')//help
                {
                    Help.Show();
                }
                else if (e.KeyChar == 'f')//save
                {

                }
                else if (e.KeyChar == 's')//Drop
                {
                    if (!paused)
                    {
                        Game.Drop();
                    }
                }
            }
            catch
            { 
                //no error message has to be displayed, this is just to prevent runtime Errors when pressing keys before the game has started 
            }
        }

你是如何获取输入的?键盘、鼠标?我猜你的方法可能只适用于少量项目。你是使用WPF还是Winforms? - bash.d
我正在使用WinForms和keypress事件来使用键盘。键盘输入是有效的,因为我还使用它来打开帮助窗体。 - Frederik
1
尝试设置 this.location = new point(x,y); - Jack Gajanan
你的事件处理程序是什么样子的? - bash.d
@Jack Gajanan,我已经将MoveSide方法改成了: public void MoveSide(int Step) {      XCO + =(20 * Step);      这.Location =新点(XCO,YCO); } 但是没有效果:( @bash.d 我会把它放在OP中。 - Frederik
1个回答

1
似乎“包含网格的更大用户控件”及其子元素没有重新绘制。 将MoveSide更改为:
public void MoveSide(int Step)
    {
        this.Left += (Step * 20);
        Update();
    }

所以一切都能正确重绘。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接