2013-02-26 68 views
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 
     } 

形状其实是只包含4个街区的ArrayList。 Block.cs是局部类,因为它是小方块的用户控件后面的代码,Shape.cs使得形状出来的块和tetrisgame只是gamelogic

的按键事件:

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 
      } 
     } 
+0

你是如何获得投入的?键盘,鼠标?当你只有几件物品时,我想你的方法可能会起作用。你使用的是WPF还是Winforms? – 2013-02-26 11:19:45

+0

我正在使用按键事件和winforms的键盘。键盘输入工作,因为我也用它来打开帮助表单。 – Frederik 2013-02-26 11:29:27

+1

尝试设置this.location =新点(x,y); – 2013-02-26 11:35:50

回答

0

看来,包含网格的“更大的用户控件”与其子节点不会重新绘制。 更改MoveSide到:

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

因此,一切都正确重绘。

相关问题