编程语言
首页 > 编程语言> > 俄罗斯方块制作代码讲解 C++

俄罗斯方块制作代码讲解 C++

作者:互联网

俄罗斯方块讲解

我们先来看一下上次的视频

<iframe allowfullscreen="true" data-mediaembed="csdn" id="Uafe3553-1643617116590" src="https://live.csdn.net/v/embed/184005"></iframe>

俄罗斯方块 效果展示


经过几次对代码的整改,整个程序有了雏形。不过还是差强人意。但是毕竟是自己做的,能力有限,对于程序的VIP设定,暂时就先按照里边的吧
下面进行对代码的讲解。

移动函数

为了让程序的输出更加好看,需要用到一个gotoXY函数,其中需要一个输出的句柄:“HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);”。

HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);

void gotoXY(HANDLE hOut, int x, int y) {
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(hOut, pos);
}

方块的定义

在方块的定义方面,定义了8种类型的方块,其中新添加了一种由六个方块组成的新方块,方块的样子如下。新方块的样子 屠魔而又成魔
本次使用的储存方块的方式是二维数组存储,那么代码就在下方了

int block00[4][4] = { { 10, 0, 0, 0 }, { 1, 1, 1, 1 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } };
int block01[4][4] = { { 11, 0, 1, 0 }, { 0, 0, 1, 0 }, { 0, 0, 1, 0 }, { 0, 0, 1, 0 } };
int block02[4][4] = { { 12, 0, 0, 0 }, { 0, 0, 0, 0 }, { 1, 1, 1, 0 }, { 0, 1, 0, 0 } };
int block03[4][4] = { { 13, 0, 0, 0 }, { 0, 1, 0, 0 }, { 1, 1, 0, 0 }, { 0, 1, 0, 0 } };
int block04[4][4] = { { 14, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 1, 0, 0 }, { 1, 1, 1, 0 } };
int block05[4][4] = { { 15, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 1, 0 }, { 0, 1, 0, 0 } };
int block06[4][4] = { { 16, 0, 0, 0 }, { 0, 0, 0, 0 }, { 1, 1, 1, 0 }, { 1, 0, 0, 0 } };
int block07[4][4] = { { 17, 0, 0, 0 }, { 1, 1, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 } };
int block08[4][4] = { { 18, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 1, 0 }, { 1, 1, 1, 0 } };
int block09[4][4] = { { 19, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 1, 0 } };
int block10[4][4] = { { 20, 0, 0, 0 }, { 0, 0, 0, 0 }, { 1, 1, 1, 0 }, { 0, 0, 1, 0 } };
int block11[4][4] = { { 21, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 }, { 1, 1, 0, 0 } };
int block12[4][4] = { { 22, 0, 0, 0 }, { 0, 0, 0, 0 }, { 1, 0, 0, 0 }, { 1, 1, 1, 0 } };
int block13[4][4] = { { 23, 0, 0, 0 }, { 0, 1, 1, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 } };
int block14[4][4] = { { 24, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 1, 1, 0 }, { 1, 1, 0, 0 } };
int block15[4][4] = { { 25, 0, 0, 0 }, { 1, 0, 0, 0 }, { 1, 1, 0, 0 }, { 0, 1, 0, 0 } };
int block16[4][4] = { { 26, 0, 0, 0 }, { 0, 0, 0, 0 }, { 1, 1, 0, 0 }, { 0, 1, 1, 0 } };
int block17[4][4] = { { 27, 0, 0, 0 }, { 0, 0, 1, 0 }, { 0, 1, 1, 0 }, { 0, 1, 0, 0 } };
int block18[4][4] = { { 28, 0, 0, 0 }, { 0, 0, 0, 0 }, { 1, 1, 0, 0 }, { 1, 1, 0, 0 } };
int block19[4][4] = { { 29, 0, 1, 1 }, { 0, 0, 1, 1 }, { 0, 0, 1, 1 }, { 0, 0, 1, 1 } };
int block20[4][4] = { { 30, 1, 0, 1 }, { 0, 1, 0, 1 }, { 0, 1, 0, 1 }, { 0, 1, 0, 1 } };
int block21[4][4] = { { 31, 0, 1, 1 }, { 0, 0, 1, 1 }, { 0, 0, 1, 1 }, { 0, 0, 1, 1 } };
int block22[4][4] = { { 32, 0, 0, 0 }, { 0, 0, 0, 0 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 } };
int block23[4][4] = { { 33, 0, 0, 0 }, { 1, 1, 1, 1 }, { 0, 0, 0, 0 }, { 1, 1, 1, 1 } };
int block24[4][4] = { { 34, 0, 0, 0 }, { 0, 0, 0, 0 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 } };

程序运行所需的函数

对于程序运行中所使用的函数,基本已经罗列到了上方了,基本都是比较重要的函数。

void write_It_namea(char a[]);
void initialWindow(HANDLE hOut);//初始化窗口
void initialPrint(HANDLE hOut);//初始化界面
int c();
int wj();
void roundBlock(HANDLE hOut, int block[4][4]);//随机生成方块并打印到下一个方块位置
int tz(HANDLE hOut, int &fraction, int &checkpoint,int &val);
bool collisionDetection(int block[4][4], int map[21][12], int x, int y);//检测碰撞
void printBlock(HANDLE hOut, int block[4][4], int x, int y);//打印方块
void clearBlock(HANDLE hOut, int block[4][4], int x, int y);//消除方块
void myLeft(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y);//左移
void myRight(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y);//右移
void myUp(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y);//顺时针旋转90度
int myDown(HANDLE hOut, int block[4][4], int map[21][12], int &x, int y);//加速下落
void myStop(HANDLE hOut, int block[4][4], int map[21][12]); //游戏暂停
int mymima();
void timee();
int mymin();
int cs();
void isnot_VIP();
int prin();
void print(HANDLE hOut);
void gameOver(HANDLE hOut, int block[4][4], int map[21][12]);//游戏结束
void eliminateRow(HANDLE hOut, int map[21][12], int &val, int &fraction, int &checkpoint);//判断是否能消行并更新分值

游戏公告

游戏的重要公告(好像自从去年开始写就没有更改过公告),可以自行更改。

void g(HANDLE hOut) {
	gotoXY(hOut, 62, 2);
	cout << "	  公告";
	gotoXY(hOut, 52, 3);
	cout << "更新须知:";
	gotoXY(hOut, 52, 4);
	cout << "  1.目前已支持一键重玩,如需请按'r'";
	gotoXY(hOut, 52, 5);
	cout << "  2.已开放挑战模式和无限模式,开启需";
	gotoXY(hOut, 52, 6);
	cout << "要达到151关卡,分数需要达到150000分";
	gotoXY(hOut, 52, 7);
	cout << "  3.达到21关、61关、101关都有精彩礼物";
	gotoXY(hOut, 52, 8);
	cout << "助力您精彩上分!";
	gotoXY(hOut, 52, 9);
	cout << "  4.新增时间看查功能";
	gotoXY(hOut, 52, 10);
	cout << "  5.新增方块,将在游戏中测试,有bug请";
	gotoXY(hOut, 52, 11);
	cout << "请立即反馈给官方QQ,QQ:1213565720";
	gotoXY(hOut, 52, 12);
	cout << "  6.新增功能方块选择,可以根据自己的爱";
	gotoXY(hOut, 52, 13);
	cout << "好来选择方块,选择方块按O后就可以选择了";
	gotoXY(hOut, 58, 14);
	cout << "更多精彩内容,尽在游戏中";
}

用户名称内容备份

这里将用户名称默认写入到了D盘,所以从D盘进行的读取,时候可以进行更改。

void namesa() {
	FILE *fp;
	int file_size;   //保存文件字符数
	//将文件内容读到此指针位置
	fp = fopen("d:\\elsfk\\name.txt", "r"); //打开文件
	fseek(fp, 0, SEEK_END);    //将文件指针指向该文件的最后
	namea_size = ftell(fp);     //根据指针位置,此时可以算出文件的字符数
	if (namea_size == -1) {
		return;
	}
	file_size = namea_size;
	//rintf("文件字符数为 %d\n", file_size);
	namea = (char *)malloc(file_size * sizeof(char)); //根据文件大小为namea动态分配空间
	memset(namea, '\0', file_size * sizeof(char)); //初始化此控件内容,否则可能会有乱码
	fseek(fp, 0, SEEK_SET);               //重新将指针指向文件首部
	fread(namea, sizeof(char), file_size, fp); //开始读取整个文件
	//printf("文件内容为\n%s", namea);
}

方块颜色备份

颜色备份的函数本来是做调试使用的,但是既然写了,那我就先不进行删除了。

void backups_Block(int x, int y) { //方块颜色备份
	gotoXY(hOut, 52, 6);
	cout << "                         ";
	gotoXY(hOut, 52, 6);
	cout << "X与Y的坐标为:" << x << "," << y;
}

方块选择

在方块的选择这一方面,也是与传统的俄罗斯方块不同,对于整个的方块,可以有一些自己主观上的选择,那么为了兼容所有的系统,在此,选择的是一些比较传统的图形:■ ★ ▲ ● ◆
所以整个选择的代码如下:

