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
}
}
你是如何獲得投入的?鍵盤,鼠標?當你只有幾件物品時,我想你的方法可能會起作用。你使用的是WPF還是Winforms? – 2013-02-26 11:19:45
我正在使用按鍵事件和winforms的鍵盤。鍵盤輸入工作,因爲我也用它來打開幫助表單。 – Frederik 2013-02-26 11:29:27
嘗試設置this.location =新點(x,y); – 2013-02-26 11:35:50