服务器之家

服务器之家 > 正文

C#拼图游戏编写代码

时间:2021-12-31 13:34     来源/作者:蜡笔小黑

本文设计了c#拼图游戏程序,供大家参考,具体内容如下

C#拼图游戏编写代码

C#拼图游戏编写代码

功能描述:

  1.用户自定义上传图片

  2.游戏难度选择:简单(3*3)、一般(5*5)、困难(9*9)三个级别

  3.纪录完成步数

模块:

  1.拼图类

  2.配置类

  3.游戏菜单窗口

  4.游戏运行窗口

 代码文件vs2013版本:

下载链接: 

--------------------------------------------------我叫分割线---------------------------------------------------------------

1.拼图类

方法:

  1.构造函数:传图片并分割成一个一个小图片

  2.交换方法

  3.大图中截取小单元方法

  4.移动单元的方法

  5.打乱单元顺序方法

?
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
using system;
using system.collections.generic;
using system.drawing;
using system.linq;
using system.text;
using system.threading.tasks;
using system.windows.forms;
 
namespace 拼图
{
 public class puzzle
 {
 public enum diff //游戏难度
 {
 simple,//简单
 ordinary,//普通
 difficulty//困难
 }
 private struct node //拼图单元格结构体
 {
 public image img;
 public int num;
 }
 private image _img; //拼图图片
 public int width; //拼图边长
 private diff _gamedif; //游戏难度
 private node[,] node; //单元格数组
 public int n; //单元格数组行列数
 
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="img">拼图大图</param>
 /// <param name="gamedif">游戏难度,该类下结构体diff</param>
 public puzzle(image img,int width, diff gamedif)
 {
 this._gamedif = gamedif;
 this._img = img;
 this.width = width;
 switch(this._gamedif)
 {
 case diff.simple:    //简单则单元格数组保存为3*3的二维数组
  this.n = 3;
  node=new node[3,3];
  break;
 case diff.ordinary:   //一般则为5*5
  this.n = 5;
  node = new node[5, 5];
  break;
 case diff.difficulty:  //困难则为9*9
  this.n = 9;
  node = new node[9, 9];
  break;
 }
 
 //分割图片形成各单元保存在数组中
 int count = 0;
 for (int x = 0; x < this.n; x++)
 {
 for (int y = 0; y < this.n; y++)
 {
 
  node[x, y].img = captureimage(this._img, this.width / this.n, this.width / this.n, x * (this.width / this.n), y * (this.width / this.n));
  node[x, y].num = count;
  count++;
 }
 }
 
 for (int x = 0; x < this.n; x++)
 {
 for (int y = 0; y < this.n; y++)
 {
 
  graphics newgra = graphics.fromimage(node[x, y].img);
  newgra.drawline(new pen(color.white), new point(0, 0), new point(0, this.width / this.n));
  newgra.drawline(new pen(color.white), new point(0, 0), new point(this.width / this.n, 0));
  newgra.drawline(new pen(color.white), new point(this.width / this.n, this.width / this.n), new point(this.width / this.n, 0));
  newgra.drawline(new pen(color.white), new point(this.width / this.n, this.width / this.n), new point(0,this.width / this.n));
 }
 }
 //(最后一项为空单独处理)
 node[n - 1, n - 1].img = image.fromfile("image\\end.png");
 graphics newgra2 = graphics.fromimage(node[n - 1, n - 1].img);
 newgra2.drawline(new pen(color.red), new point(1, 1), new point(1, this.width / this.n - 1));
 newgra2.drawline(new pen(color.red), new point(1, 1), new point(this.width / this.n - 1, 1));
 newgra2.drawline(new pen(color.red), new point(this.width / this.n - 1, this.width / this.n - 1), new point(this.width / this.n - 1, 1));
 newgra2.drawline(new pen(color.red), new point(this.width / this.n - 1, this.width / this.n - 1), new point( 1,this.width / this.n - 1));
 //打乱拼图
 this.upset();
 
 }
 
 
 /// <summary>
 /// 由图片fromimage中截图并返回
 /// </summary>
 /// <param name="fromimage">原图片</param>
 /// <param name="width">宽</param>
 /// <param name="height">高</param>
 /// <param name="spacex">起始x坐标</param>
 /// <param name="spacey">起始y坐标</param>
 /// <returns></returns>
 public image captureimage(image fromimage, int width, int height, int spacex, int spacey)
 {
 int x = 0;
 int y = 0;
 int sx = fromimage.width - width;
 int sy = fromimage.height - height;
 if (sx > 0)
 {
 x = sx > spacex ? spacex : sx;
 }
 else
 {
 width = fromimage.width;
 }
 if (sy > 0)
 {
 y = sy > spacey ? spacey : sy;
 }
 else
 {
 height = fromimage.height;
 }
 
 //创建新图位图
 bitmap bitmap = new bitmap(width, height);
 //创建作图区域
 graphics graphic = graphics.fromimage(bitmap);
 //截取原图相应区域写入作图区
 graphic.drawimage(fromimage, 0, 0, new rectangle(x, y, width, height), graphicsunit.pixel);
 //从作图区生成新图
 image saveimage = image.fromhbitmap(bitmap.gethbitmap());
 return saveimage;
 }
 /// <summary>
 /// 移动坐标(x,y)拼图单元
 /// </summary>
 /// <param name="x">拼图单元x坐标</param>
 /// <param name="y">拼图单元y坐标</param>
 public bool move(int x,int y)
 {
 //messagebox.show(" " + node[2, 2].num);
 if (x + 1 != n && node[x + 1, y].num == n * n - 1)
 {
 swap(new point(x + 1, y), new point(x, y));
 return true;
 }
 if (y + 1 != n && node[x, y + 1].num == n * n - 1)
 {
 swap(new point(x, y + 1), new point(x, y));
 return true;
 }
 if (x - 1 != -1 && node[x - 1, y].num == n * n - 1)
 {
 swap(new point(x - 1, y), new point(x, y));
 return true;
 }
 if (y - 1 != -1 && node[x, y - 1].num == n * n - 1)
 {
 swap(new point(x, y - 1), new point(x, y));
 return true;
 }
 return false;
 
 }
 //交换两个单元格
 private void swap(point a, point b)
 {
 node temp = new node();
 temp = this.node[a.x, a.y];
 this.node[a.x, a.y] = this.node[b.x, b.y];
 this.node[b.x, b.y] = temp;
 }
 public bool judge()
 {
 int count=0;
 for (int x = 0; x < this.n; x++)
 {
 for (int y = 0; y < this.n; y++)
 {
  if (this.node[x, y].num != count)
  return false;
  count++;
 }
 }
 return true;
 }
 public image display()
 {
 bitmap bitmap = new bitmap(this.width, this.width);
 //创建作图区域
 graphics newgra = graphics.fromimage(bitmap);
 for (int x = 0; x < this.n; x++)
 for (int y = 0; y < this.n; y++)
  newgra.drawimage(node[x, y].img, new point(x * this.width / this.n, y * this.width / this.n));
 return bitmap;
 }
 /// <summary>
 /// 打乱拼图
 /// </summary>
 public void upset()
 {
 int sum = 100000;
 if (this._gamedif == diff.simple) sum = 10000;
 //if (this._gamedif == diff.ordinary) sum = 100000;
 random ran = new random();
 for (int i = 0, x = n - 1, y = n - 1; i < sum; i++)
 {
 long tick = datetime.now.ticks;
 ran = new random((int)(tick & 0xffffffffl) | (int)(tick >> 32)|ran.next());
 switch (ran.next(0, 4))
 {
  case 0:
  if (x + 1 != n)
  {
  move(x + 1, y);
  x = x + 1;
  }
  
  break;
  case 1:
  if (y + 1 != n)
  {
  move(x, y + 1);
  y = y + 1;
  }
  break;
  case 2:
  if (x - 1 != -1)
  {
  move(x - 1, y);
  x = x - 1;
  }
  break;
  case 3:
  if (y - 1 != -1)
  {
  move(x, y - 1);
  y = y - 1;
  }
  break;
 }
 
 }
 }
 
 
 
 }
}

