Moving a user control inside another user control

I am trying to write a tetris clone, and after some research, I came across an example that uses small user controls to form a block and a larger user control that contains a grid.

Everything that I wrote seems to work very well (blocks are generated and placed in the grid, I can even place them somewhere else if I change the code), but I can not make the blocks block move while the program is running. In this example, I use this by changing the property of control.lefteach of the blocks. I tried this, debugged it, and until the property changes, the block does not move.

I searched for about 4 hours. I am a beginner programmer, so I know that this is probably something stupid, but I can not find what it is.

Here are the methods I wrote:

//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
        }

The form is actually an arraylist that contains only 4 blocks. Block.cs is a partial class, as this is the code behind the usercontrol, which is a small square, Shape.cs makes shapes out of blocks, and tetrisgame only gallological

Keypress Event:

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 
            }
        }
+5
source share
1 answer

It seems that the "large user control containing the grid" with it, children do not redraw. Change MoveSide to:

public void MoveSide(int Step)
    {
        this.Left += (Step * 20);
        Update();
    }

therefore, everything is redrawn correctly.

0
source

All Articles