我正在尝试编写一个俄罗斯方块克隆,在做了一些研究之后,我遇到了一个使用小型用户控件来形成块和一个包含网格的更大用户控件的示例。
我写的所有东西似乎都可以正常工作(正在生成块并将其放置在网格上,如果我更改代码,我什至可以将它们放置在其他地方),但我似乎无法让块移动,而程序正在运行。我使用的示例通过更改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
}
Shape 实际上是一个仅包含 4 个块的数组列表。Block.cs 是一个部分类,因为它是用户控件背后的代码,即小方块,Shape.cs 将形状从块中取出,俄罗斯方块游戏只是游戏逻辑
按键事件:
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
}
}