我正在尝试编写一个俄罗斯方块克隆游戏。在做了一些研究后,我发现有一个示例使用小用户控件来形成方块,并使用包含网格的大用户控件。 我已经写好的所有内容似乎都可以正常运行(方块被生成并放置在网格上,如果我更改代码,甚至可以将它们放置在其他位置),但是在程序运行时无法让方块移动。示例使用改变每个方块的
我已经搜索了大约四个小时。我是一名初学者程序员,所以我知道可能只是一些愚蠢的错误,但我找不到是什么错误。
以下是我编写的方法:
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
}
Shape实际上是一个ArrayList,其中只包含4个块。Block.cs是一个部分类,因为它是用户控件(小正方形)的代码背后,Shape.cs由块组成形状,而tetrisgame仅仅是游戏逻辑。
Keypress事件:
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
}
}
public void MoveSide(int Step) { XCO + =(20 * Step); 这.Location =新点(XCO,YCO); }
但是没有效果:( @bash.d 我会把它放在OP中。 - Frederik