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

Fre*_*rik 5 .net c# user-controls tetris winforms

我正在尝试编写一个俄罗斯方块克隆,在做了一些研究后,我遇到了一个使用小用户控件来构成块的示例以及包含网格的更大的用户控件.

我写的所有内容似乎都工作得很好(块正在生成并放置在网格上,如果我更改代码,我甚至可以将它们放在其他地方),但我似乎无法在块中移动块程序正在运行.我使用的示例通过更改control.left每个块的属性来实现此目的.我试过这个,调试它,当属性改变时,块不移动.

我现在已经四处搜索了大约4个小时.我是一名新手程序员,所以我知道它可能是愚蠢的,但我无法找到它是什么.

这是我写的方法:

//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
        }
Run Code Online (Sandbox Code Playgroud)

Shape实际上是一个arraylist,它只包含4个Blocks.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 
            }
        }
Run Code Online (Sandbox Code Playgroud)

小智 1

似乎“包含网格的更大的用户控件”及其子控件没有被重绘。将 MoveSide 更改为:

public void MoveSide(int Step)
    {
        this.Left += (Step * 20);
        Update();
    }
Run Code Online (Sandbox Code Playgroud)

所以一切都被正确地重绘了。