using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 别踩白块er
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Button kaiguan = new Button();
private void Form1_Load(object sender, EventArgs e)
{
//游戏区域
panel1.Size = new Size(400,600);
panel1.BackColor = Color.White;
panel1.Location = new Point(0,0);
this.Size = new Size(500,600);
this.Controls.Add(panel1);
kaiguan.Text = "开始游戏";
kaiguan.Location = new Point(500,200);
this.Controls.Add(kaiguan);
kaiguan.Click += Kaiguan_Click;
}
private void Kaiguan_Click(object sender, EventArgs e)
{
if (kaiguan.Text=="开始游戏")
{
timer1.Start();
timer2.Start();
kaiguan.Text = "游戏暂停";
}
else if (kaiguan.Text=="游戏暂停")
{
timer1.Stop();
timer2.Stop();
kaiguan.Text = "开始游戏";
}
}
Random ran = new Random();
private void timer1_Tick(object sender, EventArgs e)
{
//黑键
Label hei = new Label();
hei.Size = new Size(100,100);
hei.BackColor = Color.Black;
//黑left
int p = ran.Next(0,4) * 100;
hei.BorderStyle = BorderStyle.FixedSingle;
hei.Location = new Point(p,-100);
panel1.Controls.Add(hei);
hei.MouseClick += Hei_MouseClick;
Label lab1 = kuai();
Label lab2 = kuai();
Label lab3= kuai();
switch (p)
{
case 0:
lab1.Location = new Point(1*100,-100);
lab2.Location = new Point(2 * 100, -100);
lab3.Location = new Point(3 * 100, -100);
break;
case 100:
lab1.Location = new Point(0,-100);
lab2.Location = new Point(2*100,-100);
lab3.Location = new Point(3*100,-100);
break;
case 200:
lab1.Location = new Point(0, -100);
lab2.Location = new Point(1 * 100, -100);
lab3.Location = new Point(3 * 100, -100);
break;
case 300:
lab1.Location = new Point(0, -100);
lab2.Location = new Point(1 * 100, -100);
lab3.Location = new Point(2 * 100, -100);
break;
}
}
//黑块单机事件
private void Hei_MouseClick(object sender, MouseEventArgs e)
{
Label lab = sender as Label;
lab.BackColor = Color.White;
}
private Label kuai()
{
//白键
Label lab = new Label();
lab.Size = new Size(100,100);
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)
{
Label lab = sender as Label;
lab.BackColor = Color.Red;
timer2.Stop();
timer1.Stop();
MessageBox.Show("Game over");
}
private void timer2_Tick(object sender, EventArgs e)
{
foreach (Control item in panel1.Controls)
{
item.Top += 2;
if (item.Top >= panel1.Height)
{
if (item.BackColor==Color.Black)
{
timer1.Stop();
timer2.Stop();
MessageBox.Show("Game over");
}
else
{
item.Dispose();
}
}
}
}
}
}