Random r = new Random();
Timer luo = new Timer();
Timer hk = new Timer();
Label ks = new Label();
Label jf = new Label();
int n = 0;
private void Form2_Load(object sender, EventArgs e)
{
//布置背景
this.BackgroundImage = Image.FromFile(@"../../img/01.jpg");
this.BackgroundImageLayout = ImageLayout.Stretch;
//让界面居中
this.Left = Screen.PrimaryScreen.WorkingArea.Width / 2 - this.Width / 2;
this.Top = Screen.PrimaryScreen.WorkingArea.Height / 2 - this.Height / 2;
//开始按钮
//Label ks = new Label();
ks.Text = "开始游戏";
ks.Size = new Size(125, 35);
ks.Font = new Font("", 20);
ks.Location = new Point(panel1.Width + 30, 500);
ks.Click += Ks_Click;
this.Controls.Add(ks);
//计分板
jf.Size = new Size(125, 125);
jf.Font = new Font("", 15);
jf.BackColor = Color.White;
jf.Text = "点击的黑块" + n+"个";
jf.Location = new Point(panel1.Width + 30, 100);
this.Controls.Add(jf);
//黑块的生成
//Timer hk = new Timer();
hk.Interval = 930;
hk.Tick += Hk_Tick;
//黑块下落
//Timer luo = new Timer();
luo.Interval = 30;
luo.Tick += Luo_Tick;
}
private void Ks_Click(object sender, EventArgs e)
{
if (ks.Text=="开始游戏")
{
luo.Start();
hk.Start();
ks.Text = "结束游戏";
}
else if (ks.Text=="结束游戏")
{
luo.Stop();
hk.Stop();
ks.Text = "开始游戏";
}
}
//下落事件
private void Luo_Tick(object sender, EventArgs e)
{
foreach (Control item in panel1.Controls)
{
item.Top += 5;
if (item.Top >= panel1.Height)
{
if (item.BackColor == Color.Black)
{
luo.Stop();
hk.Stop();
MessageBox.Show("GG");
}
else
{
item.Dispose();
}
}
}
}
//生成黑块
private void Hk_Tick(object sender, EventArgs e)
{
Label blackKuai = new Label();
int p = r.Next(0,4) * 100;
blackKuai.Location = new Point(p, -150);
blackKuai.BorderStyle = BorderStyle.FixedSingle;
blackKuai.Size = new Size(100,150);
blackKuai.BackColor = Color.Black;
panel1.Controls.Add(blackKuai);
blackKuai.MouseClick += BlackKuai_MouseClick;
Label whitekuia1 = kuai();
Label whitekuia2 = kuai();
Label whitekuia3 = kuai();
switch (p)
{
case 0:
whitekuia1.Location = new Point(1 * 100, -150);
whitekuia2.Location = new Point(2 * 100, -150);
whitekuia3.Location = new Point(3 * 100, -150);
break;
case 100:
whitekuia1.Location = new Point(0, -150);
whitekuia2.Location = new Point(2 * 100, -150);
whitekuia3.Location = new Point(3 * 100, -150);
break;
case 200:
whitekuia1.Location = new Point(0, -150);
whitekuia2.Location = new Point(1 * 100, -150);
whitekuia3.Location = new Point(3 * 100, -150);
break;
case 300:
whitekuia1.Location = new Point(0, -150);
whitekuia2.Location = new Point(1 * 100, -150);
whitekuia3.Location = new Point(2 * 100, -150);
break;
default:
break;
}
}
//点击事件
private void BlackKuai_MouseClick(object sender, MouseEventArgs e)
{
n++;
jf.Text = "点击的黑块" + n + "个";
Label label = sender as Label;
label.BackColor = Color.White;
}
//白块事件
private Label kuai()
{
Label lab = new Label();
lab.Size = new Size(100, 150);
lab.BorderStyle = BorderStyle.FixedSingle;
lab.BackColor = Color.White;
panel1.Controls.Add(lab);
lab.MouseClick += Lab_MouseClick;
return lab;
}
//点击白块事件
private void Lab_MouseClick(object sender, MouseEventArgs e)
{
luo.Stop();
hk.Stop();
MessageBox.Show("GG");
}
}
}