C#实现简单的找茬

//窗体需要用到的控件
在这里插入图片描述
//初始的关卡
在这里插入图片描述
//第二关的关卡
在这里插入图片描述

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();
            }
        }
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值