namespace 俄罗斯方块
{
public partial class Form1 : Form
{
public static int cakeNO;
public static bool bl_nextCake = false;
Russia myRussia = new Russia();
Russia myRussia3 = new Russia();
bool isBegin = false; //判断能否开始
bool isPause = false; //判断能否暂停
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
btn_start.Enabled = true;
btn_pause.Enabled = false;
this.KeyPreview = true;
}
private void btn_start_Click(object sender, EventArgs e)
{
myRussia.lb_linesClear = label1;
myRussia.lb_score = label2;
myRussia.myControl = panel1;
myRussia3.myControl = panel3;
myRussia.PlaceInitialization();
myRussia3.PlaceInitialization();
myRussia.firstPoint = new Point(150,30); //设置方块的起始位置
label1.Text = “0”; //显示行数,归0
label2.Text = “0”; //显示分数,归0
timer1.Interval = 500; //下移的速度
timer1.Enabled = false; //停止计时
Random rand = new Random(); //实例随机对象
cakeNO = rand.Next(1,8); //获取随机数
myRussia.CakeMode(cakeNO); //生成方块样式
myRussia.Protract(panel1); //绘制场景
NextCake(); //生成下一个方块
textBox1.Focus(); //获取焦点
isBegin = true;
btn_start.Enabled = false;
btn_pause.Enabled = true;
timer1.Enabled = true; //开始计时
}
/// <summary>
/// 生成下一个方块的样式
/// </summary>
public void NextCake()
{
Graphics p3 = panel3.CreateGraphics();
p3.FillRectangle(new SolidBrush(Color.Black),0,0,panel3.Width,panel3.Height);
Random rand = new Random();
cakeNO = rand.Next(1,8);
myRussia3.firstPoint = new Point(90,60);
myRussia3.CakeMode(cakeNO);
myRussia3.Protract(panel3);
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show(“ceshi”);
if (!isBegin)
{
return;
}
if (isPause)
{
return;
}
if (e.KeyCode == Keys.Up) //变换方块的样式
{
myRussia.ConvertorMode(myRussia.convertor);
}
if (e.KeyCode == Keys.Down) //假如按下“↓”,加快速度,方块下移
{
timer1.Interval = 300;
myRussia.ConvertorMove(0);
}
if (e.KeyCode == Keys.Left) //按下“←”,方块左移
{
myRussia.ConvertorMove(1);
}
if (e.KeyCode == Keys.Right) //按下“→”,方块右移
{
myRussia.ConvertorMove(2);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
myRussia.ConvertorMove(0); //方块下移
if(bl_nextCake)
{
NextCake(); //生成下一个方块
bl_nextCake = false;
timer1.Interval = 500; //速度恢复
}
textBox1.Focus(); //获取焦点
}
private void btn_pause_Click(object sender, EventArgs e)
{
if (isPause == false)
{
isPause = true;
timer1.Enabled = false;
btn_pause.Text = “继续”;
}
else
{
isPause = false;
timer1.Enabled = true;
btn_pause.Text = “暂停”;
}
}
20
protected override bool ProcessDialogKey(Keys keyData) { if(keyData == Keys.Left || keyData == Keys.Right || keyData == Keys.Up || keyData == Keys.Down) { return false; } else { return base.ProcessDialogKey(keyData); } }
然后,需要设置form属性Keypreview=true;
20
相似楼上的代码,但是你不用去破坏行为原来的窗体的行为。
protected override bool ProcessDialogKey(Keys keyData) { if (keyData == Keys.Left || keyData == Keys.Right || keyData == Keys.Up || keyData == Keys.Down) { int x = 0; } return base.ProcessDialogKey(keyData); }
在这里的 int x= 0 的地方处理你的操作就行了,不用修改原来的返回结果。