2、配置类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using system;
using system.collections.generic;
using system.drawing;
using system.linq;
using system.text;
using system.threading.tasks;
 
namespace 拼图
{
 public static class gamepage
 {
 public static puzzle.diff dif; //游戏难度
 public static image img; //拼图图案
 }
}

游戏菜单:

通过菜单,上传图片至配置类img,并选择难度上传至配置类dif,然后拼图对象构造时读取配置类

C#拼图游戏编写代码

?
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
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 menu : form
 {
 public menu()
 {
 initializecomponent();
 gamepage.img =image.fromfile(@"image\\拼图.jpg");
 control.checkforillegalcrossthreadcalls = false;
 }
 
 private void button1_click(object sender, eventargs e)
 {
 gamepage.dif = puzzle.diff.simple;
 this.hide();
 form1 ff = new form1();
 ff.closefather+=new 拼图.form1.childclose(this.closethis);
 ff.show();
 
 }
 
 private void button2_click(object sender, eventargs e)
 {
 gamepage.dif = puzzle.diff.ordinary;
 this.hide();
 form1 ff = new form1();
 ff.closefather += new 拼图.form1.childclose(this.closethis);
 ff.show();
 
 }
 
 private void button3_click(object sender, eventargs e)
 {
 gamepage.dif = puzzle.diff.difficulty;
 this.hide();
 form1 ff = new form1();
 ff.closefather += new 拼图.form1.childclose(this.closethis);
 ff.show();
 
 }
 
 public void closethis()
 {
 this.show();
 }
 
 
 private void button4_click(object sender, eventargs e)
 {
 openfiledialog ofd = new openfiledialog();
 ofd.showdialog();
 gamepage.img = image.fromfile(ofd.filename).getthumbnailimage(600,600,new image.getthumbnailimageabort(delegate { return false; }), intptr.zero);
 
 }
 private void menu_load(object sender, eventargs e)
 {
 }
 
 }
}