void gg(HANDLE hOut) {
	SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
	g(hOut);
	gotoXY(hOut, 52, 16);
	cout << "请选择您想用的方块:";
	/*cout << "请选择您想用的方块:";
	string a;
	cin >> a;
	cout << a;
	*/while (1) {
		gotoXY(hOut, 52, 17);
		cout << "1.■ 2.★ 3.▲ 4.● 5.◆";//备份点2
		gotoXY(hOut, 52, 18);
		string a;
		cin >> a;
		switch (a[0]) {
			case '1':
				fkxz = 1;//备份点3
				break;
			case '2':
				fkxz = 2;
				break;
			case '3':
				fkxz = 3;
				break;
			case '4':
				fkxz = 4;
				break;
			case '5':
				fkxz = 5;
				break;
			default :
				gotoXY(hOut, 52, 19);
				cout << "输入有误,请重新输入~\n";
				Sleep(1000);
				gotoXY(hOut, 52, 19);
				cout << "                                      ";
				gotoXY(hOut, 52, 18);
				cout << "                                      ";
				break;
		}
		if (fkxz >= 1) {
			break;
		}
	}
}

时间提示

为了防止玩家过渡沉迷游戏,经过思考后,决定在程序中加入时间提示工具,只是做提醒作用,并没有别的用处
那么下边呢,就是我们的时间模块了,代码量不算太大

void printTime(time_t nowtime, int iFlag) {
	struct tm *timeinfo;
	timeinfo = localtime( &nowtime );

	int year, month, day, hour, min, sec;
	year = timeinfo->tm_year + 1900;
	month = timeinfo->tm_mon + 1;
	day = timeinfo->tm_mday;
	hour = timeinfo->tm_hour;
	min = timeinfo->tm_min;
	sec = timeinfo->tm_sec;
	nian = year, yue = month, ri = day, shi = hour, fen = min, miao = sec;
	fflush(stdout);
}
void timee(HANDLE hOut) {
	SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
	int iRunTime = GetTickCount();
	time_t nowtime;
	time( &nowtime );
	gotoXY(hOut, 52, 10);
	printTime(nowtime, PRINT_ALL);
	printf("\n");
}
void timeout(HANDLE hOut, int x, int y) {
	timee(hOut);
	SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
	gotoXY(hOut, x, y);
	cout << "                                      ";
	gotoXY(hOut, x, y);
	cout << "现在时间是:" << nian << "年" << yue << "月" << ri << "日" << shi << "时" << fen << "分" << miao << "秒";
}

界面初始化

在整个界面的初始化过程中呢,也是分为两个模块,一个是程序一开始的,会有一段初始化动画,另外一个是在游戏内重玩的一个动画。先看两张图片,是游戏效果的一个图片展示
游戏界面的展示
游戏界面的展示
那么我们一起来看看代码吧

void print(HANDLE hOut) {
	SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
	for (int i = 0; i < 20; ++i) {
		cout << "■                    ■☆                      ☆|                                        |" << endl;
	}
	gotoXY(hOut, 26, 0);
	cout << "☆☆☆☆☆☆☆☆☆☆☆☆------------------------------------------";
	gotoXY(hOut, 0, 20);
	cout << "■■■■■■■■■■■■☆☆☆☆☆☆☆☆☆☆☆☆☆------------------------------------------";
	gotoXY(hOut, 26, 1);
	cout << "分    数:      ";
	gotoXY(hOut, 26, 2);
	cout << "关    卡:      ";
	gotoXY(hOut, 26, 4);
	cout << "下一方块:";
	gotoXY(hOut, 26, 10);
	cout << "O:操作方法";
	gotoXY(hOut, 26, 11);
	cout << "V:更新公告";
	gotoXY(hOut, 26, 12);
	cout << "Q:清空提示";
	gotoXY(hOut, 26, 14);
	namea[namea_size - 1] = '\0';
	cout << "欢迎玩家:" << namea;
	gotoXY(hOut, 26, 16);
	cout << "关    于:";
	gotoXY(hOut, 30, 18);
	cout << "俄罗斯方块V1.0";
	gotoXY(hOut, 35, 19);
	cout << "作者:李跞恒";
	gotoXY(hOut, 50, 0);
	SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
	cout <<   "------------------------------------------";
	for (int i = 1; i < 20; i++) {
		gotoXY(hOut, 50, i);
		cout << "|                                        |";
	}
	gotoXY(hOut, 50, 20);
	cout << "------------------------------------------";
	SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
	//gg(hOut);
	for (int i = 1; i < 20; i++) {
		gotoXY(hOut, 52, i);
		cout << "                                      ";
	}
	timee(hOut);
	gotoXY(hOut, 65, 2);
	cout << "加油!";
}
void initialPrint(HANDLE hOut) {
	SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
	for (int i = 0; i < 20; ++i) {
		cout << "■                    ■☆                      ☆|                                        |" << endl;
		Sleep(5);
	}
	gotoXY(hOut, 26, 0);
	cout << "☆☆☆☆☆☆☆☆☆☆☆☆------------------------------------------";
	Sleep(5);
	gotoXY(hOut, 0, 20);
	cout << "■■■■■■■■■■■■☆☆☆☆☆☆☆☆☆☆☆☆☆------------------------------------------";
	Sleep(5);
	gotoXY(hOut, 26, 1);
	cout << "分    数:      ";
	Sleep(5);
	gotoXY(hOut, 26, 2);
	cout << "关    卡:      ";
	Sleep(5);
	gotoXY(hOut, 26, 4);
	cout << "下一方块:";
	Sleep(5);
	gotoXY(hOut, 26, 10);
	cout << "O:操作方法";
	Sleep(5);
	gotoXY(hOut, 26, 11);
	cout << "V:更新公告";
	Sleep(5);
	gotoXY(hOut, 26, 12);
	cout << "Q:清空提示";
	Sleep(5);
	gotoXY(hOut, 26, 14);
	namea[namea_size - 1] = '\0';
	cout << "欢迎玩家:" << namea;
	Sleep(5);
	gotoXY(hOut, 26, 16);
	cout << "关    于:";
	Sleep(5);
	gotoXY(hOut, 30, 18);
	cout << "俄罗斯方块V1.0";
	Sleep(5);
	gotoXY(hOut, 35, 19);
	cout << "作者:李跞恒";
	Sleep(5);
	gotoXY(hOut, 50, 0);
	SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
	cout <<   "------------------------------------------";
	Sleep(5);
	for (int i = 1; i < 20; i++) {
		gotoXY(hOut, 50, i);
		cout << "|                                        |";
		Sleep(5);
	}
	gotoXY(hOut, 50, 20);
	cout << "------------------------------------------";
	SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
	gg(hOut);
	for (int i = 1; i < 20; i++) {
		gotoXY(hOut, 52, i);
		cout << "                                      ";
	}
	timee(hOut);
	gotoXY(hOut, 65, 2);
	cout << "加油!";
}
void qk(HANDLE hOut) {
	for (int i = 1; i <= 19; i++) {
		gotoXY(hOut, 52, i);
		cout << "                                      ";
	}
}

请按空格键继续

就是一个简简单单的while循环

int prin() {
	cout << "请按空格键继续";
	char key;
	while (true) {
		key = _getch();
		if (key == ' ') {
			system("cls");
			break;
		}
	}
}

挑战模式

这个也不知道是什么时候写的了,感觉没有什么实在的意义,所以一直没有动

int tz(HANDLE hOut, int &fraction, int &checkpoint,int &val) {
		if (f == 0) {
			score += 100;
			val = 50;
		} else if (f == 1) {
			score += 200;
			val = 40;
		} else if (f == 2) {
			score += 400;
			val = 35;
		} else if (f == 3) {
			score += 800;
			val = 30;
		} else if (f == 4) {
			score += 1000;
			val = 50;
		} else if (f == 5) {
			score += 1500;
			val = 50;
		} else {
			score += 2000;
			val = 50;
		}
		gk = score / 1000 + 1;
		system("cls");
		if (f1 == 4) {
			gotoXY(hOut, 0, 0);
			SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
			cout << "段位:";
			SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
			cout << "倔强青铜  秩序白银  荣耀黄金  尊贵铂金  永恒钻石  至尊星耀  最强王者  荣耀王者";
			gotoXY(hOut, 0, 3);
			cout << "段位:当前暂未进行挑战!\n\n";
			cout << "提示:\n一、挑战模式中,按键‘r’重玩可以使用。\n二、挑战模式中,必须要比人机提前到达指定的分数,最后一关有奖励。\n三、挑战模式中没有任何奖励,必须要通过自己的实力。\n四、氪金和没氪金的分数不变,但是最后的分数加成会减少。\n五、在最后一关有隐藏奖励,达到相应分数即可,但是必须要比人机提前到达!\n六、如有bug,请联系官方QQ:1213565720,感谢您的使用。\n七、程序的最终解释权归作者所有\n";
			cnt = 102534;
			SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
			cout << "			请先仔细阅读,完成阅读请按‘o’";
			char key;
			while (true) {
				key = _getch();
				if (key == 'o' || key == 'O') {
					cout << "        ";
					system("cls");
					break;
				}
			}
		}
		if (f1 == 4) {
			f1 = 3;
			fla = 11;
			return 0;
		}
		SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
		cout << "当前段位:";
		switch (f) {
			case 0:
				SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
				cout << "倔强青铜";
				if (checkpoint >= 51 && fraction >= 50000) {
					//win(hOut,fraction,checkpoint);
					f = 1;
					f1 = 3;
					fraction = 0;
					checkpoint = 1;
					score = 0;
					gk = 1;
					fla = 11;
				}
				break;
			case 1:
				SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
				cout << "秩序白银";
				if (checkpoint >= 71 && fraction >= 70000) {
					f = 2;
					f1 = 3;
					fraction = 0;
					checkpoint = 1;
					score = 0;
					gk = 1;
					fla = 11;
				}
				break;
			case 2:
				SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
				cout << "荣耀黄金";
				if (checkpoint >= 91 && fraction >= 90000) {
					f = 3;
					f1 = 3;
					fraction = 0;
					checkpoint = 1;
					score = 0;
					gk = 1;
					fla = 11;
				}
				break;
			case 3:
				SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
				cout << "尊贵铂金";
				if (checkpoint >= 111 && fraction >= 110000) {
					f = 4;
					f1 = 3;
					fraction = 0;
					checkpoint = 1;
					score = 0;
					gk = 1;
					fla = 11;
				}
				break;
			case 4:
				SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
				cout << "永恒钻石";
				if (checkpoint >= 141 && fraction >= 140000) {
					f = 5;
					f1 = 3;
					fraction = 0;
					checkpoint = 1;
					score = 0;
					gk = 1;
					fla = 11;
				}
				break;
			case 5:
				SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
				cout << "至尊星耀";
				if (checkpoint >= 151 && fraction >= 150000) {
					f = 6;
					f1 = 3;
					fraction = 0;
					checkpoint = 1;
					score = 0;
					gk = 1;
					fla = 11;
				}
				break;
			case 6:
				SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
				cout << "最强王者";
				if (checkpoint >= 201 && fraction >= 200000) {
					f = 7;
					f1 = 3;
					fraction = 0;
					checkpoint = 1;
					score = 0;
					gk = 1;
					fla = 11;
				}
				break;
			case 7:
				SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
				cout << "荣耀王者";
				if (checkpoint >= 251 && fraction >= 250000) {
					cout << "恭喜您到达荣耀王者\n";
					//cout<<"恭喜您,达到了荣耀王者,请问您想要一下哪些东西呢?\n1.复活甲 能复活一次 2.分数增加 消除后增加分数提升200分(提升的分数上限是2000分) 3.开局跳至21关\n请选择:";
					f1 = 3;
					f = 9;
					fraction = 0;
					checkpoint = 1;
					score = 0;
					gk = 1;
					fla = 11;
				}
				break;
		}
		prin();
	}

