本文实例为大家分享了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
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
|
#include<stdio.h> #include<stdlib.h> #include<conio.h> #include<windows.h> #include<graphics.h> #include<string.h> int map[4][3]; int num = 0; IMAGE image1, image2, image3, image4, image5, image6, image7, image8, image9, image10, image11, image12, image13; void Initmap() //初始化数组 { for ( int i = 0; i < 4; i++) { for ( int j = 0; j < 3; j++) { map[i][j] = rand () % 100; } } map[2][3] = 100; } void PaintInit() //获取图片地址内容 { loadimage(&image1, L "./image/1.jpg" , 100, 100); loadimage(&image2, L "./image/2.jpg" , 100, 100); loadimage(&image3, L "./image/3.jpg" , 100, 100); loadimage(&image4, L "./image/4.jpg" , 100, 100); loadimage(&image5, L "./image/5.jpg" , 100, 100); loadimage(&image6, L "./image/6.jpg" , 100, 100); loadimage(&image7, L "./image/7.jpg" , 100, 100); loadimage(&image8, L "./image/8.jpg" , 100, 100); loadimage(&image9, L "./image/9.jpg" , 100, 100); loadimage(&image10, L "./image/10.jpg" , 100, 100); loadimage(&image11, L "./image/11.jpg" , 100, 100); loadimage(&image12, L "./image/back.jpg" , 400, 200); loadimage(&image13, L "./image/border.jpg" , 50, 300); } int finMin( int a) //找到数组里面最小的数,然后将设定的图片给对应的数; { int flag=1; for ( int i = 0; i < 4; i++) { for ( int j = 0; j < 3; j++) { if (a > map[i][j]) { flag++; //如过flag 不加上去不变表示没有比他大的数 } } } return flag; } void GameInit() //游戏初始化 { int min=map[0][0]; putimage(450, 0, &image12); putimage(400, 0, &image13); for ( int i = 0; i < 4; i++) { for ( int j = 0; j < 3; j++) { if (finMin(map[i][j]) == 1) { putimage(i * 100, j * 100, &image1); } if (finMin(map[i][j]) == 2) { putimage(i * 100, j * 100, &image2); } if (finMin(map[i][j]) == 3) { putimage(i * 100, j * 100, &image3); } if (finMin(map[i][j]) == 4) { putimage(i * 100, j * 100, &image4); } if (finMin(map[i][j]) == 5) { putimage(i * 100, j * 100, &image5); } if (finMin(map[i][j]) == 6) { putimage(i * 100, j * 100, &image6); } if (finMin(map[i][j]) == 7) { putimage(i * 100, j * 100, &image7); } if (finMin(map[i][j]) == 8) { putimage(i * 100, j * 100, &image8); } if (finMin(map[i][j]) == 9) { putimage(i * 100, j * 100, &image9); } if (finMin(map[i][j]) == 10) { putimage(i * 100, j * 100, &image10); } if (finMin(map[i][j]) == 11) { putimage(i * 100, j * 100, &image11); } } } } void PlayGame() { int x, y; MOUSEMSG m; int newA; char ch; int r, c; //找到当前空白块的下标 // for ( int i = 0; i < 4; i++) { for ( int j = 0; j < 3; j++) { if (map[i][j] == 100) { r = i; c = j; } } } ch = getch(); switch (ch) { case 'W' : case 'w' : { if (c - 1<0) return ; newA = map[r][c]; map[r][c] = map[r][c-1]; map[r][c-1] = newA; num++; break ; } case 'S' : case 's' : { if (c + 1>2) return ; newA = map[r][c]; map[r][c] = map[r][c+1]; map[r][c+1] = newA; num++; break ; } case 'A' : case 'a' : { if (r - 1<0) return ; newA = map[r][c]; map[r][c] = map[r-1][c]; map[r-1][c] = newA; num++; break ; } case 'd' : case 'D' : { if (r+1>3) return ; newA = map[r][c]; map[r][c] = map[r+1][c]; map[r+1][c] = newA; num++; break ; } } } int main() //最后如果图像游戏如果拼完整后,数组的数会是从小到大的,以此来判别是否拼图完整 { TCHAR sql[200]; initgraph(850, 300); Initmap(); PaintInit(); while (1) { cleardevice(); BeginBatchDraw(); GameInit(); outtextxy(650, 210, _T( "原图" )); _stprintf(sql, _T( "走了:%d" ), num); outtextxy(500, 250, sql); EndBatchDraw(); PlayGame(); } getch(); closegraph(); system ( "pause" ); return } |
效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/zggzgw/article/details/75070894