游戏运行窗口:

1.注册鼠标点击事件来获得输入消息

2.显示功能

3.picturebox1用来展示游戏拼图情况

4.picturebox2展示完整拼图的缩略图(当鼠标移至picturebox2时,发送消息,使picturebox1显示完整拼图)

5.numlabel来显示移动步数(通过变量num的累加来计算)

C#拼图游戏编写代码

?
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
using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.linq;
using system.text;
using system.threading;
using system.threading.tasks;
using system.windows.forms;
 
namespace 拼图
{
 public partial class form1 : form
 {
 public form1()
 {
 initializecomponent();
 }
 private puzzle puzzle;
 private int num=0;
 private image img;
 private void form1_load(object sender, eventargs e)
 {
 img = gamepage.img;
 picturebox2.image = img.getthumbnailimage(120,120, new image.getthumbnailimageabort(delegate { return false; }), intptr.zero);
 puzzle = new puzzle(img, 600, gamepage.dif);
 picturebox1.image =puzzle.display();
 }
 
 private void picturebox1_mouseclick(object sender, mouseeventargs e)
 {
 if (puzzle.move(e.x / (puzzle.width / puzzle.n), e.y / (puzzle.width / puzzle.n)))
 {
 num++;
 picturebox1.image = puzzle.display();
 if (puzzle.judge())
 {
  if (messagebox.show("恭喜过关", "是否重新玩一把", messageboxbuttons.okcancel) == dialogresult.ok)
  {
  num = 0;
  puzzle.upset();
  picturebox1.image = puzzle.display();
  
  }
  else
  {
  num = 0;
  closefather();
  this.close();
  }
 
 }
 
 }
 numlabel.text = num.tostring();
 }
 
 private void picturebox2_mouseenter(object sender, eventargs e)
 {
 picturebox1.image = img;
 }
 
 private void picturebox2_mouseleave(object sender, eventargs e)
 {
 picturebox1.image = puzzle.display();
 }
 
 
 private void form1_formclosed(object sender, formclosedeventargs e)
 {
 closefather();
 }
 public delegate void childclose();
 public event childclose closefather;
 
 }
}

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

原文链接:http://www.cnblogs.com/labixiaohei/archive/2017/04/12/6698887.html

标签:

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整 2021-08-24
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
返回顶部