⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 扫雷源代码,能智能形成堆栈与线程技术应用,是个很有用的游戏开发软件
💻 CS
📖 第 1 页 / 共 2 页
字号:
        //扫雷难度等级:高级
        private void 高级EToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.MenuItem6.Checked == false)
            {
                this.Widths = 30;
                this.Heights = 16;
                this.Miles = 99;
                this.MenuItem4.Checked = false;
                this.MenuItem5.Checked = false;
                this.MenuItem7.Checked = false;
                this.MenuItem6.Checked = true;
                this.button1_Click(sender, e);
            }
        }


        //自定义雷区宽度,高度,以及雷数
        private void 自定义ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(UserDefined.ShowSelf(this,PointToScreen (this.pictureBox4.Location),ref Widths,ref Heights,ref Miles))
            {
                this.MenuItem4.Checked=false;
                this.MenuItem5.Checked=false;
                this.MenuItem6.Checked=false;
                this.MenuItem7.Checked=true;
                button1_Click(sender,e);
            }
        }

        //开始游戏
        private void MenuItem2_Click(object sender, EventArgs e)
        {
            button1_Click(sender, e);
        }

        //初始化窗体大小
        private void Initialize_Size(object sender, EventArgs e)
        {
            foreach (Button b in buttonarray)
                this.Controls.Remove(b);
            this.ClientSize -= new System.Drawing.Size(this.Width, this.Height);
            this.ClientSize += new System.Drawing.Size(40 + 20 * this.Widths, 105 + 20 * this.Heights);
            this.button1.Location = new System.Drawing.Point(this.ClientSize.Width / 2 - 10, 27);
            this.pictureBox1.Location = new System.Drawing.Point(this.ClientSize.Width - 40, 27);
            this.pictureBox2.Location = new System.Drawing.Point(this.ClientSize.Width - 27, 27);
            this.pictureBox3.Location = new System.Drawing.Point(this.ClientSize.Width - 14, 27);
        }

        //退出游戏
        private void MenuItem9_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        private void ResumeBackColor(int x, int y)
        {
            if (x >= 0 && x < Widths && y >= 0 && y < Heights&&buttonarray[x,y].Text!=" "&&buttonarray[x,y].Text !="   ")
            {
                buttonarray[x, y].BackColor = Color.White;
                if (buttonarray[x, y].Text == "  ")
                    buttonarray[x, y].Image = global::MineSweeping.Properties.Resources.question;
            }
        }
        //释放鼠标(左键或者右键)
        private void Mouse_Up(object sender, MouseEventArgs e)
        {
            //检查是否需要恢复以(middleX,middleY)为中心的雷点背景颜色
            if (NeedToResume == true)
            {
                NeedToResume = false;
                ResumeBackColor(middleX - 1, middleY - 1);
                ResumeBackColor(middleX - 1, middleY );
                ResumeBackColor(middleX - 1, middleY + 1);
                ResumeBackColor(middleX , middleY - 1);
                ResumeBackColor(middleX , middleY);
                ResumeBackColor(middleX , middleY + 1);
                ResumeBackColor(middleX + 1, middleY - 1);
                ResumeBackColor(middleX + 1, middleY );
                ResumeBackColor(middleX + 1, middleY + 1);
            }
            if (gameover == 0)
            {
                button1.Image = global::MineSweeping.Properties.Resources.Face1;
            }
            int x = this.PointToClient(MousePosition).X/20*20;
            int y = this.PointToClient(MousePosition).Y/20*20;
            x = (x - 20) / 20;
            y = (y - 60) / 20;
            if (rightTag)
            {
                if (e.Button == MouseButtons.Right)
                {
                    rightTag = true;
                    if (buttonarray[x, y].Text=="")
                    {
                        buttonarray[x,y].Text =" ";
                        remain--;
                        Fresh_Remain();
                        buttonarray[x, y].Image = global::MineSweeping.Properties.Resources.Marked;
                    }
                    else if (buttonarray[x, y].Text == " ")
                    {
                        remain++;
                        Fresh_Remain();
                        buttonarray[x, y].Text = "  ";
                        buttonarray[x, y].Image = global::MineSweeping.Properties.Resources.question;
                    }
                    else if (buttonarray[x, y].Text == "  ")
                    {
                        buttonarray[x, y].Text = "";
                        buttonarray[x, y].Image= null;
                    }
                }
                rightTag = false;
            }
            if (leftTag)
                leftTag = false;
        }
        //
        //检查是否扫雷成功(是否已已经挖开所有非雷点)
        private void GameState()
        {
            if (dig == Widths * Heights-Miles)
            {
                gameover = 1;
                button1.Image = global::MineSweeping.Properties.Resources.Face4;
                for (int x = 0; x < Widths; x++)
                {
                    for (int y = 0; y < Heights; y++)
                    {
                        if (buttonarray[x, y].Text == "")
                        {
                            buttonarray[x, y].Text = "   ";
                            remain--;
                            Fresh_Remain();
                            buttonarray[x, y].Image = global::MineSweeping.Properties.Resources.Marked;
                        }
                        buttonarray[x, y].Text = "   ";
                        
                    }
                }
                timer1.Stop();
                Point p = new Point(pictureBox4.Location.X, pictureBox4.Location.Y + 23);

                ///检查是否超过英雄榜记录,如果超过,提示记录参赛大名!
                if (MenuItem4.Checked == true)
                {
                    if (time < least_Time)
                    {
                        least_Time = time;
                        Write_Name.ShowSelf(this,PointToScreen(p), "初级", ref least_Name) ;
                    }   
                }
                if (MenuItem5.Checked == true)
                {
                    if(time<middle_Time )
                    {
                        middle_Time=time;
                        Write_Name.ShowSelf(this,PointToScreen(p), "中级",ref middle_Name);
                    }
                }
                if(MenuItem6.Checked==true)
                {
                    if(time<highest_Time) 
                    {
                        highest_Time=time;
                        Write_Name .ShowSelf(this,PointToScreen(p),"高级",ref highest_Name );
                    }
                }
                ///
            }
        }
        //

        //随机布雷
        private void Initialize_Miles(object sender, EventArgs e)
        {
            int total = Widths * Heights;
            int s = 0, t, i;
            int[] temp = new int[total];
            int[] state=new int[total];
            for ( i = 0; i < total; i++)
            {
                temp[i] = i;
                state[i]=0;
            }
            int count = 0;
            for (count = 0; count < Miles; count++)
            {
                Random r = new Random(System.Environment.TickCount / (count + 1) + System.Environment.TickCount - s);
                s = r.Next(total - count);
                t = temp[s];
                if (state[t] != 1)
                {
                    state[t] = 1;
                }
                temp[s] = temp[total - count - 1];
            }
            for (i = 0; i < total; i++)
            {
                int y = i / Widths;
                int x = i - y* Widths;
                if (state[i] == 1)
                    MileState[x, y] = -1;
                else
                    MileState[x, y] = 0;

            }
            int [,] mmm=new int[Widths+2,Heights+2];
            for (int x = 0; x < Widths+2; x++)
            {
                for (int y = 0; y < Heights+2; y++)
                {
                    if (x == 0 || y == 0 || x == Widths + 1 || y == Heights + 1)
                        mmm[x, y] = 0;
                    else
                        mmm[x, y] = -MileState[x - 1,y - 1];
                }
            }
            for (int x = 0; x < Widths; x++)
            {
                for (int y = 0; y < Heights; y++)
                {
                    if (mmm[x + 1, y + 1] == 0)
                    {
                        MileState[x, y] = mmm[x, y] + mmm[x, y + 1] + mmm[x, y + 2] + mmm[x + 1, y];
                        MileState[x, y] += mmm[x + 1, y + 2] + mmm[x + 2, y] + mmm[x + 2, y + 1] + mmm[x + 2, y + 2];
                    }
                }
            }
        }
        //随机布雷

        //游戏失败
        private void Game_Over()
        {
            gameover = 1;
            timer1.Stop();
            button1.Image = global::MineSweeping.Properties.Resources.Face3;
            //标出未挖开的雷点
            for (int x = 0; x<Widths; x++)
            {
                for (int y = 0; y <Heights; y++)
                {
                    if (MileState[x, y] != -1 && buttonarray[x, y].Text == " ")
                        buttonarray[x, y].Image = global::MineSweeping.Properties.Resources.Bomp;
                    if (MileState[x, y] == -1&&buttonarray[x,y].Text =="")
                        buttonarray[x, y].Image = global::MineSweeping.Properties.Resources.B;
                    buttonarray[x, y].Text = "     ";
                }
            }
        }
        //
        //同步记录游戏时间
        private void timer1_Tick(object sender, EventArgs e)
        {
            time++;
            int s = time;
            if (time >= 1000)
                s = time - time / 1000 * 1000;
            int p = s / 100;
            int q = (s - p * 100) / 10;
            int r = s - p * 100 - q * 10;
            this.pictureBox1.Image = Get_TimeImage(p);
            this.pictureBox2.Image = Get_TimeImage(q);
            this.pictureBox3.Image = Get_TimeImage(r);
        }

        //返回时间背景图片
        private Image Get_TimeImage(int i)
        {
            switch (i)
            {
                case 0:
                    return global::MineSweeping.Properties.Resources._0;
                case 1:
                    return global::MineSweeping.Properties.Resources._1;
                case 2:
                    return global::MineSweeping.Properties.Resources._2;
                case 3:
                    return global::MineSweeping.Properties.Resources._3;
                case 4:
                    return global::MineSweeping.Properties.Resources._4;
                case 5:
                    return global::MineSweeping.Properties.Resources._5;
                case 6:
                    return global::MineSweeping.Properties.Resources._6;
                case 7:
                    return global::MineSweeping.Properties.Resources._7;
                case 8:
                    return global::MineSweeping.Properties.Resources._8;
                case 9:
                    return global::MineSweeping.Properties.Resources._9;
                default :
                    return global::MineSweeping.Properties.Resources._;
            }
        }
        //
        //同步显示未标记雷数
        private void Fresh_Remain()
        {
            if (remain >=0)
            {
                int p = remain / 100;
                int q = (remain - p * 100) / 10;
                int r = remain - p * 100 - q * 10;
                this.pictureBox4.Image = Get_TimeImage(p);
                this.pictureBox5.Image = Get_TimeImage(q);
                this.pictureBox6.Image = Get_TimeImage(r);
            }
            else
            {
                int DI = -remain;
                int p = DI/ 100;
                int q = (DI- p * 100) / 10;
                int r = DI - p * 100 - q * 10;
                this.pictureBox4.Image = Get_TimeImage(10);
                this.pictureBox5.Image = Get_TimeImage(q);
                this.pictureBox6.Image = Get_TimeImage(r);
            }

        }
        //
        //显示帮助文件
        private void MenuItem11_Click(object sender, EventArgs e)
        {
            Help.ShowHelp(this,"winmine.chm");
        }


        //显示关于窗体
        private void MenuItem12_Click(object sender, EventArgs e)
        {
            new AboutBox1().ShowDialog();
        }

        //显示扫雷英雄榜
        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            bool change = false;
            string least="初级:    "+least_Time .ToString()+"    "+least_Name;
            string middle="中级:    "+middle_Time .ToString()+"    "+middle_Name;
            string highest="高级:    "+highest_Time .ToString()+"    "+highest_Name;
            Point p = new Point(pictureBox4.Location.X, pictureBox4.Location.Y + 23);
            if (Hero.ShowSelf(this, PointToScreen(p), least, middle, highest, ref change))
            {
                //重新计分
                if (change == true)
                {
                    least_Time = 999;
                    middle_Time = 999;
                    highest_Time = 999;
                    least_Name = "匿名";
                    middle_Name = "匿名";
                    highest_Name = "匿名";
                }
            }

        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -