如何基于C#的winform实现数字华容道游戏

发布时间:2022-02-20 11:48:59 作者:小新
来源:亿速云 阅读:299

这篇文章给大家分享的是有关如何基于C#的winform实现数字华容道游戏的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

数字华容道游戏类似于拼图游戏,只需将数字1~15按顺序排好即可。该游戏逻辑比较简单,易于编程实现。

游戏界面如图:

如何基于C#的winform实现数字华容道游戏

编程准备:

如何基于C#的winform实现数字华容道游戏

所需控件:label 用于显示时间, 一个重新开始的button,一个panel容器来存放数字块(按钮),再加一个timer来计时及判断游戏是否结束。

主要代码:

variables类:

class variables
    {
        public static int[] a = new int[16] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
             14, 15,16 };
        public static Button[,] buttons = new Button[4, 4];
    }

数组a用于存放数字,随机打乱顺序并分配给buttons。buttons即游戏中的方块。

Methods类:

 class Method
    {
        //数组打乱顺序
        public int[] NewSorting(int[]a)
        {
            Random r = new Random();
            for(int i=0;i<a.Length;i++)
            {
                int temp = a[i];
                int randomindex = r.Next(0, a.Length);
                a[i] = a[randomindex];
                a[randomindex] = temp;
            }
            return a;
        }
 
        //向容器中添加16个按钮
        public void AddButtons(Panel panel,Button[,] buttons)
        {
            //数组随机打乱顺序
            int[] s = NewSorting(a);
            //每个按钮的宽度及高度
            int width = 32;
            int height = 32;
            int x0 = panel.Location.X;
            int y0 = panel.Location.Y;
            for(int i=0;i<buttons.GetLength(0);i++)
                for(int j=0;j<buttons.GetLength(1);j++)
                {
                    Button butt = new Button();
                    //设置按钮相关属性
                    butt.Size = new System.Drawing.Size(width, height);
                    butt.Location = new System.Drawing.Point(x0 + (j + 1) * width, y0 + (i + 1) * height);
                    butt.Visible = true;
                    //打乱顺序的数组分配给每个button
                    butt.Text = s[i * buttons.GetLength(0) + j].ToString();
                    if (butt.Text=="16")
                     {
                        butt.Hide();
                      }
                    variables.buttons[i, j] = butt;
                    
                    //手动添加点击事件
                    butt.Click += new EventHandler(butt_Click);
                    //按钮添加到容器
                    panel.Controls.Add(butt);
                }
        }
 
        //自定义点击事件
        public void butt_Click(Object sender,EventArgs e)
        {
            Button butt = sender as Button;
            Button butt_16 = Find_Button16();
 
            //判断是否相邻,如果相邻则交换
            if(Neighboor(butt,butt_16))
            {
                swap(butt, butt_16);
                butt_16.Focus();
            }
        }
 
        //找出隐藏的按钮 即16所在的按钮
        public Button Find_Button16()
        {
            for(int i=0;i<buttons.GetLength(0);i++)
                for(int j=0;j<buttons.GetLength(1);j++)
                {
                    if (buttons[i, j].Visible == false)
                        return buttons[i, j];
                }
            return null;
        }
 
        //判断两个按钮是否相邻   即两个按钮的坐标位置是否差一个宽度或者高度
        public bool Neighboor(Button butt1, Button butt2)
        {
            int x1 = butt1.Location.X;
            int y1 = butt1.Location.Y;
 
            int x2 = butt2.Location.X;
            int y2 = butt2.Location.Y;
 
            if(((x1==x2)&&(Math.Abs(y1-y2)==butt1.Height))||((y1==y2)&&(Math.Abs(x1-x2)==butt1.Width)))
             {
                return true;
              }
            return false;
        }
 
        //交换两个按钮   交换text 与visible
        public void swap(Button butt1,Button butt2)
        {
            string s = butt1.Text;
            butt1.Text = butt2.Text;
            butt2.Text = s;
 
            bool a = butt1.Visible;
            butt1.Visible = butt2.Visible;
            butt2.Visible = a;
        }
 
        //判断游戏是否完成
        public bool GameoverOrNot()
        {
            for (int i = 0; i < buttons.GetLength(1); i++)
                for (int j = 0; j < buttons.GetLength(0); j++)
                {
                    if (int.Parse(variables.buttons[i, j].Text) != (i * buttons.GetLength(0) + j + 1))
                        return false;
                }
            return true;
        }
    }

游戏中的数字方块为Methods类中的AddButtons方法自动生成的,数字方块总共有16个,其中15个的visible属性为true,1个为false。

窗体界面代码:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
                   
        Method method = new Method();
        int count;
        private void Form1_Load(object sender, EventArgs e)
        {
            method.AddButtons(panel1, buttons);
            label2.Text = "0"+"S";  //初始时间
            timer1.Start();  //启动计时器
        }
 
        private void timer1_Tick(object sender, EventArgs e)
        {
            //默认100毫秒刷新一次
            count += 1;
            label2.Text = (count/10).ToString()+"S";
            if (method.GameoverOrNot())
            {
                timer1.Stop();
                MessageBox.Show("挑战成功!");
            }
        }
 
        private void ButtonR_Click(object sender, EventArgs e)
        {
            timer1.Stop();
            for (int i = 0; i < buttons.GetLength(0); i++)
                for (int j = 0; j < buttons.GetLength(1); j++)
                {
                    buttons[i, j].Hide();
                }
 
            method.AddButtons(panel1, buttons);
            count = 0;
            timer1.Start();
        }
    }

感谢各位的阅读!关于“如何基于C#的winform实现数字华容道游戏”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

推荐阅读:
  1. jQuery实现数字华容道小游戏(实例代码)
  2. 使用C#怎么实现一个猜数字小游戏

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

winform

上一篇:mysql中如何修改事务隔离级别

下一篇:如何使用Docker Compose快速部署多容器服务

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》