当前位置:首页 > 通信资讯 > 正文

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

代码在vs2013上测试运行。

思想:

1):地图用二维数组实现,箱子、墙壁、人等事物用不同的数字表示,遍历二维数组,遇到不同的数字打印相应的图案即可。
2):按键移动原理:判断要移动的方向是怎样的障碍物,如果理论上可以移动的话,只需把对应位置的数字作相应更改即可。

?
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 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 #include<stdio.h> #include<stdlib.h> #include<windows.h> #include<conio.h> //二维数组:0表示地图上的空地,1代表墙壁,3代表箱子的目的地,4代表箱子,6代表人,7代表箱子与目的地重合;9代表人在箱子的目的地 int g_map[10][12] = { { 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 }, { 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0 }, { 1, 0, 4, 0, 1, 0, 1, 1, 1, 1, 1, 1 }, { 1, 0, 4, 6, 1, 0, 1, 0, 0, 0, 3, 1 }, { 1, 1, 1, 4, 1, 1, 1, 0, 0, 0, 3, 1 }, { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 3, 1 }, { 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1 }, { 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1 }, { 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 }, { 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0 } }; //g_开头代表全局变量 m_代表成员变量 n:整形 void drawMap(); //画地图 void up(); //上移 void down(); //下移 void left(); //左移 void right(); //右移 void gameOver(); //结束游戏 POINT GetGamerPostion();//获取玩家坐标 int main() { //设置窗口标题 SetConsoleTitleA("推箱子"); //修改窗口大小 system("mode con cols=30 lines=12"); while (1) { //清屏 system("cls"); //打印地图 drawMap(); gameOver(); char ch = _getch(); //从控制台获取输入 getchar()函数获取按键后要按enter确认,并且输入的字符要在控制台上显示 switch (ch) { case 'w':case 72://上 up(); break; case 's':case 80://下 down(); break; case 'a':case 75://左 left(); break; case 'd':case 77://右 right(); break; } } //system("pause"); return 0; } void drawMap() { for (int i = 0; i < 10; i++) { for (int j = 0; j < 12; j++) { switch (g_map[i][j]) { case 0://空地 printf(" "); break; case 1://墙壁 printf("■"); break; case 3://箱子的目的地 printf("☆"); break; case 4://箱子 printf("□"); break; case 6://人 printf("♀"); break; case 7://箱子与目的地重合 printf("★"); break; case 9://人站在目的地 printf("♀"); break; } } printf("\n"); } } void up() { //获取玩家坐标 POINT pos = GetGamerPostion(); //1.人的前面是空地 if (g_map[pos.x - 1][pos.y] == 0) { g_map[pos.x - 1][pos.y] = 6; //空地变为人 if (g_map[pos.x][pos.y] == 9) //还原原来人的位置 { g_map[pos.x][pos.y] = 3; } else g_map[pos.x][pos.y] = 0; } //2.人的前面是目的地 if (g_map[pos.x - 1][pos.y] == 3) { g_map[pos.x - 1][pos.y] = 9; //原来目的地的位置变为人站在目的地 if (g_map[pos.x][pos.y] == 9) //还原人的位置 { g_map[pos.x][pos.y] = 3; } else g_map[pos.x][pos.y] = 0; } //3.人的前面是箱子, if (g_map[pos.x - 1][pos.y] == 4) { //a.箱子前面是空地 if (g_map[pos.x - 2][pos.y] == 0) { g_map[pos.x - 2][pos.y] = 4; //空地变为箱子 g_map[pos.x - 1][pos.y] = 6; //原来箱子位置变为人 if (g_map[pos.x][pos.y] == 9) //还原人的位置 { g_map[pos.x][pos.y] = 3; } else g_map[pos.x][pos.y] = 0; } //b.箱子前面目的地 if (g_map[pos.x - 2][pos.y] == 3) { g_map[pos.x - 2][pos.y] = 7; //目的地变为箱子和目的地重合 g_map[pos.x - 1][pos.y] = 6; //原来箱子位置变为人 if (g_map[pos.x][pos.y] == 9) //还原人的位置 { g_map[pos.x][pos.y] = 3; } else g_map[pos.x][pos.y] = 0; } } //4.人的前面是箱子和目的地的重合 if (g_map[pos.x - 1][pos.y] == 7) { //a.箱子和目的地的重合前面是空地 if (g_map[pos.x - 2][pos.y] == 0) { g_map[pos.x - 2][pos.y] = 4; //空地变为箱子 g_map[pos.x - 1][pos.y] = 9; //原来箱子位置变为人 if (g_map[pos.x][pos.y] == 9) //还原人的位置 { g_map[pos.x][pos.y] = 3; } else g_map[pos.x][pos.y] = 0; } //b.箱子和目的地的重合前面是另一个目的地 if (g_map[pos.x - 2][pos.y] == 3) { g_map[pos.x - 2][pos.y] = 7; //目的地变为箱子和目的地重合 g_map[pos.x - 1][pos.y] = 9; //原来箱子位置变为人 if (g_map[pos.x][pos.y] == 9) //还原人的位置 { g_map[pos.x][pos.y] = 3; } else g_map[pos.x][pos.y] = 0; } } } //下移 void down() { //获取玩家坐标 POINT pos = GetGamerPostion(); //1.人的前面是空地 if (g_map[pos.x + 1][pos.y] == 0) { g_map[pos.x + 1][pos.y] = 6; //空地变为人 if (g_map[pos.x][pos.y] == 9) //还原原来人的位置 { g_map[pos.x][pos.y] = 3; } else g_map[pos.x][pos.y] = 0; } //2.人的前面是目的地 if (g_map[pos.x + 1][pos.y] == 3) { g_map[pos.x + 1][pos.y] = 9; //原来目的地的位置变为人站在目的地 if (g_map[pos.x][pos.y] == 9) //还原人的位置 { g_map[pos.x][pos.y] = 3; } else g_map[pos.x][pos.y] = 0; } //3.人的前面是箱子, if (g_map[pos.x + 1][pos.y] == 4) { //a.箱子前面是空地 if (g_map[pos.x + 2][pos.y] == 0) { g_map[pos.x + 2][pos.y] = 4; //空地变为箱子 g_map[pos.x + 1][pos.y] = 6; //原来箱子位置变为人 if (g_map[pos.x][pos.y] == 9) //还原人的位置 { g_map[pos.x][pos.y] = 3; } else g_map[pos.x][pos.y] = 0; } //b.箱子前面目的地 if (g_map[pos.x + 2][pos.y] == 3) { g_map[pos.x + 2][pos.y] = 7; //目的地变为箱子和目的地重合 g_map[pos.x + 1][pos.y] = 6; //原来箱子位置变为人 if (g_map[pos.x][pos.y] == 9) //还原人的位置 { g_map[pos.x][pos.y] = 3; } else g_map[pos.x][pos.y] = 0; } } //4.人的前面是箱子和目的地的重合 if (g_map[pos.x + 1][pos.y] == 7) { //a.箱子和目的地的重合前面是空地 if (g_map[pos.x + 2][pos.y] == 0) { g_map[pos.x + 2][pos.y] = 4; //空地变为箱子 g_map[pos.x + 1][pos.y] = 9; //原来箱子位置变为人 if (g_map[pos.x][pos.y] == 9) //还原人的位置 { g_map[pos.x][pos.y] = 3; } else g_map[pos.x][pos.y] = 0; } //b.箱子和目的地的重合前面是另一个目的地 if (g_map[pos.x + 2][pos.y] == 3) { g_map[pos.x + 2][pos.y] = 7; //目的地变为箱子和目的地重合 g_map[pos.x + 1][pos.y] = 9; //原来箱子位置变为人 if (g_map[pos.x][pos.y] == 9) //还原人的位置 { g_map[pos.x][pos.y] = 3; } else g_map[pos.x][pos.y] = 0; } } } //右移 void right() { //获取玩家坐标 POINT pos = GetGamerPostion(); //1.人的前面是空地 if (g_map[pos.x][pos.y + 1] == 0) { g_map[pos.x ][pos.y + 1] = 6; //空地变为人 if (g_map[pos.x][pos.y] == 9) //还原原来人的位置 { g_map[pos.x][pos.y] = 3; } else g_map[pos.x][pos.y] = 0; } //2.人的前面是目的地 if (g_map[pos.x][pos.y + 1] == 3) { g_map[pos.x][pos.y + 1] = 9; //原来目的地的位置变为人站在目的地 if (g_map[pos.x][pos.y] == 9) //还原人的位置 { g_map[pos.x][pos.y] = 3; } else g_map[pos.x][pos.y] = 0; } //3.人的前面是箱子, if (g_map[pos.x][pos.y + 1] == 4) { //a.箱子前面是空地 if (g_map[pos.x][pos.y + 2] == 0) { g_map[pos.x][pos.y + 2] = 4; //空地变为箱子 g_map[pos.x][pos.y + 1] = 6; //原来箱子位置变为人 if (g_map[pos.x][pos.y] == 9) //还原人的位置 { g_map[pos.x][pos.y] = 3; } else g_map[pos.x][pos.y] = 0; } //b.箱子前面目的地 if (g_map[pos.x][pos.y + 2] == 3) { g_map[pos.x][pos.y + 2] = 7; //目的地变为箱子和目的地重合 g_map[pos.x][pos.y + 1] = 6; //原来箱子位置变为人 if (g_map[pos.x][pos.y] == 9) //还原人的位置 { g_map[pos.x][pos.y] = 3; } else g_map[pos.x][pos.y] = 0; } } //4.人的前面是箱子和目的地的重合 if (g_map[pos.x][pos.y + 1] == 7) { //a.箱子和目的地的重合前面是空地 if (g_map[pos.x][pos.y + 2] == 0) { g_map[pos.x][pos.y + 2] = 4; //空地变为箱子 g_map[pos.x][pos.y + 1] = 9; //原来箱子位置变为人 if (g_map[pos.x][pos.y] == 9) //还原人的位置 { g_map[pos.x][pos.y] = 3; } else g_map[pos.x][pos.y] = 0; } //b.箱子和目的地的重合前面是另一个目的地 if (g_map[pos.x][pos.y + 2] == 3) { g_map[pos.x][pos.y + 2] = 7; //目的地变为箱子和目的地重合 g_map[pos.x][pos.y + 1] = 9; //原来箱子位置变为人 if (g_map[pos.x][pos.y] == 9) //还原人的位置 { g_map[pos.x][pos.y] = 3; } else g_map[pos.x][pos.y] = 0; } } } //左移 void left() { //获取玩家坐标 POINT pos = GetGamerPostion(); //1.人的前面是空地 if (g_map[pos.x][pos.y - 1] == 0) { g_map[pos.x][pos.y - 1] = 6; //空地变为人 if (g_map[pos.x][pos.y] == 9) //还原原来人的位置 { g_map[pos.x][pos.y] = 3; } else g_map[pos.x][pos.y] = 0; } //2.人的前面是目的地 if (g_map[pos.x][pos.y - 1] == 3) { g_map[pos.x][pos.y - 1] = 9; //原来目的地的位置变为人站在目的地 if (g_map[pos.x][pos.y] == 9) //还原人的位置 { g_map[pos.x][pos.y] = 3; } else g_map[pos.x][pos.y] = 0; } //3.人的前面是箱子, if (g_map[pos.x][pos.y - 1] == 4) { //a.箱子前面是空地 if (g_map[pos.x][pos.y - 2] == 0) { g_map[pos.x][pos.y - 2] = 4; //空地变为箱子 g_map[pos.x][pos.y - 1] = 6; //原来箱子位置变为人 if (g_map[pos.x][pos.y] == 9) //还原人的位置 { g_map[pos.x][pos.y] = 3; } else g_map[pos.x][pos.y] = 0; } //b.箱子前面目的地 if (g_map[pos.x][pos.y - 2] == 3) { g_map[pos.x][pos.y - 2] = 7; //目的地变为箱子和目的地重合 g_map[pos.x][pos.y - 1] = 6; //原来箱子位置变为人 if (g_map[pos.x][pos.y] == 9) //还原人的位置 { g_map[pos.x][pos.y] = 3; } else g_map[pos.x][pos.y] = 0; } } //4.人的前面是箱子和目的地的重合 if (g_map[pos.x][pos.y - 1] == 7) { //a.箱子和目的地的重合前面是空地 if (g_map[pos.x][pos.y - 2] == 0) { g_map[pos.x][pos.y - 2] = 4; //空地变为箱子 g_map[pos.x][pos.y - 1] = 9; //原来箱子位置变为人 if (g_map[pos.x][pos.y] == 9) //还原人的位置 { g_map[pos.x][pos.y] = 3; } else g_map[pos.x][pos.y] = 0; } //b.箱子和目的地的重合前面是另一个目的地 if (g_map[pos.x][pos.y - 2] == 3) { g_map[pos.x][pos.y - 2] = 7; //目的地变为箱子和目的地重合 g_map[pos.x][pos.y - 1] = 9; //原来箱子位置变为人 if (g_map[pos.x][pos.y] == 9) //还原人的位置 { g_map[pos.x][pos.y] = 3; } else g_map[pos.x][pos.y] = 0; } } } //结束游戏 void gameOver() { if (g_map[3][10] == 7 && g_map[4][10]==7 && g_map[5][10]==7) { MessageBox(NULL, L"获得胜利", L"提示",0); } } //寻找玩家位置 POINT GetGamerPostion() { POINT pos = { -1, -1 }; //表示没有找到玩家 for (int i = 0; i < 10; i++) { for (int j = 0; j < 12; j++) { if (g_map[i][j] == 6 || g_map[i][j] == 9) { pos.x = i; pos.y = j; return pos; } } } return pos; }

是不是觉得一点都不美观?和正常的推箱子游戏差太多了?其实很简单,只需要把箱子那些图片准备好,用贴图技术贴在这个框架里就OK啦!

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

原文链接:https://blog.csdn.net/qq_40754866/article/details/104521936

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。