分析:
1.实现黑块位置的随机
2.通过黑块的位置判断白块的可能性
3.实现基本的功能等;
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 游戏
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool s = true;
Timer tim = new Timer();
Timer tim1 = new Timer();
private void Form1_Load(object sender, EventArgs e)
{
panel1.Width = 360;
tim.Interval = 1000;
label2.Text = "分数:" + i;
tim.Tick += Tim_Tick;
tim1.Interval = 50;
tim1.Tick += Tim1_Tick;
label1.Click += Label1_Click;
}
int i = 0;
private void Label1_Click(object sender, EventArgs e)
{
if (s == true)
{
tim.Start();
tim1.Start();
label1.Text = "暂停游戏";
label1.BackColor = Color.Pink;
s = false;
}
else
{
tim.Stop();
tim1.Stop();
label1.Text = "开始游戏";
label1.BackColor = Color.Black;
s = true;
}
}
private void Tim1_Tick(object sender, EventArgs e)
{
foreach (Control item in panel1.Controls)
{
item.Top += 5;
if (item.BackColor==Color.Black)
{
if (item.Top >= panel1.Height-item.Height)
{
tim.Stop();
tim1.Stop();
MessageBox.Show("Game Over");
}
}
}
}
Random r = new Random();
private void Tim_Tick(object sender, EventArgs e)
{
Label lb = new Label();
lb.Size = new Size(90,90);
lb.BorderStyle = BorderStyle.FixedSingle;
int p = (r.Next(0, 4))*90;
lb.BackColor = Color.Black;
lb.MouseClick += Lb_MouseClick;
lb.Location = new Point(p,-97);
panel1.Controls.Add(lb);
Label k1 = Lb();
Label k2 = Lb();
Label k3 = Lb();
switch (p)
{
case 0:
k1.Location = new Point(90, -97);
k2.Location = new Point(2 * 90, -97);
k3.Location = new Point(3 * 90, -97);
break;
case 90:
k1.Location = new Point(0 * 90, -97);
k2.Location = new Point(2 * 90, -97);
k3.Location = new Point(3 * 90, -97);
break;
case 180:
k1.Location = new Point(0 * 90, -97);
k2.Location = new Point(1 * 90, -97);
k3.Location = new Point(3 * 90, -97);
break;
case 270:
k1.Location = new Point(0 * 90, -97);
k2.Location = new Point(1 * 90, -97);
k3.Location = new Point(2 * 90, -97);
break;
}
}
private void Lb_MouseClick(object sender, MouseEventArgs e)
{
Label lb = sender as Label;
lb.BackColor = Color.White;
i++;
label2.Text = "分数:" + i;
}
Label Lb()
{
Label lb1 = new Label();
lb1.Size = new Size(90, 90);
lb1.BorderStyle = BorderStyle.FixedSingle;
lb1.BackColor = Color.White;
lb1.Click += Lb1_Click;
panel1.Controls.Add(lb1);
return lb1;
}
private void Lb1_Click(object sender, EventArgs e)
{
foreach (Control item in panel1.Controls)
{
if (item.BackColor == Color.White)
{
tim.Stop();
tim1.Stop();
MessageBox.Show("Game Over");
item.Dispose();
}
}
}
}
}