//窗体需要用到的控件
//初始的关卡
//第二关的关卡
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();
}
//初始的关卡
int pass = 1;
Random r = new Random();
private void Form1_Load(object sender, EventArgs e)
{
//窗体的初始位置
this.Size = new Size(1300, 900);
//this.Location = new Point(,);
this.Left = Screen.PrimaryScreen.WorkingArea.Width / 2 - this.Width / 2;
this.Top = Screen.PrimaryScreen.WorkingArea.Height / 2 - this.Height / 2;
panel1.Location = new Point(50, 25);
panel1.Size = new Size(800, 800);
panel1.BackColor = Color.White;
this.Controls.Add(panel1);
// 先随机获取?图片
//调用方法
//简单的算法逻辑
NewMethod();
//参照图
result.Size = new Size(150, 150);
result.Location = new Point(panel1.Right + 50, 50);
result.SizeMode = PictureBoxSizeMode.StretchImage;
this.Controls.Add(result);
}
private void NewMethod()
{
// 通过自增设置后面的关卡
int count = (pass + 1) * (pass + 1);
//获取指定个数的数字不重复
for (int i = 0; i < count; i++)
{
// 随机里面的索引
int index = r.Next(0, 146);
if (i == 0)
{
listBox1.Items.Add(index);
}
else
{
bool pan = true;
// 循环里面集合里面的数量
for (int j = 0; j < listBox1.Items.Count; j++)
{
if (index == (int)listBox1.Items[j])
{
i--;
pan = false;
}
}
if (pan == true)
{
listBox1.Items.Add(index);
}
}
}
// 随机图片添加在容器里面
for (int i = 0; i < listBox1.Items.Count; i++)
{
PictureBox pic = new PictureBox();
pic.Size = new Size(panel1.Width / (pass + 1), panel1.Height / (pass + 1));
int index = (int)listBox1.Items[i];
pic.Tag = listBox1.Items[i];
pic.Image = imageList1.Images[index];
panel1.Controls.Add(pic);
//照片的位置
pic.Left = (i % (pass + 1)) * pic.Width;
pic.Top = (i / (pass + 1)) * pic.Height;
pic.SizeMode = PictureBoxSizeMode.StretchImage;
pic.MouseClick += Pic_MouseClick;
}
//参照图需要变得
int rin = (int)listBox1.Items[r.Next(0,listBox1.Items.Count)];
result.Tag = rin;
result.Image = imageList1.Images[rin];
}
PictureBox result = new PictureBox();
private void Pic_MouseClick(object sender, MouseEventArgs e)
{
PictureBox pic = sender as PictureBox;
if ((int)pic.Tag== (int)result.Tag)
{
panel1.Controls.Clear();
listBox1.Items.Clear();
pass++;
//最大关卡
if (pass>=11)
{
MessageBox.Show("恭喜过关");
this.Close();
}
NewMethod();
}
}
}
}