1 #include<iostream> 2 #include<string> 3 #include<ctime> 4 #include<cstdlib> 5 #include<windows.h> 6 #include<conio.h> 7 8 using namespace std; 9 10 int block00[4][4] = { { 10,0,0,0 },{ 1,1,1,1 },{ 0,0,0,0 },{ 0,0,0,0 } }; 11 int block01[4][4] = { { 11,0,1,0 },{ 0,0,1,0 },{ 0,0,1,0 },{ 0,0,1,0 } }; 12 int block02[4][4] = { { 12,0,0,0 },{ 0,0,0,0 },{ 1,1,1,0 },{ 0,1,0,0 } }; 13 int block03[4][4] = { { 13,0,0,0 },{ 0,1,0,0 },{ 1,1,0,0 },{ 0,1,0,0 } }; 14 int block04[4][4] = { { 14,0,0,0 },{ 0,0,0,0 },{ 0,1,0,0 },{ 1,1,1,0 } }; 15 int block05[4][4] = { { 15,0,0,0 },{ 0,1,0,0 },{ 0,1,1,0 },{ 0,1,0,0 } }; 16 int block06[4][4] = { { 16,0,0,0 },{ 0,0,0,0 },{ 1,1,1,0 },{ 1,0,0,0 } }; 17 int block07[4][4] = { { 17,0,0,0 },{ 1,1,0,0 },{ 0,1,0,0 },{ 0,1,0,0 } }; 18 int block08[4][4] = { { 18,0,0,0 },{ 0,0,0,0 },{ 0,0,1,0 },{ 1,1,1,0 } }; 19 int block09[4][4] = { { 19,0,0,0 },{ 0,1,0,0 },{ 0,1,0,0 },{ 0,1,1,0 } }; 20 int block10[4][4] = { { 20,0,0,0 },{ 0,0,0,0 },{ 1,1,1,0 },{ 0,0,1,0 } }; 21 int block11[4][4] = { { 21,0,0,0 },{ 0,1,0,0 },{ 0,1,0,0 },{ 1,1,0,0 } }; 22 int block12[4][4] = { { 22,0,0,0 },{ 0,0,0,0 },{ 1,0,0,0 },{ 1,1,1,0 } }; 23 int block13[4][4] = { { 23,0,0,0 },{ 0,1,1,0 },{ 0,1,0,0 },{ 0,1,0,0 } }; 24 int block14[4][4] = { { 24,0,0,0 },{ 0,0,0,0 },{ 0,1,1,0 },{ 1,1,0,0 } }; 25 int block15[4][4] = { { 25,0,0,0 },{ 1,0,0,0 },{ 1,1,0,0 },{ 0,1,0,0 } }; 26 int block16[4][4] = { { 26,0,0,0 },{ 0,0,0,0 },{ 1,1,0,0 },{ 0,1,1,0 } }; 27 int block17[4][4] = { { 27,0,0,0 },{ 0,0,1,0 },{ 0,1,1,0 },{ 0,1,0,0 } }; 28 int block18[4][4] = { { 28,0,0,0 },{ 0,0,0,0 },{ 1,1,0,0 },{ 1,1,0,0 } }; 29 30 void initialWindow(HANDLE hOut);//初始化窗口 31 void initialPrint(HANDLE hOut);//初始化界面 32 void gotoXY(HANDLE hOut, int x, int y);//移动光标 33 void roundBlock(HANDLE hOut, int block[4][4]);//随机生成方块并打印到下一个方块位置 34 bool collisionDetection(int block[4][4], int map[21][12], int x, int y);//检测碰撞 35 void printBlock(HANDLE hOut, int block[4][4], int x, int y);//打印方块 36 void clearBlock(HANDLE hOut, int block[4][4], int x, int y);//消除方块 37 void myLeft(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y);//左移 38 void myRight(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y);//右移 39 void myUp(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y);//顺时针旋转90度 40 int myDown(HANDLE hOut, int block[4][4], int map[21][12], int &x, int y);//加速下落 41 void myStop(HANDLE hOut, int block[4][4]);//游戏暂停 42 void gameOver(HANDLE hOut, int block[4][4], int map[21][12]);//游戏结束 43 void eliminateRow(HANDLE hOut, int map[21][12], int &val, int &fraction, int &checkpoint);//判断是否能消行并更新分值 44 int main() 45 { 46 int map[21][12]; 47 int blockA[4][4];//候选区的方块 48 int blockB[4][4];//下落中的方块 49 int positionX, positionY;//方块左上角的坐标 50 bool check;//检查方块还能不能下落 51 char key;//用来存储按键 52 int val;//用来控制下落速度 53 int fraction;//用来存储得分 54 int checkpoint;//用来存储关卡 55 int times; 56 HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);//获取标准输出设备句柄 57 initialWindow(hOut); 58 initial: 59 gotoXY(hOut, 0, 0); 60 initialPrint(hOut); 61 check = true; 62 val = 50; 63 fraction = 0; 64 checkpoint = 1; 65 times = val; 66 for (int i = 0; i < 20; ++i) 67 { 68 for (int j = 1; j < 11; ++j) 69 { 70 map[i][j] = 0; 71 } 72 } 73 for (int i = 0; i < 20; ++i) 74 { 75 map[i][0] = map[i][11] = 1; 76 } 77 for (int i = 0; i < 12; ++i) 78 { 79 map[20][i] = 1; 80 } 81 82 srand((unsigned)time(NULL)); 83 roundBlock(hOut, blockA); 84 while (true) 85 { 86 if (check) 87 { 88 eliminateRow(hOut, map, val, fraction, checkpoint); 89 check = false; 90 positionX = -3; 91 positionY = 4; 92 if (collisionDetection(blockA, map, positionX, positionY)) 93 { 94 for (int i = 0; i < 4; ++i) 95 { 96 for (int j = 0; j < 4; ++j) 97 { 98 blockB[i][j] = blockA[i][j]; 99 } 100 } 101 roundBlock(hOut, blockA); 102 } 103 else 104 { 105 gameOver(hOut, blockA, map); 106 goto initial; 107 } 108 } 109 printBlock(hOut, blockB, positionX, positionY); 110 if (_kbhit()) 111 { 112 key = _getch(); 113 switch (key) 114 { 115 case 72: 116 myUp(hOut, blockB, map, positionX, positionY); 117 break; 118 case 75: 119 myLeft(hOut, blockB, map, positionX, positionY); 120 break; 121 case 77: 122 myRight(hOut, blockB, map, positionX, positionY); 123 break; 124 case 80: 125 switch (myDown(hOut, blockB, map, positionX, positionY)) 126 { 127 case 0: 128 check = false; 129 break; 130 case 1: 131 check = true; 132 break; 133 case 2: 134 gameOver(hOut, blockB, map); 135 goto initial; 136 default: 137 break; 138 } 139 break; 140 case 32: 141 myStop(hOut, blockA); 142 break; 143 case 27: 144 exit(0); 145 default: 146 break; 147 } 148 } 149 Sleep(20); 150 if (0 == --times) 151 { 152 switch (myDown(hOut, blockB, map, positionX, positionY)) 153 { 154 case 0: 155 check = false; 156 break; 157 case 1: 158 check = true; 159 break; 160 case 2: 161 gameOver(hOut, blockB, map); 162 goto initial; 163 default: 164 break; 165 } 166 times = val; 167 } 168 } 169 cin.get(); 170 return 0; 171 } 172 173 void initialWindow(HANDLE hOut) 174 { 175 SetConsoleTitle("俄罗斯方块"); 176 COORD size = { 80, 25 }; 177 SetConsoleScreenBufferSize(hOut, size); 178 SMALL_RECT rc = { 0, 0, 79, 24 }; 179 SetConsoleWindowInfo(hOut, true, &rc); 180 CONSOLE_CURSOR_INFO cursor_info = { 1, 0 }; 181 SetConsoleCursorInfo(hOut, &cursor_info); 182 } 183 184 void initialPrint(HANDLE hOut) 185 { 186 SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); 187 for (int i = 0; i < 20; ++i) 188 { 189 cout << "■ ■☆ ☆" << endl; 190 } 191 gotoXY(hOut, 26, 0); 192 cout << "☆☆☆☆☆☆☆☆☆☆☆"; 193 gotoXY(hOut, 0, 20); 194 cout << "■■■■■■■■■■■■☆☆☆☆☆☆☆☆☆☆☆☆☆"; 195 gotoXY(hOut, 26, 1); 196 cout << "分 数: "; 197 gotoXY(hOut, 26, 2); 198 cout << "关 卡: "; 199 gotoXY(hOut, 26, 4); 200 cout << "下一方块:"; 201 gotoXY(hOut, 26, 9); 202 cout << "操作方法:"; 203 gotoXY(hOut, 30, 11); 204 cout << "↑:旋转 ↓:速降"; 205 gotoXY(hOut, 30, 12); 206 cout << "→:右移 ←:左移"; 207 gotoXY(hOut, 30, 13); 208 cout << "空格键:开始/暂停"; 209 gotoXY(hOut, 30, 14); 210 cout << "Esc 键:退出"; 211 gotoXY(hOut, 26, 16); 212 cout << "关 于:"; 213 gotoXY(hOut, 30, 18); 214 cout << "俄罗斯方块V1.0"; 215 gotoXY(hOut, 35, 19); 216 cout << "作者:潘约尔"; 217 } 218 219 void gotoXY(HANDLE hOut, int x, int y) 220 { 221 COORD pos; 222 pos.X = x; 223 pos.Y = y; 224 SetConsoleCursorPosition(hOut, pos); 225 } 226 227 void roundBlock(HANDLE hOut, int block[4][4]) 228 { 229 clearBlock(hOut, block, 5, 15); 230 switch (rand() % 19) 231 { 232 case 0: 233 for (int i = 0; i < 4; ++i) 234 { 235 for (int j = 0; j < 4; ++j) 236 { 237 block[i][j] = block00[i][j]; 238 } 239 } 240 break; 241 case 1: 242 for (int i = 0; i < 4; ++i) 243 { 244 for (int j = 0; j < 4; ++j) 245 { 246 block[i][j] = block01[i][j]; 247 } 248 } 249 break; 250 case 2: 251 for (int i = 0; i < 4; ++i) 252 { 253 for (int j = 0; j < 4; ++j) 254 { 255 block[i][j] = block02[i][j]; 256 } 257 } 258 break; 259 case 3: 260 for (int i = 0; i < 4; ++i) 261 { 262 for (int j = 0; j < 4; ++j) 263 { 264 block[i][j] = block03[i][j]; 265 } 266 } 267 break; 268 case 4: 269 for (int i = 0; i < 4; ++i) 270 { 271 for (int j = 0; j < 4; ++j) 272 { 273 block[i][j] = block04[i][j]; 274 } 275 } 276 break; 277 case 5: 278 for (int i = 0; i < 4; ++i) 279 { 280 for (int j = 0; j < 4; ++j) 281 { 282 block[i][j] = block05[i][j]; 283 } 284 } 285 break; 286 case 6: 287 for (int i = 0; i < 4; ++i) 288 { 289 for (int j = 0; j < 4; ++j) 290 { 291 block[i][j] = block06[i][j]; 292 } 293 } 294 break; 295 case 7: 296 for (int i = 0; i < 4; ++i) 297 { 298 for (int j = 0; j < 4; ++j) 299 { 300 block[i][j] = block07[i][j]; 301 } 302 } 303 break; 304 case 8: 305 for (int i = 0; i < 4; ++i) 306 { 307 for (int j = 0; j < 4; ++j) 308 { 309 block[i][j] = block08[i][j]; 310 } 311 } 312 break; 313 case 9: 314 for (int i = 0; i < 4; ++i) 315 { 316 for (int j = 0; j < 4; ++j) 317 { 318 block[i][j] = block09[i][j]; 319 } 320 } 321 break; 322 case 10: 323 for (int i = 0; i < 4; ++i) 324 { 325 for (int j = 0; j < 4; ++j) 326 { 327 block[i][j] = block10[i][j]; 328 } 329 } 330 break; 331 case 11: 332 for (int i = 0; i < 4; ++i) 333 { 334 for (int j = 0; j < 4; ++j) 335 { 336 block[i][j] = block11[i][j]; 337 } 338 } 339 break; 340 case 12: 341 for (int i = 0; i < 4; ++i) 342 { 343 for (int j = 0; j < 4; ++j) 344 { 345 block[i][j] = block12[i][j]; 346 } 347 } 348 break; 349 case 13: 350 for (int i = 0; i < 4; ++i) 351 { 352 for (int j = 0; j < 4; ++j) 353 { 354 block[i][j] = block13[i][j]; 355 } 356 } 357 break; 358 case 14: 359 for (int i = 0; i < 4; ++i) 360 { 361 for (int j = 0; j < 4; ++j) 362 { 363 block[i][j] = block14[i][j]; 364 } 365 } 366 break; 367 case 15: 368 for (int i = 0; i < 4; ++i) 369 { 370 for (int j = 0; j < 4; ++j) 371 { 372 block[i][j] = block15[i][j]; 373 } 374 } 375 break; 376 case 16: 377 for (int i = 0; i < 4; ++i) 378 { 379 for (int j = 0; j < 4; ++j) 380 { 381 block[i][j] = block16[i][j]; 382 } 383 } 384 break; 385 case 17: 386 for (int i = 0; i < 4; ++i) 387 { 388 for (int j = 0; j < 4; ++j) 389 { 390 block[i][j] = block17[i][j]; 391 } 392 } 393 break; 394 case 18: 395 for (int i = 0; i < 4; ++i) 396 { 397 for (int j = 0; j < 4; ++j) 398 { 399 block[i][j] = block18[i][j]; 400 } 401 } 402 break; 403 default: 404 break; 405 } 406 printBlock(hOut, block, 5, 15); 407 } 408 409 bool collisionDetection(int block[4][4], int map[21][12], int x, int y) 410 { 411 for (int i = 0; i < 4; ++i) 412 { 413 for (int j = 0; j < 4; ++j) 414 { 415 if (x + i >= 0 && y + j >= 0 && map[x + i][y + j] == 1 && block[i][j] == 1) 416 { 417 return false; 418 } 419 } 420 } 421 return true; 422 } 423 424 void printBlock(HANDLE hOut, int block[4][4], int x, int y) 425 { 426 switch (block[0][0]) 427 { 428 case 10: 429 case 11: 430 SetConsoleTextAttribute(hOut, FOREGROUND_GREEN); 431 break; 432 case 12: 433 case 13: 434 case 14: 435 case 15: 436 SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY); 437 break; 438 case 16: 439 case 17: 440 case 18: 441 case 19: 442 SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY); 443 break; 444 case 20: 445 case 21: 446 case 22: 447 case 23: 448 SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY); 449 break; 450 case 24: 451 case 25: 452 SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY); 453 break; 454 case 26: 455 case 27: 456 SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_INTENSITY); 457 break; 458 case 28: 459 SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY); 460 break; 461 default: 462 break; 463 } 464 for (int i = 0; i < 4; ++i) 465 { 466 if (i + x >= 0) 467 { 468 for (int j = 0; j < 4; ++j) 469 { 470 if (block[i][j] == 1) 471 { 472 473 gotoXY(hOut, 2 * (y + j), x + i); 474 cout << "■"; 475 } 476 } 477 } 478 } 479 } 480 481 void clearBlock(HANDLE hOut, int block[4][4], int x, int y) 482 { 483 for (int i = 0; i < 4; ++i) 484 { 485 if (i + x >= 0) 486 { 487 for (int j = 0; j < 4; ++j) 488 { 489 if (block[i][j] == 1) 490 { 491 gotoXY(hOut, 2 * (y + j), x + i); 492 cout << " "; 493 } 494 } 495 } 496 } 497 } 498 499 void gameOver(HANDLE hOut, int block[4][4], int map[21][12]) 500 { 501 SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY); 502 gotoXY(hOut, 9, 8); 503 cout << "GAME OVER"; 504 gotoXY(hOut, 8, 9); 505 cout << "空格键:重来"; 506 gotoXY(hOut, 8, 10); 507 cout << "ESC键:退出"; 508 char key; 509 while (true) 510 { 511 key = _getch(); 512 if (key == 32) 513 { 514 return; 515 } 516 if (key == 27) 517 { 518 exit(0); 519 } 520 } 521 } 522 523 int myDown(HANDLE hOut, int block[4][4], int map[21][12], int &x, int y) 524 { 525 if (collisionDetection(block, map, x + 1, y)) 526 { 527 clearBlock(hOut, block, x, y); 528 ++x; 529 return 0; 530 } 531 if (x < 0) 532 { 533 return 2; 534 } 535 for (int i = 0; i < 4; ++i) 536 { 537 for (int j = 0; j < 4; ++j) 538 { 539 if (block[i][j] == 1) 540 { 541 map[x + i][y + j] = 1; 542 SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY); 543 gotoXY(hOut, 2 * (y + j), x + i); 544 cout << "■"; 545 } 546 } 547 } 548 return 1; 549 } 550 551 void myLeft(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y) 552 { 553 if (collisionDetection(block, map, x, y - 1)) 554 { 555 clearBlock(hOut, block, x, y); 556 --y; 557 } 558 } 559 560 void myRight(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y) 561 { 562 if (collisionDetection(block, map, x, y + 1)) 563 { 564 clearBlock(hOut, block, x, y); 565 ++y; 566 } 567 } 568 569 void myUp(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y) 570 { 571 switch (block[0][0]) 572 { 573 case 10: 574 if (collisionDetection(block01, map, x, y)) 575 { 576 clearBlock(hOut, block, x, y); 577 for (int i = 0; i < 4; ++i) 578 { 579 for (int j = 0; j < 4; ++j) 580 { 581 block[i][j] = block01[i][j]; 582 } 583 } 584 } 585 break; 586 case 11: 587 if (collisionDetection(block00, map, x, y)) 588 { 589 clearBlock(hOut, block, x, y); 590 for (int i = 0; i < 4; ++i) 591 { 592 for (int j = 0; j < 4; ++j) 593 { 594 block[i][j] = block00[i][j]; 595 } 596 } 597 } 598 else if (collisionDetection(block00, map, x, y - 1)) 599 { 600 clearBlock(hOut, block, x, y); 601 for (int i = 0; i < 4; ++i) 602 { 603 for (int j = 0; j < 4; ++j) 604 { 605 block[i][j] = block00[i][j]; 606 } 607 } 608 --y; 609 } 610 else if (collisionDetection(block00, map, x, y + 1)) 611 { 612 clearBlock(hOut, block, x, y); 613 for (int i = 0; i < 4; ++i) 614 { 615 for (int j = 0; j < 4; ++j) 616 { 617 block[i][j] = block00[i][j]; 618 } 619 } 620 ++y; 621 } 622 else if (collisionDetection(block00, map, x, y - 2)) 623 { 624 clearBlock(hOut, block, x, y); 625 for (int i = 0; i < 4; ++i) 626 { 627 for (int j = 0; j < 4; ++j) 628 { 629 block[i][j] = block00[i][j]; 630 } 631 } 632 y = y - 2; 633 } 634 else if (collisionDetection(block00, map, x, y + 2)) 635 { 636 clearBlock(hOut, block, x, y); 637 for (int i = 0; i < 4; ++i) 638 { 639 for (int j = 0; j < 4; ++j) 640 { 641 block[i][j] = block00[i][j]; 642 } 643 } 644 y = y + 2; 645 } 646 break; 647 case 12: 648 if (collisionDetection(block03, map, x, y)) 649 { 650 clearBlock(hOut, block, x, y); 651 for (int i = 0; i < 4; ++i) 652 { 653 for (int j = 0; j < 4; ++j) 654 { 655 block[i][j] = block03[i][j]; 656 } 657 } 658 } 659 else if (collisionDetection(block03, map, x, y - 1)) 660 { 661 clearBlock(hOut, block, x, y); 662 for (int i = 0; i < 4; ++i) 663 { 664 for (int j = 0; j < 4; ++j) 665 { 666 block[i][j] = block03[i][j]; 667 } 668 } 669 --y; 670 } 671 else if (collisionDetection(block03, map, x, y + 1)) 672 { 673 clearBlock(hOut, block, x, y); 674 for (int i = 0; i < 4; ++i) 675 { 676 for (int j = 0; j < 4; ++j) 677 { 678 block[i][j] = block03[i][j]; 679 } 680 } 681 ++y; 682 } 683 break; 684 case 13: 685 if (collisionDetection(block04, map, x, y)) 686 { 687 clearBlock(hOut, block, x, y); 688 for (int i = 0; i < 4; ++i) 689 { 690 for (int j = 0; j < 4; ++j) 691 { 692 block[i][j] = block04[i][j]; 693 } 694 } 695 } 696 else if (collisionDetection(block04, map, x, y - 1)) 697 { 698 clearBlock(hOut, block, x, y); 699 for (int i = 0; i < 4; ++i) 700 { 701 for (int j = 0; j < 4; ++j) 702 { 703 block[i][j] = block04[i][j]; 704 } 705 } 706 --y; 707 } 708 else if (collisionDetection(block04, map, x, y + 1)) 709 { 710 clearBlock(hOut, block, x, y); 711 for (int i = 0; i < 4; ++i) 712 { 713 for (int j = 0; j < 4; ++j) 714 { 715 block[i][j] = block04[i][j]; 716 } 717 } 718 ++y; 719 } 720 break; 721 case 14: 722 if (collisionDetection(block05, map, x, y)) 723 { 724 clearBlock(hOut, block, x, y); 725 for (int i = 0; i < 4; ++i) 726 { 727 for (int j = 0; j < 4; ++j) 728 { 729 block[i][j] = block05[i][j]; 730 } 731 } 732 } 733 else if (collisionDetection(block05, map, x, y - 1)) 734 { 735 clearBlock(hOut, block, x, y); 736 for (int i = 0; i < 4; ++i) 737 { 738 for (int j = 0; j < 4; ++j) 739 { 740 block[i][j] = block05[i][j]; 741 } 742 } 743 --y; 744 } 745 else if (collisionDetection(block05, map, x, y + 1)) 746 { 747 clearBlock(hOut, block, x, y); 748 for (int i = 0; i < 4; ++i) 749 { 750 for (int j = 0; j < 4; ++j) 751 { 752 block[i][j] = block05[i][j]; 753 } 754 } 755 ++y; 756 } 757 break; 758 case 15: 759 if (collisionDetection(block02, map, x, y)) 760 { 761 clearBlock(hOut, block, x, y); 762 for (int i = 0; i < 4; ++i) 763 { 764 for (int j = 0; j < 4; ++j) 765 { 766 block[i][j] = block02[i][j]; 767 } 768 } 769 } 770 else if (collisionDetection(block02, map, x, y - 1)) 771 { 772 clearBlock(hOut, block, x, y); 773 for (int i = 0; i < 4; ++i) 774 { 775 for (int j = 0; j < 4; ++j) 776 { 777 block[i][j] = block02[i][j]; 778 } 779 } 780 --y; 781 } 782 else if (collisionDetection(block02, map, x, y + 1)) 783 { 784 clearBlock(hOut, block, x, y); 785 for (int i = 0; i < 4; ++i) 786 { 787 for (int j = 0; j < 4; ++j) 788 { 789 block[i][j] = block02[i][j]; 790 } 791 } 792 ++y; 793 } 794 break; 795 796 case 16: 797 if (collisionDetection(block07, map, x, y)) 798 { 799 clearBlock(hOut, block, x, y); 800 for (int i = 0; i < 4; ++i) 801 { 802 for (int j = 0; j < 4; ++j) 803 { 804 block[i][j] = block07[i][j]; 805 } 806 } 807 } 808 else if (collisionDetection(block07, map, x, y - 1)) 809 { 810 clearBlock(hOut, block, x, y); 811 for (int i = 0; i < 4; ++i) 812 { 813 for (int j = 0; j < 4; ++j) 814 { 815 block[i][j] = block07[i][j]; 816 } 817 } 818 --y; 819 } 820 else if (collisionDetection(block07, map, x, y + 1)) 821 { 822 clearBlock(hOut, block, x, y); 823 for (int i = 0; i < 4; ++i) 824 { 825 for (int j = 0; j < 4; ++j) 826 { 827 block[i][j] = block07[i][j]; 828 } 829 } 830 ++y; 831 } 832 break; 833 case 17: 834 if (collisionDetection(block08, map, x, y)) 835 { 836 clearBlock(hOut, block, x, y); 837 for (int i = 0; i < 4; ++i) 838 { 839 for (int j = 0; j < 4; ++j) 840 { 841 block[i][j] = block08[i][j]; 842 } 843 } 844 } 845 else if (collisionDetection(block08, map, x, y - 1)) 846 { 847 clearBlock(hOut, block, x, y); 848 for (int i = 0; i < 4; ++i) 849 { 850 for (int j = 0; j < 4; ++j) 851 { 852 block[i][j] = block08[i][j]; 853 } 854 } 855 --y; 856 } 857 else if (collisionDetection(block08, map, x, y + 1)) 858 { 859 clearBlock(hOut, block, x, y); 860 for (int i = 0; i < 4; ++i) 861 { 862 for (int j = 0; j < 4; ++j) 863 { 864 block[i][j] = block08[i][j]; 865 } 866 } 867 ++y; 868 } 869 break; 870 case 18: 871 if (collisionDetection(block09, map, x, y)) 872 { 873 clearBlock(hOut, block, x, y); 874 for (int i = 0; i < 4; ++i) 875 { 876 for (int j = 0; j < 4; ++j) 877 { 878 block[i][j] = block09[i][j]; 879 } 880 } 881 } 882 else if (collisionDetection(block09, map, x, y - 1)) 883 { 884 clearBlock(hOut, block, x, y); 885 for (int i = 0; i < 4; ++i) 886 { 887 for (int j = 0; j < 4; ++j) 888 { 889 block[i][j] = block09[i][j]; 890 } 891 } 892 --y; 893 } 894 else if (collisionDetection(block09, map, x, y + 1)) 895 { 896 clearBlock(hOut, block, x, y); 897 for (int i = 0; i < 4; ++i) 898 { 899 for (int j = 0; j < 4; ++j) 900 { 901 block[i][j] = block09[i][j]; 902 } 903 } 904 ++y; 905 } 906 break; 907 case 19: 908 if (collisionDetection(block06, map, x, y)) 909 { 910 clearBlock(hOut, block, x, y); 911 for (int i = 0; i < 4; ++i) 912 { 913 for (int j = 0; j < 4; ++j) 914 { 915 block[i][j] = block06[i][j]; 916 } 917 } 918 } 919 else if (collisionDetection(block06, map, x, y - 1)) 920 { 921 clearBlock(hOut, block, x, y); 922 for (int i = 0; i < 4; ++i) 923 { 924 for (int j = 0; j < 4; ++j) 925 { 926 block[i][j] = block06[i][j]; 927 } 928 } 929 --y; 930 } 931 else if (collisionDetection(block06, map, x, y + 1)) 932 { 933 clearBlock(hOut, block, x, y); 934 for (int i = 0; i < 4; ++i) 935 { 936 for (int j = 0; j < 4; ++j) 937 { 938 block[i][j] = block06[i][j]; 939 } 940 } 941 ++y; 942 } 943 break; 944 case 20: 945 if (collisionDetection(block11, map, x, y)) 946 { 947 clearBlock(hOut, block, x, y); 948 for (int i = 0; i < 4; ++i) 949 { 950 for (int j = 0; j < 4; ++j) 951 { 952 block[i][j] = block11[i][j]; 953 } 954 } 955 } 956 else if (collisionDetection(block11, map, x, y - 1)) 957 { 958 clearBlock(hOut, block, x, y); 959 for (int i = 0; i < 4; ++i) 960 { 961 for (int j = 0; j < 4; ++j) 962 { 963 block[i][j] = block11[i][j]; 964 } 965 } 966 --y; 967 } 968 else if (collisionDetection(block11, map, x, y + 1)) 969 { 970 clearBlock(hOut, block, x, y); 971 for (int i = 0; i < 4; ++i) 972 { 973 for (int j = 0; j < 4; ++j) 974 { 975 block[i][j] = block11[i][j]; 976 } 977 } 978 ++y; 979 } 980 break; 981 case 21: 982 if (collisionDetection(block12, map, x, y)) 983 { 984 clearBlock(hOut, block, x, y); 985 for (int i = 0; i < 4; ++i) 986 { 987 for (int j = 0; j < 4; ++j) 988 { 989 block[i][j] = block12[i][j]; 990 } 991 } 992 } 993 else if (collisionDetection(block12, map, x, y - 1)) 994 { 995 clearBlock(hOut, block, x, y); 996 for (int i = 0; i < 4; ++i) 997 { 998 for (int j = 0; j < 4; ++j) 999 { 1000 block[i][j] = block12[i][j]; 1001 } 1002 } 1003 --y; 1004 } 1005 else if (collisionDetection(block12, map, x, y + 1)) 1006 { 1007 clearBlock(hOut, block, x, y); 1008 for (int i = 0; i < 4; ++i) 1009 { 1010 for (int j = 0; j < 4; ++j) 1011 { 1012 block[i][j] = block12[i][j]; 1013 } 1014 } 1015 ++y; 1016 } 1017 break; 1018 case 22: 1019 if (collisionDetection(block13, map, x, y)) 1020 { 1021 clearBlock(hOut, block, x, y); 1022 for (int i = 0; i < 4; ++i) 1023 { 1024 for (int j = 0; j < 4; ++j) 1025 { 1026 block[i][j] = block13[i][j]; 1027 } 1028 } 1029 } 1030 else if (collisionDetection(block13, map, x, y - 1)) 1031 { 1032 clearBlock(hOut, block, x, y); 1033 for (int i = 0; i < 4; ++i) 1034 { 1035 for (int j = 0; j < 4; ++j) 1036 { 1037 block[i][j] = block13[i][j]; 1038 } 1039 } 1040 --y; 1041 } 1042 else if (collisionDetection(block13, map, x, y + 1)) 1043 { 1044 clearBlock(hOut, block, x, y); 1045 for (int i = 0; i < 4; ++i) 1046 { 1047 for (int j = 0; j < 4; ++j) 1048 { 1049 block[i][j] = block13[i][j]; 1050 } 1051 } 1052 ++y; 1053 } 1054 break; 1055 case 23: 1056 if (collisionDetection(block10, map, x, y)) 1057 { 1058 clearBlock(hOut, block, x, y); 1059 for (int i = 0; i < 4; ++i) 1060 { 1061 for (int j = 0; j < 4; ++j) 1062 { 1063 block[i][j] = block10[i][j]; 1064 } 1065 } 1066 } 1067 else if (collisionDetection(block10, map, x, y - 1)) 1068 { 1069 clearBlock(hOut, block, x, y); 1070 for (int i = 0; i < 4; ++i) 1071 { 1072 for (int j = 0; j < 4; ++j) 1073 { 1074 block[i][j] = block10[i][j]; 1075 } 1076 } 1077 --y; 1078 } 1079 else if (collisionDetection(block10, map, x, y + 1)) 1080 { 1081 clearBlock(hOut, block, x, y); 1082 for (int i = 0; i < 4; ++i) 1083 { 1084 for (int j = 0; j < 4; ++j) 1085 { 1086 block[i][j] = block10[i][j]; 1087 } 1088 } 1089 ++y; 1090 } 1091 break; 1092 case 24: 1093 if (collisionDetection(block15, map, x, y)) 1094 { 1095 clearBlock(hOut, block, x, y); 1096 for (int i = 0; i < 4; ++i) 1097 { 1098 for (int j = 0; j < 4; ++j) 1099 { 1100 block[i][j] = block15[i][j]; 1101 } 1102 } 1103 } 1104 else if (collisionDetection(block15, map, x, y - 1)) 1105 { 1106 clearBlock(hOut, block, x, y); 1107 for (int i = 0; i < 4; ++i) 1108 { 1109 for (int j = 0; j < 4; ++j) 1110 { 1111 block[i][j] = block15[i][j]; 1112 } 1113 } 1114 --y; 1115 } 1116 else if (collisionDetection(block15, map, x, y + 1)) 1117 { 1118 clearBlock(hOut, block, x, y); 1119 for (int i = 0; i < 4; ++i) 1120 { 1121 for (int j = 0; j < 4; ++j) 1122 { 1123 block[i][j] = block15[i][j]; 1124 } 1125 } 1126 ++y; 1127 } 1128 break; 1129 case 25: 1130 if (collisionDetection(block14, map, x, y)) 1131 { 1132 clearBlock(hOut, block, x, y); 1133 for (int i = 0; i < 4; ++i) 1134 { 1135 for (int j = 0; j < 4; ++j) 1136 { 1137 block[i][j] = block14[i][j]; 1138 } 1139 } 1140 } 1141 else if (collisionDetection(block14, map, x, y - 1)) 1142 { 1143 clearBlock(hOut, block, x, y); 1144 for (int i = 0; i < 4; ++i) 1145 { 1146 for (int j = 0; j < 4; ++j) 1147 { 1148 block[i][j] = block14[i][j]; 1149 } 1150 } 1151 --y; 1152 } 1153 else if (collisionDetection(block14, map, x, y + 1)) 1154 { 1155 clearBlock(hOut, block, x, y); 1156 for (int i = 0; i < 4; ++i) 1157 { 1158 for (int j = 0; j < 4; ++j) 1159 { 1160 block[i][j] = block14[i][j]; 1161 } 1162 } 1163 ++y; 1164 } 1165 break; 1166 case 26: 1167 if (collisionDetection(block17, map, x, y)) 1168 { 1169 clearBlock(hOut, block, x, y); 1170 for (int i = 0; i < 4; ++i) 1171 { 1172 for (int j = 0; j < 4; ++j) 1173 { 1174 block[i][j] = block17[i][j]; 1175 } 1176 } 1177 } 1178 else if (collisionDetection(block17, map, x, y - 1)) 1179 { 1180 clearBlock(hOut, block, x, y); 1181 for (int i = 0; i < 4; ++i) 1182 { 1183 for (int j = 0; j < 4; ++j) 1184 { 1185 block[i][j] = block17[i][j]; 1186 } 1187 } 1188 --y; 1189 } 1190 else if (collisionDetection(block17, map, x, y + 1)) 1191 { 1192 clearBlock(hOut, block, x, y); 1193 for (int i = 0; i < 4; ++i) 1194 { 1195 for (int j = 0; j < 4; ++j) 1196 { 1197 block[i][j] = block17[i][j]; 1198 } 1199 } 1200 ++y; 1201 } 1202 break; 1203 case 27: 1204 if (collisionDetection(block16, map, x, y)) 1205 { 1206 clearBlock(hOut, block, x, y); 1207 for (int i = 0; i < 4; ++i) 1208 { 1209 for (int j = 0; j < 4; ++j) 1210 { 1211 block[i][j] = block16[i][j]; 1212 } 1213 } 1214 } 1215 else if (collisionDetection(block16, map, x, y - 1)) 1216 { 1217 clearBlock(hOut, block, x, y); 1218 for (int i = 0; i < 4; ++i) 1219 { 1220 for (int j = 0; j < 4; ++j) 1221 { 1222 block[i][j] = block16[i][j]; 1223 } 1224 } 1225 --y; 1226 } 1227 else if (collisionDetection(block16, map, x, y + 1)) 1228 { 1229 clearBlock(hOut, block, x, y); 1230 for (int i = 0; i < 4; ++i) 1231 { 1232 for (int j = 0; j < 4; ++j) 1233 { 1234 block[i][j] = block16[i][j]; 1235 } 1236 } 1237 ++y; 1238 } 1239 break; 1240 default: 1241 break; 1242 } 1243 } 1244 1245 void myStop(HANDLE hOut, int block[4][4]) 1246 { 1247 clearBlock(hOut, block, 5, 15); 1248 SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY); 1249 gotoXY(hOut, 30, 7); 1250 cout << "游戏暂停"; 1251 char key; 1252 while (true) 1253 { 1254 key = _getch(); 1255 if (key == 32) 1256 { 1257 gotoXY(hOut, 30, 7); 1258 cout << " "; 1259 printBlock(hOut, block, 5, 15); 1260 return; 1261 } 1262 if (key == 27) 1263 { 1264 exit(0); 1265 } 1266 } 1267 } 1268 1269 void eliminateRow(HANDLE hOut, int map[21][12], int &val, int &fraction, int &checkpoint) 1270 { 1271 SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY); 1272 for (int i = 19; i >= 0; --i) 1273 { 1274 int x = 0; 1275 for (int j = 1; j < 11; ++j) 1276 { 1277 x += map[i][j]; 1278 } 1279 if (x == 10) 1280 { 1281 fraction += 100; 1282 if (val > 1 && fraction / 1000 + 1 != checkpoint) 1283 { 1284 checkpoint = fraction / 1000 + 1; 1285 val -= 5; 1286 } 1287 for (int m = i; m > 0; --m) 1288 { 1289 for (int n = 1; n < 11; ++n) 1290 { 1291 map[m][n] = map[m - 1][n]; 1292 gotoXY(hOut, 2 * n, m); 1293 if (map[m][n] == 1) 1294 { 1295 cout << "■"; 1296 } 1297 else 1298 { 1299 cout << " "; 1300 } 1301 } 1302 } 1303 ++i; 1304 } 1305 } 1306 gotoXY(hOut, 36, 1); 1307 cout << fraction; 1308 gotoXY(hOut, 36, 2); 1309 cout << checkpoint; 1310 }
enjoy:)