写入函数

这里不做过多的介绍了,这些函数是为了写入某个特定的值的。

int wj() {
	if (c() == 0) {
		string path;
		path = "d:\\elsfk";
		path = "md " + path;
		system(path.c_str());
	}
}
int mymima() {
	ofstream ofile;
	ofile.open("d:\\elsfk\\mima.txt");
	ofile << 9;
	ofile.close();
}
int mymin() {
	char a;
	ifstream ifile;               //定义输入文件
	ifile.open("d:\\elsfk\\mima.txt");     //作为输入文件打开
	ifile.get(a);
	ifile.close(); //关闭文件
	return a;
}
int cs() {
	ofstream ofile;
	ofile.open("d:\\elsfk\\c.txt");
	ofile << "^";
	ofile.close();
}
void isnot_VIP() {
	ofstream ofile;
	ofile.open("d:\\elsfk\\c.txt");
	ofile << "D";
	ofile.close();
}
int c() {
	char a;
	ifstream ifile;               //定义输入文件
	ifile.open("d:\\elsfk\\c.txt");     //作为输入文件打开
	ifile.get(a);
	ifile.close(); //关闭文件
	return a;
}
void write_It_namea(char a[]) {
	ofstream ofile;
	ofile.open("d:\\elsfk\\name.txt");
	ofile << a;
	ofile << " ";
	ofile.close();
}

初始窗口

主要是对名称的初始,和使用到的一些东西,不做过多介绍

void initialWindow(HANDLE hOut) {
	SetConsoleTitle("俄罗斯方块");
	COORD size = { 80, 25 };
	SetConsoleScreenBufferSize(hOut, size);
	SMALL_RECT rc = { 0, 0, 79, 24 };
	SetConsoleWindowInfo(hOut, true, &rc);
	CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
	SetConsoleCursorInfo(hOut, &cursor_info);
}

随机生成的方块

所有的方块一共有24种形态,所以进行了一次时间随机数的随机,函数的主要作用是将准备好的方块内容转换到当前的方块,代码逻辑比较单一

void roundBlock(HANDLE hOut, int block[4][4]) {
	clearBlock(hOut, block, 5, 15);
	int n = rand() % 24;
	switch (n) {
		case 0:
			for (int i = 0; i < 4; ++i) {
				for (int j = 0; j < 4; ++j) {
					block[i][j] = block00[i][j];
				}
			}
			break;
		case 1:
			for (int i = 0; i < 4; ++i) {
				for (int j = 0; j < 4; ++j) {
					block[i][j] = block01[i][j];
				}
			}
			break;
		case 2:
			for (int i = 0; i < 4; ++i) {
				for (int j = 0; j < 4; ++j) {
					block[i][j] = block02[i][j];
				}
			}
			break;
		case 3:
			for (int i = 0; i < 4; ++i) {
				for (int j = 0; j < 4; ++j) {
					block[i][j] = block03[i][j];
				}
			}
			break;
		case 4:
			for (int i = 0; i < 4; ++i) {
				for (int j = 0; j < 4; ++j) {
					block[i][j] = block04[i][j];
				}
			}
			break;
		case 5:
			for (int i = 0; i < 4; ++i) {
				for (int j = 0; j < 4; ++j) {
					block[i][j] = block05[i][j];
				}
			}
			break;
		case 6:
			for (int i = 0; i < 4; ++i) {
				for (int j = 0; j < 4; ++j) {
					block[i][j] = block06[i][j];
				}
			}
			break;
		case 7:
			for (int i = 0; i < 4; ++i) {
				for (int j = 0; j < 4; ++j) {
					block[i][j] = block07[i][j];
				}
			}
			break;
		case 8:
			for (int i = 0; i < 4; ++i) {
				for (int j = 0; j < 4; ++j) {
					block[i][j] = block08[i][j];
				}
			}
			break;
		case 9:
			for (int i = 0; i < 4; ++i) {
				for (int j = 0; j < 4; ++j) {
					block[i][j] = block09[i][j];
				}
			}
			break;
		case 10:
			for (int i = 0; i < 4; ++i) {
				for (int j = 0; j < 4; ++j) {
					block[i][j] = block10[i][j];
				}
			}
			break;
		case 11:
			for (int i = 0; i < 4; ++i) {
				for (int j = 0; j < 4; ++j) {
					block[i][j] = block11[i][j];
				}
			}
			break;
		case 12:
			for (int i = 0; i < 4; ++i) {
				for (int j = 0; j < 4; ++j) {
					block[i][j] = block12[i][j];
				}
			}
			break;
		case 13:
			for (int i = 0; i < 4; ++i) {
				for (int j = 0; j < 4; ++j) {
					block[i][j] = block13[i][j];
				}
			}
			break;
		case 14:
			for (int i = 0; i < 4; ++i) {
				for (int j = 0; j < 4; ++j) {
					block[i][j] = block14[i][j];
				}
			}
			break;
		case 15:
			for (int i = 0; i < 4; ++i) {
				for (int j = 0; j < 4; ++j) {
					block[i][j] = block15[i][j];
				}
			}
			break;
		case 16:
			for (int i = 0; i < 4; ++i) {
				for (int j = 0; j < 4; ++j) {
					block[i][j] = block16[i][j];
				}
			}
			break;
		case 17:
			for (int i = 0; i < 4; ++i) {
				for (int j = 0; j < 4; ++j) {
					block[i][j] = block17[i][j];
				}
			}
			break;
		case 18:
			for (int i = 0; i < 4; ++i) {
				for (int j = 0; j < 4; ++j) {
					block[i][j] = block18[i][j];
				}
			}
			break;
		case 19:
			for (int i = 0; i < 4; i++) {
				for (int j = 0; j < 4; j++) {
					block[i][j] = block19[i][j];
				}
			}
			break;
		case 20:
			for (int i = 0; i < 4; i++) {
				for (int j = 0; j < 4; j++) {
					block[i][j] = block20[i][j];
				}
			}
			break;
		case 22:
			for (int i = 0; i < 4; i++) {
				for (int j = 0; j < 4; j++) {
					block[i][j] = block22[i][j];
				}
			}
			break;
		case 23:
			for (int i = 0; i < 4; i++) {
				for (int j = 0; j < 4; j++) {
					block[i][j] = block23[i][j];

				}
			}
			break;
		case 24:
			for (int i = 0; i < 4; i++) {
				for (int j = 0; j < 4; j++) {
					block[i][j] = block24[i][j];
				}
			}
			break;
		default:
			break;
	}
	printBlock(hOut, block, 5, 15);
}

输出方块

方块生成了,也要有一共输出啊,所以在生成方块的时候,有的人也已经注意到了,生成的时候是在前面有一串数字,这串数字是方块的编码,所以在这里就用到了,设置输出的颜色,和备份放到了一起

void printBlock(HANDLE hOut, int block[4][4], int x, int y) {
	switch (block[0][0]) {
		case 10:
		case 11:
			SetConsoleTextAttribute(hOut, FOREGROUND_GREEN);
			NOW_color_block = 10;
			break;
		case 12:
		case 13:
		case 14:
		case 15:
			NOW_color_block = 12;
			SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
			break;
		case 16:
		case 17:
		case 18:
		case 19:
			NOW_color_block = 16;
			SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
			break;
		case 20:
		case 21:
		case 22:
		case 23:
			NOW_color_block = 20;
			SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
			break;
		case 24:
		case 25:
			NOW_color_block = 24;
			SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
			break;
		case 26:
		case 27:
			NOW_color_block = 26;
			SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
			break;
		case 28:
			NOW_color_block = 28;
			SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
			break;
		case 29:
		case 30:
		case 31:
		case 32:
		case 33:
		case 34:
			NOW_color_block = 29;
			SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
			break;
		default:
			break;
	}
	for (int i = 0; i < 4; ++i) {
		if (i + x >= 0) {
			for (int j = 0; j < 4; ++j) {
				if (block[i][j] == 1) {
					gotoXY(hOut, 2 * (y + j), x + i);
					if (fkxz == 1) {
						cout << "■";
					} else if (fkxz == 2) {
						cout << "★";
					} else if (fkxz == 3) {
						cout << "▲";
					} else if (fkxz == 4) {
						cout << "●";
					} else if (fkxz == 5) {
						cout << "◆";
					} else {
						cout << "0";
					}
				}
			}
		}
	}
}

