// a= height b=width 把些没影响的功能删了(小花,说明) // #include "stdio.h" #include"stdlib.h" #include"windows.h" #include"time.h" #include"conio.h" int color(int c); //文字颜色 void gotoxy(int x, int y); void DrawGameFrame(); void Flag(struct Tetris*); void MakeTetris(struct Tetris*); void PrintTetris(struct Tetris*); void CleanTetris(struct Tetris*); int ifMove(struct Tetris*); void Del_fullline(struct Tetris*); void gameplay(); void regulation(); void explation(); void welcome(); void Replay(struct Tetris*tetris); void title(); void close(); #define FrameX 13 #define FrameY 3 #define Frame_a 20 #define Frame_b 18 int i, j, Temp, Temp2, Temp1; //temp0初始化为无图案。temp1、2游戏边框,方块,无图画 int a[80][80] = { 0 }; int b[4]; //标记4个口方块:1表示有方块,0表示无方块 struct Tetris { int x; int y; int flag; int next; int speed; int number; int score; int level; }; HANDLE hOut; void regulation(); void explation(); void close(); main() { title(); welcome(); } //function START int color(int c) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c); return 0; } void gotoxy(int x, int y) { COORD pos; pos.X = x;//横坐标 pos.Y = y;//直坐标 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); } // fucntion END //function START void title() { color(15); gotoxy(24, 3); printf("tetris\n"); color(11); gotoxy(18, 5); printf("■"); gotoxy(18, 6); printf("■■"); gotoxy(18, 7); printf("■"); color(14); gotoxy(26, 6); printf("■■"); gotoxy(28, 7); printf("■■"); color(10); gotoxy(36, 6); printf("■■"); gotoxy(36, 7); color(13); gotoxy(45, 5); printf("■"); gotoxy(45, 6); printf("■"); gotoxy(45, 7); printf("■"); gotoxy(45, 8); printf("■"); color(12); gotoxy(56, 6); printf("■"); gotoxy(52, 7); printf("■■■"); } //function END //function START #pragma warning(disable : 4996) void welcome() { int n; int j = 1; int i = 1; color(14); for (i = 9; i <= 20; i++) { for (j = 15; j <= 60; j++) { gotoxy(j, i); if (i == 9 || i == 20)printf("="); else if (j == 15 || j == 59)printf("||"); } } color(12); gotoxy(25, 12); printf("1.start game"); gotoxy(40, 12); printf("2.exit"); color(13); gotoxy(18, 22); printf("enter number"); gotoxy(18, 23); printf("[ ]\b\b"); scanf_s("%d", &n); switch (n) { case 1: system("cls"); DrawGameFrame(); gameplay(); break; case 2: close(); break; } } //function END //function START void DrawGameFrame() { gotoxy(FrameX + Frame_b - 7, FrameY - 2); color(11); printf("tetris"); gotoxy(FrameX + 2 * Frame_b + 3, FrameY + 7); color(2); printf("----------"); gotoxy(FrameX + 2 * Frame_b + 13, FrameY + 7); color(2); printf("next block"); gotoxy(FrameX + 2 * Frame_b + 3, FrameY + 13); color(2); printf("----------"); gotoxy(FrameX + 2 * Frame_b + 3, FrameY + 17); color(2); printf(" ^to spin block "); gotoxy(FrameX + 2 * Frame_b + 3, FrameY + 15); color(2); printf(" Esc to exit game"); gotoxy(FrameX, FrameY); color(12); printf("┌"); gotoxy(FrameX + 2 * Frame_b - 2, FrameY); color(12); printf("┐"); gotoxy(FrameX, FrameY + Frame_a); color(12); printf("┕"); gotoxy(FrameX + 2 * Frame_b - 2, FrameY + Frame_a); color(12); printf("┙"); a[FrameX][FrameY + Frame_a] = 2; a[FrameX + 2 * Frame_b - 2][FrameY + Frame_a] = 2; //start UP for (i = 2; i < 2 * Frame_b - 2; i += 2) { gotoxy(FrameX + i, FrameY); printf("─"); }//end UP //start DOWN for (i = 2; i < 2 * Frame_b - 2; i += 2) { gotoxy(FrameX + i, FrameY + Frame_a); printf("─"); a[FrameX + i][FrameY + Frame_a] = 2; }//end DOWN for (i = 1; i < Frame_a; i++) { gotoxy(FrameX, FrameY + i); printf("│"); a[FrameX][FrameY + i] = 2; } for (i = 1; i < Frame_a; i++) { gotoxy(FrameX + 2 * Frame_b - 2, FrameY + i); printf("│"); a[FrameX + 2 * Frame_b - 2][FrameY + i] = 2; } } //function END //function START void MakeTetris(struct Tetris*tetris) { a[tetris->x][tetris->y] = b[0]; switch (tetris->flag) { case 1: { color(10); a[tetris->x][tetris->y - 1] = b[1]; a[tetris->x + 2][tetris->y - 1] = b[2]; a[tetris->x + 2][tetris->y] = b[3]; //■■ //■■ break; } case 2: { color(13); a[tetris->x - 2][tetris->y] = b[1]; a[tetris->x + 2][tetris->y] = b[2]; a[tetris->x + 4][tetris->y] = b[3]; break; //■■■■ } case 3: { color(13); a[tetris->x][tetris->y - 1] = b[1]; a[tetris->x][tetris->y - 2] = b[2];//■ a[tetris->x][tetris->y + 1] = b[3];//■ //■ //■ break; } case 4: { color(11); a[tetris->x - 2][tetris->y] = b[1]; a[tetris->x + 2][tetris->y] = b[2]; a[tetris->x][tetris->y + 1] = b[3]; break; //■■■ // ■ } case 5: { color(11); a[tetris->x][tetris->y - 1] = b[1]; a[tetris->x][tetris->y + 1] = b[2]; a[tetris->x - 2][tetris->y] = b[3]; // ■ //■■ // ■ break; } case 6: { color(11); a[tetris->x][tetris->y - 1] = b[1]; a[tetris->x - 2][tetris->y] = b[2]; a[tetris->x + 2][tetris->y] = b[3]; break; // ■ //■■■ } case 7: { color(11); a[tetris->x][tetris->y - 1] = b[1]; a[tetris->x][tetris->y + 1] = b[2]; a[tetris->x + 2][tetris->y] = b[3]; break; //■ //■■ //■ } case 8: { color(14); a[tetris->x][tetris->y + 1] = b[1]; a[tetris->x - 2][tetris->y] = b[2]; a[tetris->x + 2][tetris->y] = b[3]; break; //■■ // ■■ } case 9: { color(14); a[tetris->x][tetris->y - 1] = b[1]; a[tetris->x - 2][tetris->y] = b[2]; a[tetris->x - 2][tetris->y + 1] = b[3]; break; // ■ //■■ //■ } case 10: { color(14); a[tetris->x][tetris->y - 1] = b[1]; a[tetris->x - 2][tetris->y - 1] = b[2]; a[tetris->x + 2][tetris->y] = b[3]; break; // ■■ //■■ } case 11: { color(14); a[tetris->x][tetris->y + 1] = b[1]; a[tetris->x - 2][tetris->y - 1] = b[2]; a[tetris->x - 2][tetris->y] = b[3]; break; //■ //■■ // ■ } case 12: { color(12); a[tetris->x][tetris->y - 1] = b[1]; a[tetris->x][tetris->y + 1] = b[2]; a[tetris->x - 2][tetris->y - 1] = b[3]; break; //■■ // ■ // ■ } case 13: { color(12); a[tetris->x - 2][tetris->y] = b[1]; a[tetris->x + 2][tetris->y - 1] = b[2]; a[tetris->x + 2][tetris->y] = b[3]; break; // ■ //■■■■ } case 14: { color(12); a[tetris->x][tetris->y - 1] = b[1]; a[tetris->x][tetris->y + 1] = b[2]; a[tetris->x + 2][tetris->y + 1] = b[3]; break; //■ //■ //■■ } case 15: { color(12); a[tetris->x - 2][tetris->y] = b[1]; a[tetris->x - 2][tetris->y + 1] = b[2]; a[tetris->x + 2][tetris->y] = b[3]; break; //■■■ //■ } case 16: { color(12); a[tetris->x][tetris->y + 1] = b[1]; a[tetris->x][tetris->y - 1] = b[2]; a[tetris->x + 2][tetris->y - 1] = b[3]; break; //■■ //■ //■ } case 17: { color(12); a[tetris->x - 2][tetris->y] = b[1]; a[tetris->x + 2][tetris->y + 1] = b[2]; a[tetris->x + 2][tetris->y] = b[3]; break; //■■■ // ■ } case 18: { color(12); a[tetris->x][tetris->y - 1] = b[1]; a[tetris->x][tetris->y + 1] = b[2]; a[tetris->x - 2][tetris->y + 1] = b[3]; break; // ■ // ■ //■■ } case 19: { color(12); a[tetris->x - 2][tetris->y] = b[1]; a[tetris->x - 2][tetris->y - 1] = b[2]; a[tetris->x + 2][tetris->y] = b[3]; break; //■ //■■■ } } } //function END //function START v void PrintTetris(struct Tetris*tetris) { for (i = 0; i < 4; i++) { b[i] = 1; } MakeTetris(tetris); for (i = tetris->x - 2; i <= tetris->x + 4; i += 2) { for (j = tetris->y - 2; j <= tetris->y + 1; j++) { if (a[i][j] == 1 && j>FrameY) { gotoxy(i, j); printf("■"); } } } gotoxy(FrameX + 2 * Frame_b + 3, FrameY + 1); color(4); printf("level: "); color(12); printf("%d", tetris->level); gotoxy(FrameX + 2 * Frame_b + 3, FrameY + 3); color(4); printf("score: "); color(12); printf("%d", tetris->score); gotoxy(FrameX + 2 * Frame_b + 3, FrameY + 5); color(4); printf("speed: "); color(12); printf("%dms", tetris->speed); } //function END v //function START int ifMove(struct Tetris*tetris) { if (a[tetris->x][tetris->y] != 0) { return 0; } else { if ( (tetris->flag == 1 && (a[tetris->x][tetris->y - 1] == 0 && a[tetris->x + 2][tetris->y - 1] == 0 && a[tetris->x + 2][tetris->y] == 0)) || (tetris->flag == 2 && (a[tetris->x - 2][tetris->y] == 0 && a[tetris->x + 2][tetris->y] == 0 && a[tetris->x + 4][tetris->y] == 0)) || (tetris->flag == 3 && (a[tetris->x][tetris->y - 1] == 0 && a[tetris->x][tetris->y - 2] == 0 && a[tetris->x][tetris->y + 1] == 0)) || (tetris->flag == 4 && (a[tetris->x - 2][tetris->y] == 0 && a[tetris->x + 2][tetris->y] == 0 && a[tetris->x][tetris->y + 1] == 0)) || (tetris->flag == 5 && (a[tetris->x][tetris->y - 1] == 0 && a[tetris->x][tetris->y + 1] == 0 && a[tetris->x - 2][tetris->y] == 0)) || (tetris->flag == 6 && (a[tetris->x][tetris->y - 1] == 0 && a[tetris->x - 2][tetris->y] == 0 && a[tetris->x + 2][tetris->y] == 0)) || (tetris->flag == 7 && (a[tetris->x][tetris->y - 1] == 0 && a[tetris->x][tetris->y + 1] == 0 && a[tetris->x + 2][tetris->y] == 0)) || (tetris->flag == 8 && (a[tetris->x][tetris->y + 1] == 0 && a[tetris->x - 2][tetris->y] == 0 && a[tetris->x + 2][tetris->y + 1] == 0)) || (tetris->flag == 9 && (a[tetris->x][tetris->y - 1] == 0 && a[tetris->x - 2][tetris->y] == 0 && a[tetris->x - 2][tetris->y + 1] == 0)) || (tetris->flag == 10 && (a[tetris->x][tetris->y - 1] == 0 && a[tetris->x - 2][tetris->y - 1] == 0 && a[tetris->x + 2][tetris->y] == 0)) || (tetris->flag == 11 && (a[tetris->x - 2][tetris->y] == 0 && a[tetris->x + 2][tetris->y] == 0 && a[tetris->x][tetris->y + 1] == 0)) || (tetris->flag == 12 && (a[tetris->x][tetris->y - 1] == 0 && a[tetris->x][tetris->y + 1] == 0 && a[tetris->x - 2][tetris->y + 1 - 1] == 0)) || (tetris->flag == 15 && (a[tetris->x - 2][tetris->y] == 0 && a[tetris->x - 2][tetris->y + 1] == 0 && a[tetris->x + 2][tetris->y] == 0)) || (tetris->flag == 14 && (a[tetris->x][tetris->y - 1] == 0 && a[tetris->x][tetris->y + 1] == 0 && a[tetris->x + 2][tetris->y + 1] == 0)) || (tetris->flag == 13 && (a[tetris->x - 2][tetris->y] == 0 && a[tetris->x + 2][tetris->y - 1] == 0 && a[tetris->x + 2][tetris->y] == 0)) || (tetris->flag == 16 && (a[tetris->x][tetris->y + 1] == 0 && a[tetris->x][tetris->y - 1] == 0 && a[tetris->x + 2][tetris->y - 1] == 0)) || (tetris->flag == 19 && (a[tetris->x - 2][tetris->y] == 0 && a[tetris->x - 2][tetris->y - 1] == 0 && a[tetris->x + 2][tetris->y] == 0)) || (tetris->flag == 18 && (a[tetris->x][tetris->y - 1] == 0 && a[tetris->x][tetris->y + 1] == 0 && a[tetris->x - 2][tetris->y + 1] == 0)) || (tetris->flag == 17 && (a[tetris->x - 2][tetris->y] == 0 && a[tetris->x + 2][tetris->y + 1] == 0 && a[tetris->x + 2][tetris->y + 1] == 0)) )// if end { return 1; } }//else end return 0; } //function END //function START void CleanTetris(struct Tetris*tetris) { for (i = 0; i <= 4; i++) { //for start b[i] = 0; }// for end MakeTetris(tetris); for (i = tetris->x - 2; i <= tetris->x + 4; i += 2) { // for start 1 for (j = tetris->y - 2; j <= tetris->y + 1; j++) { // for start 2 if (a[i][j] == 0 && j > FrameY) { // if start gotoxy(i, j); printf(" "); }// if end } // for end 2 }// for end3 } //function END //function START v void Del_fullline(struct Tetris*tetris) { int k, del_rows = 0; for (j = FrameY + Frame_a - 1; j >= FrameY + 1; j--) { //for 1 start k = 0; for (j = FrameX + 2; i < FrameX + 2 * Frame_b - 2; i += 2) { // for 2strat if (a[i][j] == 1) { //if 1 start k++; if (k == Frame_b - 2) {//fi2 start for (k = FrameX + 2; k < FrameX + 2 * Frame_b - 2; k += 2) {//for 3 start a[k][j] = 0; gotoxy(k, j); printf(" "); }//for 3end for (k = j - 1; k > FrameY; k--) { //for 4 start for (i = FrameX + 2; iscore += 100 * del_rows; } //function END v //function START v void Flag(struct Tetris*tetris) { tetris->number++; srand(time(NULL)); if (tetris->number == 1) { tetris->flag = rand() % 19 + 1; } tetris->next = rand() % 19 + 1; } //function END v //function START //function START v void Replay(struct Tetris* Tetris) { system("cls"); memset(a, 0, 6400 * sizeof(int)); DrawGameFrame(); gameplay(); } //function END v void gameplay() { int n; struct Tetris t, *tetris = &t; char ch; tetris->number = 0; tetris->speed = 300; tetris->score = 0; tetris->level = 1; while (1) { Flag(tetris); Temp = tetris->flag; tetris->x = FrameX + 2 * Frame_b + 6; tetris->y = FrameY + 10; tetris->flag = tetris->next; PrintTetris(tetris); tetris->x = FrameX + Frame_b; tetris->y = FrameY - 1; tetris->flag = Temp; while (1) { label:PrintTetris(tetris); Sleep(tetris->speed); CleanTetris(tetris); Temp1 = tetris->x; Temp2 = tetris->flag; if (kbhit()) { ch = getch(); if (ch == 75) { tetris->x = 2; } if (ch == 77) { tetris->x += 2; } if (ch == 80) { if (ifMove(tetris) != 0) { tetris->y += 2; } if (ifMove(tetris) == 0) { tetris->y = FrameY + Frame_a - 2; } } if (ch == 72) { if (tetris->flag >= 2 && tetris->flag <= 3) { tetris->flag++; tetris->flag %= 2; tetris->flag += 2; } if (tetris->flag >= 4 && tetris->flag <= 7) { tetris->flag++; tetris->flag %= 4; tetris->flag += 4; } if (tetris->flag >= 8 && tetris->flag <= 11) { tetris->flag++; tetris->flag %= 4; tetris->flag += 8; } if (tetris->flag >= 12 && tetris->flag <= 15) { tetris->flag++; tetris->flag %= 4; tetris->flag += 12; } if (tetris->flag >= 16 && tetris->flag <= 19) { tetris->flag++; tetris->flag %= 4; tetris->flag += 16; } } if (ch == 32) { PrintTetris(tetris); while (1) { if (kbhit()) { ch = getch(); {goto label; } } } } if (ch == 27) { system("cls"); memset(a, 0, 6400 * sizeof(int)); welcome(); } if (ifMove(tetris) == 0) { tetris->x = Temp1; tetris->flag = Temp2; } else { goto label; } } tetris->y++; if (ifMove(tetris) == 0) { tetris->y--; PrintTetris(tetris); Del_fullline(tetris); break; } } for (i = tetris->y - 2; iy + 2; i++) { if (i == FrameY) { system("cls"); gotoxy(29, 7); printf(" \n"); color(12); printf("\t\t\t■■■■ ■ ■ ■■ \n"); printf("\t\t\t■ ■■ ■ ■ ■ \n"); printf("\t\t\t■■■ ■ ■ ■ ■ ■ \n"); printf("\t\t\t■ ■ ■ ■ ■ ■ \n"); printf("\t\t\t■■■■ ■ ■ ■■ \n"); gotoxy(17, 18); color(14); printf("我要重新玩一局-------1"); gotoxy(44, 18); printf("不玩了,退出吧-------2\n"); int n; gotoxy(32, 20); printf("选择【1/2】:"); color(11); scanf("%d", &n); switch (n) { case 1: system("cls"); Replay(tetris); break; case 2: exit(0); break; } } } tetris->flag = tetris->next; tetris->x = FrameX + 2 * Frame_b + 6; tetris->y = FrameY + 10; CleanTetris(tetris); } }//function end