gpt4 book ai didi

C语言系列之推箱子游戏

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 34 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章C语言系列之推箱子游戏由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

本文实例为大家分享了C语言系列之推箱子游戏的具体代码,供大家参考,具体内容如下 。

输入WSAD控制行走,只需要把一个方向的代码写好了,剩下的是三个方向就是复制粘贴和简单的修改就可以了.

?
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include <stdio.h>
#include <conio.h>  //键盘输入函数库
 
//0:空地 1:墙 2:箱子要放的位置   5:人与星号重叠5:箱子与星号重叠 3:人+-3 4:箱子:+-4
int map[10][10] = {1,1,1,1,1,1,1,1,1,1,
                    1,0,0,0,0,0,2,0,0,1,
                    1,0,0,3,4,0,0,0,0,1,
                    1,0,0,1,1,0,0,0,0,1,
                    1,0,1,0,0,0,1,0,0,1,
                    1,0,0,4,0,0,1,0,0,1,
                    1,0,0,0,0,0,0,0,0,1,
                    1,0,2,0,0,0,0,0,0,1,
                    1,0,0,0,0,0,0,0,0,1,
                    1,1,1,1,1,1,1,1,1,1,
                 };
 
int flag=0;
 
void PrintMap()
{
     int i, j;
     for (i = 0; i < 10; i++)
     {
         for (j = 0; j < 10; j++)
         {
             switch (map[i][j])
             {
                 //0:空地 1:墙 2:箱子要放的位置 3:人 4:箱子  5:箱子到达目的地
             case 0: printf ( "  " ); break ;
             case 1: printf ( "■" ); break ;
             case 2: printf ( "☆" ); break ;
             case 3: printf ( "♀" );  break ;
             case 4: printf ( "□" ); break ;
             case 5: printf ( "★" ); break ;
             default :
                 break ;
             }
         }
         printf ( "\n" );
     }
}
 