碰撞检测

这个东西没有什么可以说的,就是单纯的用算法写的

bool collisionDetection(int block[4][4], int map[21][12], int x, int y) {
	for (int i = 0; i < 4; ++i) {
		for (int j = 0; j < 4; ++j) {
			if (x + i >= 0 && y + j >= 0 && map[x + i][y + j] == 1 && block[i][j] == 1) {
				return false;
			}
		}
	}
	return true;
}

清除方块

对于方块的清空其实是与方块的输出类似的,但是为了方便,方块的颜色备份的内容的清空也是写在了这个地方,在这里输出了两个空格,因为每个方块占用的是2个字节,而在这种界面上的输出,刚好是每个空格一个字节。但是在Windows7上,这种方块的清空只需要输出一个空格就行,在这里我做过测试的。那么,为了兼容Windows10与Windows11,这里就使用输出两个空格(两个字节)来清空控制台。

void clearBlock(HANDLE hOut, int block[4][4], int x, int y) {
	for (int i = 0; i < 4; ++i) {
		if (i + x >= 0) {
			for (int j = 0; j < 4; ++j) {
				if (block[i][j] == 1) {
					gotoXY(hOut, 2 * (y + j), x + i);
					cout << "  ";
					NOW_color_bloCK[x + i][y + j] = 0;
				}
			}
		}
	}
}

游戏结束

当游戏结束的时候,是会有存档的没有删除的,那么在这里呢,对于当前存档要进行一个重新的写入,就可以实现了存档的删除的类似操作,那么在主函数内部,当判断到游戏结束了,会对游戏的数据进行清零处理,从而达到删除的效果。

void gameOver(HANDLE hOut, int block[4][4], int map[21][12]) {
	SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
	gotoXY(hOut, 9, 8);
	cout << "GAME OVER";
	gotoXY(hOut, 8, 9);
	cout << "空格键:重来";
	gotoXY(hOut, 8, 10);
	cout << "ESC键:退出";
	char key;
	while (true) {
		key = _getch();
		if (key == 32) {
			gotoXY(hOut, 2, 11);
			cnt = 0;
			system("pause");
			system("cls");
			return;
		}
		if (key == 27) {
			system("cls");
			freopen("D:\\elsfk\\color.txt", "w", stdout);
			for (int i = 0; i < 21; i++) {
				for (int j = 0; j < 12; j++) {
					cout << color_backups[i][j] << "	";
				}
				cout << endl;
			}
			fclose(stdout);
			freopen("D:\\elsfk\\last.txt", "w", stdout);
			for (int i = 0; i < 21; i++) {
				for (int j = 0; j < 12; j++) {
					cout << map[i][j] << "	";
				}
				cout << endl;
			}
			fclose(stdout);
			exit(0);
			fclose(stdout);
			break;
			exit(0);
		}
	}
}

方块的变形

对于方块的一个变型操作,使用了逐个枚举的方法,将方块的一个变型的操作进行一个类似打标的操作

void myUp(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y) {
	switch (block[0][0]) {
		case 10:
			if (collisionDetection(block01, map, x, y)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block01[i][j];
					}
				}
			}
			break;
		case 11:
			if (collisionDetection(block00, map, x, y)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block00[i][j];
					}
				}
			} else if (collisionDetection(block00, map, x, y - 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block00[i][j];
					}
				}
				--y;
			} else if (collisionDetection(block00, map, x, y + 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block00[i][j];
					}
				}
				++y;
			} else if (collisionDetection(block00, map, x, y - 2)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block00[i][j];
					}
				}
				y = y - 2;
			} else if (collisionDetection(block00, map, x, y + 2)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block00[i][j];
					}
				}
				y = y + 2;
			}
			break;
		case 12:
			if (collisionDetection(block03, map, x, y)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block03[i][j];
					}
				}
			} else if (collisionDetection(block03, map, x, y - 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block03[i][j];
					}
				}
				--y;
			} else if (collisionDetection(block03, map, x, y + 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block03[i][j];
					}
				}
				++y;
			}
			break;
		case 13:
			if (collisionDetection(block04, map, x, y)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block04[i][j];
					}
				}
			} else if (collisionDetection(block04, map, x, y - 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block04[i][j];
					}
				}
				--y;
			} else if (collisionDetection(block04, map, x, y + 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block04[i][j];
					}
				}
				++y;
			}
			break;
		case 14:
			if (collisionDetection(block05, map, x, y)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block05[i][j];
					}
				}
			} else if (collisionDetection(block05, map, x, y - 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block05[i][j];
					}
				}
				--y;
			} else if (collisionDetection(block05, map, x, y + 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block05[i][j];
					}
				}
				++y;
			}
			break;
		case 15:
			if (collisionDetection(block02, map, x, y)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block02[i][j];
					}
				}
			} else if (collisionDetection(block02, map, x, y - 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block02[i][j];
					}
				}
				--y;
			} else if (collisionDetection(block02, map, x, y + 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block02[i][j];
					}
				}
				++y;
			}
			break;

		case 16:
			if (collisionDetection(block07, map, x, y)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block07[i][j];
					}
				}
			} else if (collisionDetection(block07, map, x, y - 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block07[i][j];
					}
				}
				--y;
			} else if (collisionDetection(block07, map, x, y + 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block07[i][j];
					}
				}
				++y;
			}
			break;
		case 17:
			if (collisionDetection(block08, map, x, y)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block08[i][j];
					}
				}
			} else if (collisionDetection(block08, map, x, y - 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block08[i][j];
					}
				}
				--y;
			} else if (collisionDetection(block08, map, x, y + 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block08[i][j];
					}
				}
				++y;
			}
			break;
		case 18:
			if (collisionDetection(block09, map, x, y)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block09[i][j];
					}
				}
			} else if (collisionDetection(block09, map, x, y - 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block09[i][j];
					}
				}
				--y;
			} else if (collisionDetection(block09, map, x, y + 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block09[i][j];
					}
				}
				++y;
			}
			break;
		case 19:
			if (collisionDetection(block06, map, x, y)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block06[i][j];
					}
				}
			} else if (collisionDetection(block06, map, x, y - 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block06[i][j];
					}
				}
				--y;
			} else if (collisionDetection(block06, map, x, y + 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block06[i][j];
					}
				}
				++y;
			}
			break;
		case 20:
			if (collisionDetection(block11, map, x, y)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block11[i][j];
					}
				}
			} else if (collisionDetection(block11, map, x, y - 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block11[i][j];
					}
				}
				--y;
			} else if (collisionDetection(block11, map, x, y + 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block11[i][j];
					}
				}
				++y;
			}
			break;
		case 21:
			if (collisionDetection(block12, map, x, y)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block12[i][j];
					}
				}
			} else if (collisionDetection(block12, map, x, y - 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block12[i][j];
					}
				}
				--y;
			} else if (collisionDetection(block12, map, x, y + 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block12[i][j];
					}
				}
				++y;
			}
			break;
		case 22:
			if (collisionDetection(block13, map, x, y)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block13[i][j];
					}
				}
			} else if (collisionDetection(block13, map, x, y - 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block13[i][j];
					}
				}
				--y;
			} else if (collisionDetection(block13, map, x, y + 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block13[i][j];
					}
				}
				++y;
			}
			break;
		case 23:
			if (collisionDetection(block10, map, x, y)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block10[i][j];
					}
				}
			} else if (collisionDetection(block10, map, x, y - 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block10[i][j];
					}
				}
				--y;
			} else if (collisionDetection(block10, map, x, y + 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block10[i][j];
					}
				}
				++y;
			}
			break;
		case 24:
			if (collisionDetection(block15, map, x, y)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block15[i][j];
					}
				}
			} else if (collisionDetection(block15, map, x, y - 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block15[i][j];
					}
				}
				--y;
			} else if (collisionDetection(block15, map, x, y + 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block15[i][j];
					}
				}
				++y;
			}
			break;
		case 25:
			if (collisionDetection(block14, map, x, y)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block14[i][j];
					}
				}
			} else if (collisionDetection(block14, map, x, y - 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block14[i][j];
					}
				}
				--y;
			} else if (collisionDetection(block14, map, x, y + 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block14[i][j];
					}
				}
				++y;
			}
			break;
		case 26:
			if (collisionDetection(block17, map, x, y)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block17[i][j];
					}
				}
			} else if (collisionDetection(block17, map, x, y - 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block17[i][j];
					}
				}
				--y;
			} else if (collisionDetection(block17, map, x, y + 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block17[i][j];
					}
				}
				++y;
			}
			break;
		case 27:
			if (collisionDetection(block16, map, x, y)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block16[i][j];
					}
				}
			} else if (collisionDetection(block16, map, x, y - 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block16[i][j];
					}
				}
				--y;
			} else if (collisionDetection(block16, map, x, y + 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						block[i][j] = block16[i][j];
					}
				}
				++y;
			}
			break;
		case 29:
			if (collisionDetection(block19, map, x, y - 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; i++) {
					for (int j = 0; j < 4; j++) {
						block[i][j] = block20[i][j];
					}
				}
			} else if (collisionDetection(block19, map, x, y - 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; i++) {
					for (int j = 0; j < 4; j++) {
						block[i][j] = block21[i][j];
					}
				}
			} else if (collisionDetection(block19, map, x, y - 2)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; i++) {
					for (int j = 0; j < 4; j++) {
						block[i][j] = block22[i][j];
					}
				}
			} else if (collisionDetection(block19, map, x, y - 2)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; i++) {
					for (int j = 0; j < 4; j++) {
						block[i][j] = block23[i][j];
					}
				}
			} else if (collisionDetection(block19, map, x, y - 2)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; i++) {
					for (int j = 0; j < 4; j++) {
						block[i][j] = block24[i][j];
					}
				}
			}
			break;
		case 30:
			if (collisionDetection(block20, map, x, y - 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; i++) {
					for (int j = 0; j < 4; j++) {
						block[i][j] = block21[i][j];
					}
				}
			} else if (collisionDetection(block20, map, x, y - 2)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; i++) {
					for (int j = 0; j < 4; j++) {
						block[i][j] = block22[i][j];
					}
				}
			} else if (collisionDetection(block20, map, x, y - 2)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; i++) {
					for (int j = 0; j < 4; j++) {
						block[i][j] = block23[i][j];
					}
				}
			} else if (collisionDetection(block20, map, x, y - 2)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; i++) {
					for (int j = 0; j < 4; j++) {
						block[i][j] = block24[i][j];
					}
				}
			} else if (collisionDetection(block20, map, x, y - 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; i++) {
					for (int j = 0; j < 4; j++) {
						block[i][j] = block19[i][j];
					}
				}
			}
			break;
		case 31:
			if (collisionDetection(block21, map, x, y - 2)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; i++) {
					for (int j = 0; j < 4; j++) {
						block[i][j] = block22[i][j];
					}
				}
			} else if (collisionDetection(block21, map, x, y - 2)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; i++) {
					for (int j = 0; j < 4; j++) {
						block[i][j] = block23[i][j];
					}
				}
			} else if (collisionDetection(block21, map, x, y - 2)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; i++) {
					for (int j = 0; j < 4; j++) {
						block[i][j] = block24[i][j];
					}
				}
			} else if (collisionDetection(block21, map, x, y - 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; i++) {
					for (int j = 0; j < 4; j++) {
						block[i][j] = block19[i][j];
					}
				}
			} else if (collisionDetection(block21, map, x, y - 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; i++) {
					for (int j = 0; j < 4; j++) {
						block[i][j] = block20[i][j];
					}
				}
			}
			break;
		case 32:
			if (collisionDetection(block22, map, x, y - 2)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; i++) {
					for (int j = 0; j < 4; j++) {
						block[i][j] = block23[i][j];
					}
				}
			} else if (collisionDetection(block22, map, x, y - 2)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; i++) {
					for (int j = 0; j < 4; j++) {
						block[i][j] = block24[i][j];
					}
				}
			} else if (collisionDetection(block22, map, x, y - 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; i++) {
					for (int j = 0; j < 4; j++) {
						block[i][j] = block19[i][j];
					}
				}
			} else if (collisionDetection(block22, map, x, y - 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; i++) {
					for (int j = 0; j < 4; j++) {
						block[i][j] = block20[i][j];
					}
				}
			} else if (collisionDetection(block22, map, x, y - 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; i++) {
					for (int j = 0; j < 4; j++) {
						block[i][j] = block21[i][j];
					}
				}
			}
			break;
		case 33:
			if (collisionDetection(block23, map, x, y - 2)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; i++) {
					for (int j = 0; j < 4; j++) {
						block[i][j] = block24[i][j];
					}
				}
			} else if (collisionDetection(block23, map, x, y - 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; i++) {
					for (int j = 0; j < 4; j++) {
						block[i][j] = block19[i][j];
					}
				}
			} else if (collisionDetection(block23, map, x, y - 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; i++) {
					for (int j = 0; j < 4; j++) {
						block[i][j] = block20[i][j];
					}
				}
			} else if (collisionDetection(block23, map, x, y - 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; i++) {
					for (int j = 0; j < 4; j++) {
						block[i][j] = block21[i][j];
					}
				}
			} else if (collisionDetection(block23, map, x, y - 2)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; i++) {
					for (int j = 0; j < 4; j++) {
						block[i][j] = block22[i][j];
					}
				}
			}
			break;
		case 34:
			if (collisionDetection(block24, map, x, y - 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; i++) {
					for (int j = 0; j < 4; j++) {
						block[i][j] = block19[i][j];
					}
				}
			} else if (collisionDetection(block24, map, x, y - 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; i++) {
					for (int j = 0; j < 4; j++) {
						block[i][j] = block20[i][j];
					}
				}
			} else if (collisionDetection(block24, map, x, y - 1)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; i++) {
					for (int j = 0; j < 4; j++) {
						block[i][j] = block21[i][j];
					}
				}
			} else if (collisionDetection(block24, map, x, y - 2)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; i++) {
					for (int j = 0; j < 4; j++) {
						block[i][j] = block22[i][j];
					}
				}
			} else if (collisionDetection(block24, map, x, y - 2)) {
				clearBlock(hOut, block, x, y);
				for (int i = 0; i < 4; i++) {
					for (int j = 0; j < 4; j++) {
						block[i][j] = block23[i][j];
					}
				}
			}
			break;
		default:
			break;
	}
}

