5

I'm trying to write a tetris clone, and after doing some research I've come across an example that uses small user controls to form the block and a bigger user control that contains the grid.

Everything I've written already seems to work just fine (blocks are being generated and placed on the grid, i can even place them somewhere else if I change the code), but I can't seem to get the blocks to move while the program is running. The example I use does this by changing the control.left property of each of the blocks. I've tried this, debugged it and while the property changes, the block doesn't move.

I've searched around for about 4 hours now. I'm a novice programmer, so I know it's probably something stupid, but I cant find what it is.

Here's the methods I've written:

//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 is actually an arraylist which just contains the 4 Blocks. Block.cs is a partial class since it's the code behind the usercontrol that is the small squares, Shape.cs makes the shapes out of the blocks and tetrisgame is just the gamelogic

the 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 
            }
        }
Mickey
  • 943
  • 1
  • 19
  • 41
Frederik
  • 51
  • 2
  • How are you gaining input? Keyboard, mouse? I guess your approach might be working when you have only a few items. Are you using WPF or Winforms? – bash.d Feb 26 '13 at 11:19
  • I'm using the keyboard with a keypress event and winforms. The keyboard input works, since I also use it to open the help form. – Frederik Feb 26 '13 at 11:29
  • 1
    try setting this.location = new point(x,y); – Jack Gajanan Feb 26 '13 at 11:35
  • What does your event handler look like? – bash.d Feb 26 '13 at 11:37
  • @Jack Gajanan I've changed the Moveside Method to look like `public void MoveSide(int Step) { XCO += (20 * Step); this.Location = new Point(XCO, YCO); }` But to no avail :( @bash.d I'll place it in the OP – Frederik Feb 26 '13 at 16:49

1 Answers1

1

It seems that the "bigger user control that contains the grid" with it's children is not redrawn. Change MoveSide to:

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

so everything is redrawn correctly.