服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - 基于C#的winform实现数字华容道游戏

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

2023-02-08 14:04Iawfy_ C#

这篇文章主要为大家详细介绍了基于C#的winform实现数字华容道游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

游戏界面如图:

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

编程准备:

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

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

主要代码:

variables类:

?
1
2
3
4
5
6
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类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
 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。

窗体界面代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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();
        }
    }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/Iawfy_/article/details/111191312

延伸 · 阅读

精彩推荐
  • C#C#对Access进行增删改查的完整示例

    C#对Access进行增删改查的完整示例

    本文主要是讲C#对Access数据库的增删改查操作,想学习C#和Access数据库操作基础的可以参考借鉴,以下代码都经过实践测试可用,下面跟着小编一起来看看。...

    C#教程网7472021-12-03
  • C#C#实现3D效果完整实例

    C#实现3D效果完整实例

    这篇文章主要介绍了C#实现3D效果的方法,结合完整实例形式分析了C#实现文字3D显示效果的具体步骤与相关操作技巧,需要的朋友可以参考下...

    Quber7722021-12-02
  • C#浅析C#静态类,静态构造函数,静态变量

    浅析C#静态类,静态构造函数,静态变量

    这篇文章主要介绍了浅析C#静态类,静态构造函数,静态变量 的相关资料,非常不错具有参考借鉴价值,需要的朋友可以参考下...

    jerrylsxu7552021-11-24
  • C#c#中多线程间的同步示例详解

    c#中多线程间的同步示例详解

    使用线程时最头痛的就是共享资源的同步问题,处理不好会得到错误的结果,所以下面这篇文章主要给大家介绍了关于c#中多线程间同步的相关资料,需要的朋...

    化云随风10822022-12-06
  • C#C# Newtonsoft.Json 的使用说明

    C# Newtonsoft.Json 的使用说明

    这篇文章主要介绍了C# Newtonsoft.Json 的使用说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    enych7872022-10-27
  • C#C#操作IIS方法集合

    C#操作IIS方法集合

    这篇文章主要介绍了C#操作IIS方法集合的相关资料,需要的朋友可以参考下...

    C#教程网12812021-10-28
  • C#C#无损压缩图片

    C#无损压缩图片

    本文主要介绍了C#无损压缩图片的相关知识。具有很好的参考价值。下面跟着小编一起来看下吧...

    漫天行3672021-12-31
  • C#C# WebApi+Webrtc局域网音视频通话实例

    C# WebApi+Webrtc局域网音视频通话实例

    这篇文章主要为大家详细介绍了C# WebApi+Webrtc局域网音视频通话实例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    qq5371668410642022-11-27