void PlayGame()
{
     int num = 0;
     char temp;
     temp = getch(); //键盘输入无回显函数
     static int x=2,y=3;
     switch (temp)
     {
         case 'W' :
         if (map[x-1][y] == 0) //如果下一个的地方为空地
         {
             if (flag == 0)
             {
                 map[x-1][y] = 3;
                 map[x][y] = 0;
                 x--;
             }
             else
             {
                 map[x-1][y] = 3;
                 map[x][y] = 2;
                 x--;
                 flag = 0;
             }
 
         }
         else if (map[x-1][y] == 2) //如果下一个的地方为箱子存放地
         {
             map[x-1][y] = 3;
             map[x][y] = 0;
             x--;
             if (map[x-1][y] == 0)
             {
                 flag = 1;
             }
         }
         else if (map[x-1][y] == 4) //下一个去的地方有箱子
         {
 
                 if (map[x-2][y] == 0 ) //x-2是空地时
                 {
                     if (flag == 0)
                     {
                         map[x-2][y] = 4;
                         map[x-1][y] = 3;
                         map[x][y] = 0;
                         x--;
                     }
                     else
                     {
                         map[x-2][y] = 4;
                         map[x-1][y] = 3;
                         map[x][y] = 2;
                         x--;
                         flag = 0;
                     }
                 }
                 else if (map[x-2][y] == 2) //x-2是箱子的存放地
                 {
                     map[x-2][y] = 5;
                     map[x-1][y] = 3;
                     map[x][y] = 0;
                     x--;
                 }
         }
 
         else if (map[x-1][y] == 5) //下一个去的地方是已经在存放点的箱子
         {
             if (map[x-2][y] != 1)
             {
                 map[x-2][y] = 4;
                 map[x-1][y] = 3;
                 map[x][y] = 0;
                 x--;
                 if (map[x-2][y] != 1)
                 {
                     flag = 1;
                 }
             }
 
         }
         break ;
 
         case 'S' :
         if (map[x+1][y] == 0) //如果下一个的地方为空地
         {
             if (flag == 0)
             {
                 map[x+1][y] = 3;
                 map[x][y] = 0;
                 x++;
             }
             else
             {
                 map[x+1][y] = 3;
                 map[x][y] = 2;
                 x++;
                 flag = 0;
             }
 
         }
         else if (map[x+1][y] == 2) //如果下一个的地方为箱子存放地
         {
             map[x+1][y] = 3;
             map[x][y] = 0;
             x++;
             if (map[x+1][y] == 0)
             {
                 flag = 1;
             }
         }
         else if (map[x+1][y] == 4) //下一个去的地方有箱子
         {
 
                 if (map[x+2][y] == 0 ) //x-2是空地时
                 {
                     if (flag == 0)
                     {
                         map[x+2][y] = 4;
                         map[x+1][y] = 3;
                         map[x][y] = 0;
                         x++;
                     }
                     else
                     {
                         map[x+2][y] = 4;
                         map[x+1][y] = 3;
                         map[x][y] = 2;
                         x++;
                         flag = 0;
                     }
                 }
                 else if (map[x+2][y] == 2) //x-2是箱子的存放地
                 {
                     map[x+2][y] = 5;
                     map[x+1][y] = 3;
                     map[x][y] = 0;
                     x++;
                 }
         }
 
         else if (map[x+1][y] == 5) //下一个去的地方是已经在存放点的箱子
         {
             if (map[x+2][y] != 1)
             {
                 map[x+2][y] = 4;
                 map[x+1][y] = 3;
                 map[x][y] = 0;
                 x++;
                 if (map[x+2][y] != 1)
                 {
                     flag = 1;
                 }
             }
 
         }
         break ;
 
         case 'A' :
         if (map[x][y-1] == 0) //如果下一个的地方为空地
         {
             if (flag == 0)
             {
                 map[x][y-1] = 3;
                 map[x][y] = 0;
                 y--;
             }
             else
             {
                 map[x][y-1] = 3;
                 map[x][y] = 2;
                 y--;
                 flag = 0;
             }
 
         }
         else if (map[x][y-1] == 2) //如果下一个的地方为箱子存放地
         {
             map[x][y-1] = 3;
             map[x][y] = 0;
             y--;
             if (map[x][y-1] == 0)
             {
                 flag = 1;
             }
         }
         else if (map[x][y-1] == 4) //下一个去的地方有箱子
         {
 
                 if (map[x][y-2] == 0 ) //x-2是空地时
                 {
                     if (flag == 0)
                     {
                         map[x][y-2] = 4;
                         map[x][y-1] = 3;
                         map[x][y] = 0;
                         y--;
                     }
                     else
                     {
                         map[x][y-2] = 4;
                         map[x][y-1] = 3;
                         map[x][y] = 2;
                         y--;
                         flag = 0;
                     }
                 }
                 else if (map[x][y-2] == 2) //x-2是箱子的存放地
                 {
                     map[x][y-2] = 5;
                     map[x][y-1] = 3;
                     map[x][y] = 0;
                     y--;
                 }
         }
 
         else if (map[x][y-1] == 5) //下一个去的地方是已经在存放点的箱子
         {
             if (map[x][y-2] != 1)
             {
                 map[x][y-2] = 4;
                 map[x][y-1] = 3;
                 map[x][y] = 0;
                 y--;
                 if (map[x][y-2] != 1)
                 {
                     flag = 1;
                 }
             }
 
         }
         break ;
 
         case 'D' :
         if (map[x][y+1] == 0) //如果下一个的地方为空地
         {
             if (flag == 0)
             {
                 map[x][y+1] = 3;
                 map[x][y] = 0;
                 y++;
             }
             else
             {
                 map[x][y+1] = 3;
                 map[x][y] = 2;
                 y++;
                 flag = 0;
             }
 
         }
         else if (map[x][y+1] == 2) //如果下一个的地方为箱子存放地
         {
             map[x][y+1] = 3;
             map[x][y] = 0;
             y++;
             if (map[x][y+1] == 0)
             {
                 flag = 1;
             }
         }
         else if (map[x][y+1] == 4) //下一个去的地方有箱子
         {
 
                 if (map[x][y+2] == 0 ) //x-2是空地时
                 {
                     if (flag == 0)
                     {
                         map[x][y+2] = 4;
                         map[x][y+1] = 3;
                         map[x][y] = 0;
                         y++;
                     }
                     else
                     {
                         map[x][y+2] = 4;
                         map[x][y+1] = 3;
                         map[x][y] = 2;
                         y++;
                         flag = 0;
                     }
                 }
                 else if (map[x][y+2] == 2) //x-2是箱子的存放地
                 {
                     map[x][y+2] = 5;
                     map[x][y+1] = 3;
                     map[x][y] = 0;
                     y++;
                 }
         }
 
         else if (map[x][y+1] == 5) //下一个去的地方是已经在存放点的箱子
         {
             if (map[x][y+2] != 1)
             {
                 map[x][y+2] = 4;
                 map[x][y+1] = 3;
                 map[x][y] = 0;
                 y++;
                 if (map[x][y+2] != 1)
                 {
                     flag = 1;
                 }
             }
 
         }
         break ;
     default :
         break ;
     }
}
 
 
int  main()
{
     printf ( "    \r\n请输入WASD控制行走\r\n" );
     sleep(2);
     while ((map[1][6] != 5) || (map[7][2] != 5))
     {
         PrintMap();
         PlayGame();
         system ( "cls" );
     }
     printf ( "    \n恭喜您成功通关\n\n\n" );
     return 0;
}

结果显示:

C语言系列之推箱子游戏

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.

原文链接:https://blog.csdn.net/weixin_45402928/article/details/118462706 。

最后此篇关于C语言系列之推箱子游戏的文章就讲到这里了,如果你想了解更多关于C语言系列之推箱子游戏的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com