private bool downKey = false, rightKey = false, leftKey = false;
private void TetrisGame_KeyDown(object sender, KeyEventArgs e)
{
Graphics g = Graphics.FromImage(canvas);
if (e.KeyCode == Keys.Down && CurrentBlock.canMoveDown())
{
downKey = true;
CurrentBlock.moveDown(g);
if (rightKey && CurrentBlock.canMoveRight()) CurrentBlock.moveRight(g);
else if (leftKey && CurrentBlock.canMoveLeft()) CurrentBlock.moveLeft(g);
}
else if (e.KeyCode== Keys.Down)
{
downKey = true;
newBlock();
}
else if (e.KeyCode == Keys.Right && CurrentBlock.canMoveRight())
{
rightKey = true;
CurrentBlock.moveRight(g);
if (downKey && CurrentBlock.canMoveDown()) CurrentBlock.moveDown(g);
else if (downKey) newBlock();
}
else if (e.KeyCode == Keys.Right)
{
rightKey = true;
if (downKey && CurrentBlock.canMoveDown()) CurrentBlock.moveDown(g);
else if (downKey) newBlock();
}
else if (e.KeyCode == Keys.Left && CurrentBlock.canMoveLeft())
{
leftKey = true;
CurrentBlock.moveLeft(g);
if (downKey && CurrentBlock.canMoveDown()) CurrentBlock.moveDown(g);
else if (downKey) newBlock();
}
else if (e.KeyCode == Keys.Left)
{
leftKey = true;
if (downKey && CurrentBlock.canMoveDown()) CurrentBlock.moveDown(g);
else if (downKey) newBlock();
}
this.Refresh();
}
private void TetrisGame_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Down)
downKey = false;
else if (e.KeyCode == Keys.Right)
rightKey = false;
else if (e.KeyCode == Keys.Left)
leftKey = false;
}