下左右的3个函数

对于左右这两个函数来讲,基本是一致的,重点来说下降的函数,下降的函数是需要对地图进行一个写入的操作,为了是进行接下来的消除一行方块的一个判定,那么在写入map的同时,会进行color的写入,当前的颜色,以保证对于备份文件的使用。

int myDown(HANDLE hOut, int block[4][4], int map[21][12], int &x, int y) {
	if (collisionDetection(block, map, x + 1, y)) {
		clearBlock(hOut, block, x, y);
		++x;
		return 0;
	}
	if (x < 0) {
		return 2;
	}
	for (int i = 0; i < 4; ++i) {
		for (int j = 0; j < 4; ++j) {
			if (block[i][j] == 1) {
				map[x + i][y + j] = 1;
				//SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
				//gotoXY(hOut, 2 * (y + j), x + i);
				color_backups[x + i][y + j] = NOW_color_block;
				//cout << "■";
				//cout<<"■";
			}
		}
	}
	return 1;
}

void myLeft(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y) {
	if (collisionDetection(block, map, x, y - 1)) {
		clearBlock(hOut, block, x, y);
		--y;
	}
}

void myRight(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y) {
	if (collisionDetection(block, map, x, y + 1)) {
		clearBlock(hOut, block, x, y);
		++y;
	}
}

游戏暂停

游戏不可能让玩家一直玩下去,在中途也可能会需要有别的事情,那么对于这种情况,在这里,使用了一个暂停的方法,那即使是暂停了,那也不能忘记时间,在这里,也是贴心的进行了一个时间的输出。
输出时间显然不能与输入冲突,就会使用一个_kbhit()的函数,是检测键盘有没有按下按键,如果按下了,就输入,没有及继续输出时间。

void myStop(HANDLE hOut, int block[4][4], int map[21][12]) {
	clearBlock(hOut, block, 5, 15);
	SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
	gotoXY(hOut, 30, 7);
	cout << "游戏暂停";
	char key;
	gotoXY(hOut, 52, 10);
	//cout << "暂停时时间:" << nian << "年" << yue << "月" << ri << "日" << shi << "时" << fen << "分" << miao << "秒";
	while (true) {
		if (_kbhit()) {
			key = getch();
			if (key == 32) {
				gotoXY(hOut, 30, 7);
				cout << "        ";
				printBlock(hOut, block, 5, 15);
				return;
			}
			if (key == 27) {
				system("cls");
				freopen("D:\\elsfk\\color.txt", "w", stdout);
				for (int i = 0; i < 21; i++) {
					for (int j = 0; j < 12; j++) {
						cout << color_backups[i][j] << "	";
					}
					cout << endl;
				}
				fclose(stdout);
				freopen("D:\\elsfk\\last.txt", "w", stdout);
				for (int i = 0; i < 21; i++) {
					for (int j = 0; j < 12; j++) {
						cout << map[i][j] << "	";
					}
					cout << endl;
				}
				fclose(stdout);
				//system("start D:\\elsfk\\last.txt");
				//system("start D:\\elsfk\\color.txt");
				exit(0);
				fclose(stdout);
				freopen("D:\\elsfk\\Users.txt", "w", stdout);
				//cout << fraction << " " << checkpoint << " " << val << endl;
				fclose(stdout);
				break;
				exit(0);
			}
		} else {
			timee(hOut);
			gotoXY(hOut, 52, 12);
			cout << "现在时间是:" << nian << "年" << yue << "月" << ri << "日" << shi << "时" << fen << "分" << miao << "秒";
		}
	}
}

分数与关卡的计算

这是倒数第二个函数了,对于分数的计算,这个函数主要是在看的是当前这一行的数据总和加起来是10的,进行一个加分的处理。另外,对于关卡的计算是一句分数的总和除以1000在加上1,计算的。

void eliminateRow(HANDLE hOut, int map[21][12], int &val, int &fraction, int &checkpoint) {
	SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
	for (int i = 19; i >= 0; --i) {
		int x = 0;
		for (int j = 1; j < 11; ++j) {
			x += map[i][j];
		}
		if (x == 10) {
			if (it_is_vis_VIP) {
				fraction += 200;//分数
				if (val > 1 && fraction / 1000 + 1 != checkpoint) {
					checkpoint = fraction / 1000 + 1;
					val = 50;
				}
			} else {
				fraction += 100; //分数
				if (val > 1 && fraction / 1000 + 1 != checkpoint) {
					checkpoint = fraction / 1000 + 1;
					val -= 5;
				}
			}
			for (int m = i; m > 0; --m) {
				for (int n = 1; n < 11; ++n) {
					map[m][n] = map[m - 1][n];
					color_backups[m][n] = color_backups[m - 1][n];
					gotoXY(hOut, 2 * n, m);
					if (map[m][n] == 1) {
						if (x == 1) {
							cout << "■";
						} else if (x == 2) {
							cout << "●";
						} else if (x == 3) {
							cout << "◆";
						} else if (x == 4) {
							cout << "★";
						}
					} else {
						cout << "  ";
					}
				}
			}
			++i;
		}
	}
}

主函数

主函数的内容有点多,在这里不在进行详细解释了,一位内如果进行详细解释的话,一篇文章也写不完,可以先自己理解一下吧

int main() {
	wj();
	namesa();
	string a, b;
	a = "llh%*)^@(#%";
	//char a[8] = {'\0'}, b = '0';
	if ((c() != '^') && (c() != 'D')) {
		cout << "此电脑是否是您的常用电脑? (Y)是 (N)不是:";
		char cha;
		cin >> cha;
		cout << endl;
		if (cha == 'Y') {
			cout << endl;
			cout << "请输入游戏密钥:(与游戏运行无关)";
			cin >> b;
			if (a == b) {
				cs();
				cout << "\n《VIP用户手册》\n尊敬的VIP用户您好,欢迎使用俄罗斯方块小游戏。\n	1.已为您准备好内置更换方块的特权。\n	2.您的分数值将会超越其他人,有更好的游戏体验。\n	3.会对您的游戏记录进行实时存档\n";
			} else {
				isnot_VIP();
				cout << "您是否考虑加入VIP呢,申请VIP只需要加客服QQ:1213565720 即可";
			}
		} else {
			cout << "这不是你的电脑,游戏将不会记录!";
			cout << endl;
			cout << "请输入游戏密钥:(与游戏运行无关)";
			cin >> b;
			if (a == b) {
				cs();
				cout << "\n《VIP用户手册》\n尊敬的VIP用户您好,欢迎使用俄罗斯方块小游戏。\n	1.已为您准备好内置更换方块的特权。\n	2.您的分数值将会超越其他人,有更好的游戏体验。\n	3.会对您的游戏记录进行实时存档\n";
			}
		}
		cout << endl;

	}
	system("pause");
	system("cls");
	int map[21][12];
	int blockA[4][4];//候选区的方块
	int blockB[4][4];//下落中的方块
	int positionX, positionY;//方块左上角的坐标
	bool check;//检查方块还能不能下落
	char key;//用来存储按键
	int val;//用来控制下落速度
	int fraction;//用来存储得分
	int checkpoint;//用来存储关卡
	int times;
	int tc;
	int cnnt = 0;
	/*上方变量声明需要备份*/
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);//获取标准输出设备句柄
	initialWindow(hOut);
	if (c() == 'D') {
		it_is_vis_VIP=0;
		cnnt = 0;
	} else {
		it_is_vis_VIP=1;
		cnnt = 1;/*该变量的值需要备份*/
	}
	system("mode con cols=94 lines=26");
	if (namea_size == -1) {
		while (1) {
			gotoXY(hOut, 30, 10);
			cout << "请输入用户名:\n";
			gotoXY(hOut, 30, 11);
			char nameaa[200];
			cin >> nameaa;
			write_It_namea(nameaa);
			exit(0);
			int lenn = 0;
			lenn = strlen(nameaa);
			nameaa[lenn] = ' ';
			if (a[0] == ' ' || a[0] == '\0') {
				system("cls");
				cout << "用户姓名识别失败,请重新输入~\n";
			} else if (lenn >= 12 && lenn <= 1) {
				system("cls");
				cout << "用户姓名过长,请重新输入。例:测试员001\n";
			} else {
				system("cls");
				cout << nameaa << ",您好,游戏即将开始,请先做好游戏准备。\n";
				Sleep(500);
				system("cls");
				cout << "3\n";
				Sleep(1000);
				system("cls");
				cout << "2\n";
				Sleep(1000);
				system("cls");
				cout << "1\n";
				Sleep(1000);
				system("cls");
				cout << "游戏开始!!!";
				Sleep(2000);
				system("cls");
				cnnt = 1;
				break;
			}
		}
	}
	cout << "cnnt的值为" << cnnt;
	system("pause");
	timee(hOut);
	int nian1, yue1, ri1, shi1, fen1, miao1;
	nian1 = nian, yue1 = yue, ri1 = ri, shi1 = shi, fen1 = fen, miao1 = miao;
initial:
	gotoXY(hOut, 0, 0);
	initialPrint(hOut);
	int temp = fkxz;
	check = true;
	val = 50;
	fraction = 0 * 1000;
	checkpoint = 1;
	cnt = 0;
	times = val;
	/*上方变量不冲突的进行备份*/
	/*for (int i = 0; i < 20; ++i) {
		for (int j = 1; j < 11; ++j) {
			map[i][j] = 0;
		}
	}
	for (int i = 0; i < 20; ++i) {
		map[i][0] = map[i][11] = 1;
	}
	for (int i = 0; i < 12; ++i) {
		map[20][i] = 1;
	}*/
	/*地图备份*/
	ifstream infile;
	infile.open("d:\\elsfk\\last.txt");
	if (!infile) cout << "error" << endl;
	string str;
	int t1;
	int*p = &map[0][0];
	while (infile >> t1) {        //遇到空白符结束
		*p = t1;
		p++;
	}
	bool if_flag_map_check = false;
	gotoXY(hOut, 52, 3);
	cout << "正在校验存档文件";
	for (int i = 0; i < 21; ++i) {
		for (int j = 1; j < 12; ++j) {
			//Sleep(5);
			if (map[i][j] < 0 || map[i][j] > 1) {
				if_flag_map_check = true;
				break;
			}
		}
	}
	gotoXY(hOut, 52, 4);
	cout << "备份文件校验完成";
	Sleep(500);
	system("cls");
	print(hOut);
	if (if_flag_map_check) {
		gotoXY(hOut, 52, 3);
		cout << "备份文件有误,正在重新创建地图…";
		for (int i = 0; i < 20; ++i) {
			for (int j = 1; j < 11; ++j) {
				map[i][j] = 0;
				//			Sleep(10);
			}
		}
		for (int i = 0; i < 20; ++i) {
			map[i][0] = map[i][11] = 1;
			//		Sleep(10);
		}
		for (int i = 0; i < 12; ++i) {
			map[20][i] = 1;
			//		Sleep(10);
		}
		gotoXY(hOut, 52, 4);
		cout << "地图创建成功,正在备份地图…";
		Sleep(500);
		system("cls");
		print(hOut);
		system("cls");
		freopen("D:\\elsfk\\last.txt", "w", stdout);
		MessageBox(NULL, "俄罗斯方块地图已开始备份,请不要关闭此程序,感谢您的配合!", "俄罗斯方块", MB_OK);
		for (int i = 0; i < 21; i++) {
			for (int j = 0; j < 12; j++) {
				cout << map[i][j] << "  ";
				//			Sleep(20);
			}
			cout << endl;
			//		Sleep(20);
		}
		MessageBox(NULL, "地图存档创建完成,正在重新打开", "俄罗斯方块", MB_OK);
		system("start 俄罗斯方块.exe") ;
		exit(0);
		fclose(stdout);
		freopen("D:\\elsfk\\Users.txt", "w", stdout);
		cout << fraction << " " << checkpoint << " " << val << endl;
		fclose(stdout);
	}
	fkxz = temp;
	ifstream infile1;
	infile1.open("d:\\elsfk\\color.txt");
	if (!infile1) cout << "error" << endl;
	string str1;
	int t11;
	int*p1 = &color_backups[0][0];
	while (infile1 >> t11) {        //遇到空白符结束
		*p1 = t11;
		p1++;
	}
	for (int i = 0; i < 20; ++i) {
		for (int j = 1; j < 11; ++j) {
			if (map[i][j] == 1) {
				switch (color_backups[i][j]) {
					case 10:
					case 11:
						SetConsoleTextAttribute(hOut, FOREGROUND_GREEN);
						NOW_color_block = 10;
						break;
					case 12:
					case 13:
					case 14:
					case 15:
						NOW_color_block = 12;
						SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
						break;
					case 16:
					case 17:
					case 18:
					case 19:
						NOW_color_block = 16;
						SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
						break;
					case 20:
					case 21:
					case 22:
					case 23:
						NOW_color_block = 20;
						SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
						break;
					case 24:
					case 25:
						NOW_color_block = 24;
						SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
						break;
					case 26:
					case 27:
						NOW_color_block = 26;
						SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
						break;
					case 28:
						NOW_color_block = 28;
						SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
						break;
					case 29:
					case 30:
					case 31:
					case 32:
					case 33:
					case 34:
						NOW_color_block = 29;
						SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
						break;
					default:
						break;
				}
				gotoXY(hOut, 2 * ( j),  i);
				if (fkxz == 1) {
					cout << "■";
				} else if (fkxz == 2) {
					cout << "★";
				} else if (fkxz == 3) {
					cout << "▲";
				} else if (fkxz == 4) {
					cout << "●";
				} else if (fkxz == 5) {
					cout << "◆";
				} else {
					cout << "0";
				}
			}
		}
	}
	srand((unsigned)time(NULL));
	roundBlock(hOut, blockA);
	int x1 = 52, y1 = 12;
	while (true) {
		for (int i = 0; i < 20; ++i) {
			for (int j = 1; j < 11; ++j) {
				if (map[i][j] == 1) {
					switch (color_backups[i][j]) {
						case 10:
						case 11:
							SetConsoleTextAttribute(hOut, FOREGROUND_GREEN);
							NOW_color_block = 10;
							break;
						case 12:
						case 13:
						case 14:
						case 15:
							NOW_color_block = 12;
							SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
							break;
						case 16:
						case 17:
						case 18:
						case 19:
							NOW_color_block = 16;
							SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
							break;
						case 20:
						case 21:
						case 22:
						case 23:
							NOW_color_block = 20;
							SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
							break;
						case 24:
						case 25:
							NOW_color_block = 24;
							SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
							break;
						case 26:
						case 27:
							NOW_color_block = 26;
							SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
							break;
						case 28:
							NOW_color_block = 28;
							SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
							break;
						case 29:
						case 30:
						case 31:
						case 32:
						case 33:
						case 34:
							NOW_color_block = 29;
							SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
							break;
						default:
							break;
					}
					gotoXY(hOut, 2 * ( j),  i);
					if (fkxz == 1) {
						cout << "■";
					} else if (fkxz == 2) {
						cout << "★";
					} else if (fkxz == 3) {
						cout << "▲";
					} else if (fkxz == 4) {
						cout << "●";
					} else if (fkxz == 5) {
						cout << "◆";
					} else {
						cout << "0";
					}
				}
			}
		}
		timeout(hOut, x1, y1);
		SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
		gotoXY(hOut, 26, 14);
		cout << "欢迎玩家:" << namea;
		int a;/*备份是否到达附加关卡*/
		if (a == 3) {
			fraction = 20 * 1000, checkpoint = 21;
			a = 50, cnt = 1000;
		}
		if (checkpoint >= 21 && cnt == 0) {
			for (int i = 2; i < 7; i++) {
				gotoXY(hOut, 52, i);
				cout << "                                    " ;
				Sleep(100);
			}
			SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
			gotoXY(hOut, 52, 2);
			cout << "准备达到21关的礼包,请稍等";
			gotoXY(hOut, 52, 3);
			system("pause");
			gotoXY(hOut, 52, 4);
			if (flag) {
				cout << "终于到达21关,好厉害啊,准备给你跳跃";
				gotoXY(hOut, 52, 5);
				cout << "到41关,可以获得" << 20 * 1000 << "分!";
				gotoXY(hOut, 52, 6);
				system("pause");
				checkpoint = checkpoint + 20;
				fraction = (checkpoint - 1) * 1000;
			} else {
				cout << "目前已经达到了21关,已经很厉害了,";
				gotoXY(hOut, 52, 5);
				cout << "决定赠送给你10000分,可以直接跳跃至";
				gotoXY(hOut, 52, 6);
				cout << "31关,加油,后边还有礼包!";
				gotoXY(hOut, 52, 7);
				system("pause");
				checkpoint = checkpoint + 10;
				fraction = (checkpoint - 1) * 1000;
			}
			cnt++;
			for (int i = 2; i < 7; i++) {
				gotoXY(hOut, 52, i);
				cout << "                                    " ;
				Sleep(100);
			}
		} else if (checkpoint >= 61 && (cnt == 1 || cnt == 0)) {
			SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
			for (int i = 2; i < 7; i++) {
				gotoXY(hOut, 52, i);
				cout << "                                    " ;
				Sleep(100);
			}
			gotoXY(hOut, 52, 2);
			Sleep(100);
			cout << "哇,厉害了,居然有如此之传奇!";
			gotoXY(hOut, 52, 3);
			cout << "达到61关也不容易,真的太厉害了!";
			gotoXY(hOut, 52, 4);
			if (flag) {
				cout << "准备给你可以直接给你提升至91关!";
				gotoXY(hOut, 52, 5);
				cout << "30000分将送给你,加油!";
				gotoXY(hOut, 52, 6);
				system("pause");
				checkpoint = checkpoint + 30;
				fraction = (checkpoint - 1) * 1000;
			} else {
				cout << "我们决定给您提升至75关!";
				gotoXY(hOut, 52, 5);
				cout << "15000分将送给你,加油!";
				gotoXY(hOut, 52, 6);
				cout << "到达100关,将会有礼物给你!加油";
				gotoXY(hOut, 52, 7);
				system("pause");
				checkpoint = checkpoint + 15;
				fraction = (checkpoint - 1) * 1000;
			}
			cnt++;
			for (int i = 2; i < 7; i++) {
				gotoXY(hOut, 52, i);
				cout << "                                    " ;
				Sleep(100);
			}
		} else if (checkpoint >= 101 && (cnt == 2 || cnt == 1 || cnt == 0)) {
			SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
			gotoXY(hOut, 52, 8);
			cout << "厉害厉害,都到达101关了,速度给您减";
			gotoXY(hOut, 52, 9);
			cout << "慢了,加油吧!游戏到达151关结束,关";
			gotoXY(hOut, 52, 10);
			cout << "卡、得分将会重置分数,重新开始后,会";
			gotoXY(hOut, 52, 11);
			cout << "赠送你20000分,直接到达21关,并且,";
			gotoXY(hOut, 52, 12);
			cout << "开始挑战模式,真人挑战电脑,加油吧,";
			gotoXY(hOut, 52, 13);
			cout << "到达以后将给您详细解释";
			gotoXY(hOut, 52, 14);
			system("pause");
			for (int i = 8; i < 15; i++) {
				gotoXY(hOut, 50, i);
				cout << "╠                                      ╣";
			}
			val = 50;
			cnt++;
		} else if (checkpoint >= 151 && (cnt == 3 || cnt == 2 || cnt == 1 || cnt == 0)) {
			while (true) {
				f1 = 4;
				SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
				for (int i = 1; i <= 14; i++) {
					gotoXY(hOut, 52, i);
					cout << "                                      " ;
				}
				gotoXY(hOut, 2, 2);
				cout << "                      ";
				sk = 1;
				SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
				gotoXY(hOut, 36, 1);
				cout << fraction;
				gotoXY(hOut, 36, 2);
				cout << checkpoint;
				for (int i = 0; i < 20; ++i) {
					for (int j = 1; j < 11; ++j) {
						map[i][j] = 0;
					}
				}
				for (int i = 0; i < 20; ++i) {
					map[i][0] = map[i][11] = 1;
				}
				for (int i = 0; i < 12; ++i) {
					map[20][i] = 1;
				}
				srand((unsigned)time(NULL));
				roundBlock(hOut, blockA);
				gotoXY(hOut, 52, 2);
				cout << "最后一关到达了,可以开启挑战模式了,";
				gotoXY(hOut, 52, 3);
				cout << "现在你有三个模式选择:1.挑战模式 2.";
				gotoXY(hOut, 52, 4);
				cout << "无尽模式 3.开始新的游戏  请选择:\n";
				while (1) {
					timee(hOut);
					gotoXY(hOut, 52, 5);
					cin >> a;
					if (a == 1 || a == 2 || a == 3) {
						break;
					} else {
						gotoXY(hOut, 52, 6);
						cout << "此模块正在开发,请重新选择!";
						Sleep(500);
						gotoXY(hOut, 52, 5);
						cout << "                                      ";
						gotoXY(hOut, 52, 6);
						cout << "                                      ";
					}
				}
				if (a == 1) {
					tz(hOut, fraction, checkpoint, val);
					print(hOut);
					fraction = 0;
					checkpoint = 1;
					score = 0;
					break;
				} else if (a == 2) {
					fraction = 0;
					checkpoint = 1;
					gotoXY(hOut, 52, 6);
					cout << "请选择下降速度:1.正常游戏下降速度";
					gotoXY(hOut, 52, 7);
					cout << "(30-50) 2.特快下降速度(10-29) ";
					gotoXY(hOut, 52, 8);
					cout << "3.魔鬼级下降速度(0-9) 4.特慢下降速";
					gotoXY(hOut, 52, 9);
					cout << "度(100-200)5.无下降速度\n";
					while (1) {
						long long b;
						gotoXY(hOut, 52, 10);
						cin >> b;
						if (b == 1) {
							times = 50;
							val = 50;
							break;
						} else if (b == 2) {
							times = 29;
							val = 29;
							break;
						} else if (b == 3) {
							times = 9;
							val = 9;
							break;
						} else if (b == 4) {
							times = 200;
							val = 200;
							break;
						} else if (b == 5) {
							times = 2100000000;
							val = 2100000000;
							break;
						} else {
							gotoXY(hOut, 52, 11);
							cout << "无法定义下降速度,请重新选择!\n";
							Sleep(1500);
							gotoXY(hOut, 52, 11);
							cout << "                                      ";
							gotoXY(hOut, 52, 10);
							cout << "                                      ";
						}
					}
					cout << "是否需要增加服务:1.需要 2.不需要\n";
					system("cls");
					print(hOut);
					break;
				} else if (a == 3) {
					for (int i = 1; i <= 19; i++) {
						gotoXY(hOut, 52, i);
						cout << "                                      ";
					}
					gotoXY(hOut, 52, 4);
					cout << "游戏即将重置分数与关卡,回到初始将会给";
					gotoXY(hOut, 52, 5);
					cout << "您20000分作为补偿,重新开始请按1 重新";
					gotoXY(hOut, 52, 6);
					cout << "选择请按2";
					gotoXY(hOut, 52, 7);
					int num;
					while (1) {
						cin >> num;
						if (num == 1) {
							fraction = 20 * 1000, checkpoint = 21;
							goto initial;
							break;
						} else if (num == 2) {
							num = 3;
							break;
						} else {
							gotoXY(hOut, 52, 7);
							cout << "                                      ";
						}
					}
				}
			}
		}
		if (a == 1) {
			if (f1 != 1) {
				if (f == 0 || f == 1 || f == 2 || f == 3 || f == 4 || f == 5 || f == 6 || f == 7) {
					if ((f == 0 && checkpoint >= 51) || (f == 1 && checkpoint >= 71) || (f == 2 && checkpoint >= 91) || (f == 3 && checkpoint >= 111) || (f == 4 && checkpoint >= 141) || (f == 5 && checkpoint >= 151) || (f == 6 && checkpoint >= 201) || (f == 7 && checkpoint >= 251)) {
						tz(hOut, fraction, checkpoint, val);
						print(hOut);
						for (int i = 0; i < 20; ++i) {
							for (int j = 1; j < 11; ++j) {
								map[i][j] = 0;
							}
						}
						for (int i = 0; i < 20; ++i) {
							map[i][0] = map[i][11] = 1;
						}
						for (int i = 0; i < 12; ++i) {
							map[20][i] = 1;
						}
						srand((unsigned)time(NULL));
						roundBlock(hOut, blockA);
						printBlock(hOut, blockA, 5, 15);
						score = 0;
					}
					SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
					gotoXY(hOut, 52, 1) ;
					cout << "              挑战模式              ";
					gotoXY(hOut, 52, 2);
					cout << "人机分数:" << score << "        ";
					gotoXY(hOut, 52, 3);
					cout << "你的分数:" << fraction;
					gotoXY(hOut, 52, 4);
					cout << "人机关卡:" << gk;
					gotoXY(hOut, 52, 5);
					cout << "你的关卡:" << checkpoint;
					gotoXY(hOut, 52, 6);
					cout << "目前状态:";
					if (fraction - score > 0) {
						cout << "暂时领先" << fraction - score << "分";
					} else if (fraction - score < 0) {
						cout << "人机领先" << score - fraction << "分";
					} else if (score == fraction) {
						cout << "分数接近,加油";
					}
					printBlock(hOut, blockA, 5, 15);
				}
			}
		}
		SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
		gotoXY(hOut, 36, 1);
		cout << fraction;
		gotoXY(hOut, 36, 2);
		cout << checkpoint;
		//******最重要的方块碰撞,对于碰撞的方块的检测******
		if (check) {
			/*system("pause");
						system("pause");
						while(1){
							//TODO
						}*/
			eliminateRow(hOut, map, val, fraction, checkpoint);
			check = false;
			positionX = -3;
			positionY = 4;
			if (collisionDetection(blockA, map, positionX, positionY)) {/*对于有事是否结束的一个检测*/
				for (int i = 0; i < 4; ++i) {
					for (int j = 0; j < 4; ++j) {
						blockB[i][j] = blockA[i][j];//方块的更换,对于当前方块的进行更换到需要更新的方块
					}
				}
				roundBlock(hOut, blockA);/*当前的方块的颜色进行打印,设置为当前的颜色*/
			} else {
				for (int i = 0; i < 20; ++i) {
					for (int j = 1; j < 11; ++j) {
						map[i][j] = 0;
					}
				}
				for (int i = 0; i < 20; ++i) {
					map[i][0] = map[i][11] = 1;
				}
				for (int i = 0; i < 12; ++i) {
					map[20][i] = 1;
				}
				for (int i = 0; i < 20; ++i) {
					for (int j = 1; j < 11; ++j) {
						color_backups[i][j] = 0;
					}
				}
				for (int i = 0; i < 20; ++i) {
					color_backups[i][0] = color_backups[i][11] = 1;
				}
				for (int i = 0; i < 12; ++i) {
					color_backups[20][i] = 1;
				}
				gameOver(hOut, blockA, map);/*游戏结束的*/
				goto initial;/*回到循环的地方*/
			}
		}
		printBlock(hOut, blockB, positionX, positionY);
		if (_kbhit()) {
			/*未检测到输入却能继续的关键*/  /*用于非阻塞地响应键盘输入事件*/
			key = _getch();
			switch (key) {
				case '1'://内挂换方块
					if (cnnt == 1) {
						check = true;
						//myRight(hOut, blockB, map, positionX, positionY);
						clearBlock(hOut, blockB, positionX, positionY);
					}
					break;
				case 72://上
					myUp(hOut, blockB, map, positionX, positionY);
					break;
				case 75://左
					myLeft(hOut, blockB, map, positionX, positionY);
					break;
				case 77://下
					myRight(hOut, blockB, map, positionX, positionY);
					break;
				case 80://右
					switch (myDown(hOut, blockB, map, positionX, positionY)) {
						case 0:
							check = false;
							break;
						case 1:
							backups_Block(positionX, positionY);
							check = true;
							break;
						case 2:
							for (int i = 0; i < 20; ++i) {
								for (int j = 1; j < 11; ++j) {
									map[i][j] = 0;
								}
							}
							for (int i = 0; i < 20; ++i) {
								map[i][0] = map[i][11] = 1;
							}
							for (int i = 0; i < 12; ++i) {
								map[20][i] = 1;
							}
							for (int i = 0; i < 20; ++i) {
								for (int j = 1; j < 11; ++j) {
									color_backups[i][j] = 0;
								}
							}
							for (int i = 0; i < 20; ++i) {
								color_backups[i][0] = color_backups[i][11] = 1;
							}
							for (int i = 0; i < 12; ++i) {
								color_backups[20][i] = 1;
							}
							gameOver(hOut, blockB, map);
							goto initial;
						default:
							break;
					}
					break;
				case 32://" "
					myStop(hOut, blockA, map);
					break;
				case 'R':
				case 'r':
					system("cls");
					print(hOut);
					//print(hOut);
					for (int i = 0; i < 20; ++i) {
						for (int j = 1; j < 11; ++j) {
							map[i][j] = 0;
						}
					}
					for (int i = 0; i < 20; ++i) {
						map[i][0] = map[i][11] = 1;
					}
					for (int i = 0; i < 12; ++i) {
						map[20][i] = 1;
					}
					for (int i = 0; i < 20; ++i) {
						for (int j = 1; j < 11; ++j) {
							color_backups[i][j] = 0;
						}
					}
					for (int i = 0; i < 20; ++i) {
						color_backups[i][0] = color_backups[i][11] = 29;
					}
					for (int i = 0; i < 12; ++i) {
						color_backups[20][i] = 29;
					}
					srand((unsigned)time(NULL));
					roundBlock(hOut, blockA);
					printBlock(hOut, blockA, 5, 15);
					cnt = 0;
					fraction = 0;
					checkpoint = 1;
					if (f >= 1 && f <= 7) {
						score = 0;
						gk = 1;
						cnt = 15648;
						break;
					}
					cnt = 0;
					break;
				case 'O':
				case 'o':
					SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
					for (int i = 2; i <= 19; i++) {
						gotoXY(hOut, 52, i);
						cout << "                                      ";
					}
					gotoXY(hOut, 52, 2);
					cout << "	    操作方法:";
					gotoXY(hOut, 56, 3);
					cout << "	↑:旋转 ↓:速降";
					gotoXY(hOut, 56, 4);
					cout << "	→:右移 ←:左移";
					gotoXY(hOut, 56, 5);
					cout << "	空格键:开始/暂停";
					gotoXY(hOut, 56, 6);
					cout << "	r:重玩";
					gotoXY(hOut, 56, 7);
					cout << "	Esc 键:退出";
					gotoXY(hOut, 52, 8);
					cout << "	    祝您游戏愉快!";
					y1 = 12;
					break;
				case 'V':
				case 'v':
					SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
					for (int i = 1; i <= 19; i++) {
						gotoXY(hOut, 52, i);
						cout << "                                      ";
					}
					g(hOut);
					y1 = 18;
					break;
				case 'q':
				case 'Q':
					y1 = 12;
					qk(hOut);
					break;
				case 27://"Esc"
					system("cls");
					freopen("D:\\elsfk\\color.txt", "w", stdout);
					for (int i = 0; i < 21; i++) {
						for (int j = 0; j < 12; j++) {
							cout << color_backups[i][j] << "	";
						}
						cout << endl;
					}
					fclose(stdout);
					freopen("D:\\elsfk\\last.txt", "w", stdout);
					for (int i = 0; i < 21; i++) {
						for (int j = 0; j < 12; j++) {
							cout << map[i][j] << "	";
						}
						cout << endl;
					}
					fclose(stdout);
					//system("start D:\\elsfk\\last.txt");
					//system("start D:\\elsfk\\color.txt");
					exit(0);
					fclose(stdout);
					freopen("D:\\elsfk\\Users.txt", "w", stdout);
					cout << fraction << " " << checkpoint << " " << val << endl;
					fclose(stdout);
					break;
				default:
					break;
			}
		}
		Sleep(20);
		if (0 == --times) {
			switch (myDown(hOut, blockB, map, positionX, positionY)) {
				case 0:
					check = false;
					break;
				case 1:
					backups_Block(positionX, positionY);
					check = true;
					break;
				case 2:
					for (int i = 0; i < 20; ++i) {
						for (int j = 1; j < 11; ++j) {
							map[i][j] = 0;
						}
					}
					for (int i = 0; i < 20; ++i) {
						map[i][0] = map[i][11] = 1;
					}
					for (int i = 0; i < 12; ++i) {
						map[20][i] = 1;
					}
					for (int i = 0; i < 20; ++i) {
						for (int j = 1; j < 11; ++j) {
							color_backups[i][j] = 0;
						}
					}
					for (int i = 0; i < 20; ++i) {
						color_backups[i][0] = color_backups[i][11] = 1;
					}
					for (int i = 0; i < 12; ++i) {
						color_backups[20][i] = 1;
					}
					gameOver(hOut, blockB, map);
					goto initial;
				default:
					break;
			}
			times = val;
		}
		if (f1 == 4) {
			score = 0;
			f1 = 2;
		}
	}
	cin.get();
	return 0;
}

到这里,本篇文章也就要结束了,在最后,给大家附上俄罗斯方块的代码链接。

俄罗斯方块的代码

标签:hOut,map,int,C++,++,block,case,讲解,方块
来源: https://blog.csdn.net/llh200804/article/details/122760448