跳到正文

个人记录

俄罗斯方块 V3.8

huyichen2014约 24 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 24 分钟

版本

  1. v2当前
  2. v1

俄罗斯方块Dev c代码

C++
#include<bits/stdc++.h>
#include<conio.h>
#include<windows.h>
using namespace std;

//俄罗斯方块 V3.8 注释版 2024-07-13更新
//浙江宁波江北中心小学 404班 胡翊宸
//首发于洛谷:www.luogu.com.cn/article/6jw5jrid
//发现BUG请留言或私信,求一键三连哈哈

struct rlist {//游戏记录排名结构体
	char names[20] = "______";
	int scores;
} ranklist[10];
int x = -3, y = 5;//方块顶部下落初始坐标
int speed = 1, start, level = 1, tim;//初始速度值 1,难度值 1,start 游戏开始标志,tim 已累计消行,达到 Speed * 10 触发升级
string c = "回";//中国汉字版的俄罗斯"方块",可以自定义"囧",等其他汉字
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);//windows光标控制函数
COORD coord;//光标坐标结构体
char ch;//键盘控制读取键值
int t, n1, n2, n21, f[30], score;//t 记录cpu时钟,n1 当前方块类型,n2 下一个随机方块类型,n21 下一个随机方块方向
//f[30] 是否满行标识,score 游戏分值
const bool s[7][4][4][4] = {//俄罗斯方块点阵数据,7种类型,4个方向,4 ×4点阵
	{	{{0, 0, 0, 0}, {0, 0, 0, 0}, {1, 1, 0, 0}, {1, 1, 0, 0}}, {{0, 0, 0, 0}, {0, 0, 0, 0}, {1, 1, 0, 0}, {1, 1, 0, 0}},
		{{0, 0, 0, 0}, {0, 0, 0, 0}, {1, 1, 0, 0}, {1, 1, 0, 0}}, {{0, 0, 0, 0}, {0, 0, 0, 0}, {1, 1, 0, 0}, {1, 1, 0, 0}}
	},
	{	{{0, 0, 0, 0}, {0, 0, 0, 0}, {1, 1, 0, 0}, {0, 1, 1, 0}}, {{0, 0, 0, 0}, {0, 1, 0, 0}, {1, 1, 0, 0}, {1, 0, 0, 0}},
		{{0, 0, 0, 0}, {0, 0, 0, 0}, {1, 1, 0,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{{0, 0, 0, 0}, {1, 0, 0, 0}, {1, 1, 0, 0}, {1, 0, 0, 0}},
		{{0, 0, 0, 0}, {0, 0, 0, 0}, {1, 1, 1, 0}, {0, 1, 0, 0}}, {{0, 0, 0, 0}, {0, 1, 0, 0}, {1, 1, 0, 0}, {0, 1, 0, 0}}
	},
};
int s2[22][12];//游戏区背景方块状态
const int s3[7][4] = {{1, 1, 1, 1}, {2, 1, 2, 1}, {0, 3, 0, 3}, {2, 1, 2, 1}, {2, 2, 1, 2}, {1, 2, 1, 2}, {2, 1, 2, 2}};
const int s6[7][4] = {{2, 2, 2, 2}, {2, 1, 2, 1}, {0, 3, 0, 3}, {2, 1, 2, 1}, {1, 2, 1, 2}, {1, 1, 2, 1}, {1, 1, 2, 1}};
//俄罗斯方块下落碰撞检测矩阵数据

void levelup();//升级函数
void print_all();//输出显示函数

void locate(int x, int y) {//光标移动到指定位置
	coord.X = y;
	coord.Y = x;
	SetConsoleCursorPosition(hout, coord);
}

void hide() {//隐藏光标
	CONSOLE_CURSOR_INFO cursor_info = {1, 0};
	SetConsoleCursorInfo(hout, &cursor_info);
}

void print(int x, int y) {//打印下落的方块
	for (int i = abs(min(0, x - 1)); i < 4; i++) {
		for (int j = abs(min(0, y - 1)); j < 4; j++)
			if (s[n1][n2][i][j] == 1) {
				locate(x + i, (y + j) * 2 - 1);
				cout << c;
			}
	}
}

void print2(int x, int y) {//打印右侧提示方块
	for (int i = abs(min(0, x - 1)); i < 4; i++) {
		for (int j = abs(min(0, y - 1)); j < 4; j++)
			if (s[n21][0][i][j] == 1) {
				locate(x + i, (y + j) * 2);
				cout << c;
			}
	}
}

void fall() {//消行后背景方块下降处理函数
	int p = 20, i, j;
	for (i = 19; i >= 1; i--) {
		while (p) {
			for (j = 1; j <= 10; j++) {
				if (s2[p][j]) break;
			}
			if (j <= 10) p--, i--;
			else break;
		}
		while (i) {
			for (j = 1; j <= 10; j++) {
				if (s2[i][j]) break;
			}
			if (j > 10) i--;
			else break;
		}
		for (j = 1; j <= 10; j++) {
			swap(s2[p][j], s2[i][j]);
		}
		p--;
	}
	x = -3, y = 5;
}
void clean(int x, int y) {//擦除下落的方块
	for (int i = abs(min(0, x - 1)); i < 4; i++) {
		for (int j = abs(min(0, y - 1)); j < 4; j++) {
			if (s[n1][n2][i][j] == 1) {
				locate(x + i, (y + j) * 2 - 1);
				cout << "  ";
			}
		}
	}
}

void clean2(int x, int y) {//擦除右侧提示方块
	for (int i = abs(min(0, x - 1)); i < 4; i++) {
		for (int j = abs(min(0, y - 1)); j < 4; j++) {
			locate(x + i, (y + j) * 2);
			cout << "  ";
		}
	}
}

void show() { //右侧信息显示
	locate(1, 23);
	printf("Score: %06d", score);
	locate(2, 23);
	printf("Level: < %02d >", level);
	locate(3, 23);
	printf("Speed: < %02d >", speed);
	if ( start ) {
		locate(5, 22);
		cout << "  Next block:  ";
		clean2(6, 13);//擦除当前提示方块
		print2(6, 13);//显示下一提示方块
		locate(10, 22);
		for (int i = 1; i < 16; i++) //显示升级进度指示条
			if (i <= tim * 1.5 / speed) cout << ">";
			else cout << "-";
	} else {
		locate(5, 22);
		cout << "  ←→ 选择难度";
		locate(6, 22);
		cout << "  ↑↓ 选择速度";
		locate(7, 22);
		cout << "  Enter  开始  ";
		locate(8, 22);
		cout << "  制作:胡翊宸  ";
		locate(9, 22);
		cout << "俄罗斯方块 V3.8";
	}
}
void flash(int x) {//满行消行闪烁函数
	int n = 6;
	while (n--) {
		for (int i = max(x, 1); i < min(x + 4, 21); i++) {
			if (f[i] == 1) {
				locate(i, 1);
				if (n & 1) cout << c << c << c << c << c << c << c << c << c << c;
				else cout << "                    ";
			}
		}
		Sleep(200);//暂停200毫秒
	}
	memset(f, 0, sizeof(f));
}


int find(int x) {//检查是否满行
	int i, j, b = 0, m = 0;
	for (i = max(x, 1); i < min(x + 4, 21); i++) {
		for (j = 1; j <= 10; j++) {
			if (!s2[i][j]) break;
		}
		if (j > 10) {
			b = 1;
			m++;
			f[i] = 1;
			for (int j = 1; j <= 10; j++)
				s2[i][j] = 0;
		}
	}
	if (b) {//满行得分累计
		flash(x);
		fall();
		print_all();
		if (m == 1) score += 6 * speed + level * 4;
		if (m == 2) score += 18 * speed + level * 12;
		if (m == 3) score += 36 * speed + level * 24;
		if (m == 4) score += 60 * speed + level * 40;
		//计分规则:同时消除1,2,3,4行,按1:3:6:10比例记分
		//且根据速度关和难度关,按6:4分配,计入总分score
		tim += m;//tim消行数量值,每满10 * speed触发升级
		if (tim >= 10 * speed) {
			show();
			tim -= 10 * speed;
			speed++;
			levelup();
		}
	}
	return b;
}

void print_all() {//打印背景方块
	for (int i = 1; i <= 20; i++) {
		locate(i, 1);
		for (int j = 1; j <= 10; j++) {
			if (s2[i][j] == 1) cout << c;
			else cout << "  ";
		}
	}
	find(x);//检测是否满行
	show();//更新右侧信息栏
}

void levelup() { //触发升级
	for (int i = 0; i < 3; i++) {
		locate(9, 5);
		cout << "+----------+";
		locate(10, 5);
		cout << "| Level Up |";
		locate(11, 5);
		cout << "+----------+";
		Sleep(300);
		print_all();
		Sleep(300);
	}
}

void level_init() { //初始化难度,生成随机方块
	for (int i = 22 - level; i <= 20 ; i++) {
		for (int j = 1; j <= 10; j++) {
			s2[i][j] = rand() % 2;
		}
	}
}

void print_wall() {//打印边框
	cout << "+";
	for (int i = 0; i < 20; i++) cout << "-";
	cout << "+";
	for (int i = 0; i <= 14; i++) cout << "-";
	cout << "+" << endl;
	for (int j = 0; j < 20; j++) {
		cout << "|";
		for (int i = 0; i < 20; i++) cout << " ";
		cout << "|";
		for (int i = 0; i <= 14; i++)
			if (j == 9) cout << '-';
			else cout << " ";
		cout << "|" << endl;
	}
	cout << "+";
	for (int i = 0; i < 20; i++) cout << "-";
	cout << "+";
	for (int i = 0; i <= 14; i++) cout << "-";
	cout << "+" << endl;
}

int pare(int x, int y) {//主方块移动控制检测,返回1代表可以移动,0代表无法移动
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 4; j++) {
			if (s[n1][n2][i][j] && s2[abs(x + i)][y + j]) {
				return 0;
			}
		}
	}
	return 1;
}

int paste(int x, int y) {//下落检测是否产生碰撞
	bool b = 1;
	if (x < 1 - s6[n1][n2]) b = 0;
	for (int i = abs(min(0, x)); i < 4; i++) {
		for (int j = 0; j < 4; j++) {
			s2[abs(x + i)][y + j] = s2[x + i][y + j] || s[n1][n2][i][j];
		}
	}
	return b;
}

bool cmp(rlist a, rlist b) { //分数排名之结构体排序
	return a.scores > b.scores;
}

int ranklist_in() { //读取排行榜
	FILE *file;
	char str[200];
	int lst = 0, sc = 0;
	file = fopen("ranklist.txt", "r");
	if (file == NULL) {
		locate(22, 0);
		printf("ranklist.txt文件读取失败\n");
		return 1;
	}
	while (fgets(str, sizeof(str), file) != NULL) {
		sc = 0;
		sscanf(str, "%[^:]", ranklist[lst].names);
		for (int i = 7; i < 13; i++) sc = sc * 10 + str[i] - '0';
		ranklist[lst].scores = sc;
		lst++;
	}
	fclose(file);
	return 0;
}

int ranklist_save() { //保存排行榜
	FILE *file;
	file = fopen("ranklist.txt", "w");
	if (file == NULL) {
		locate(22, 0);
		printf("ranklist.txt文件保存失败\n");
		return 1;
	}
	for (int i = 0; i < 8; i++) {
		locate(13 + i, 22);
		printf(" %-6s %06d", ranklist[i].names, ranklist[i].scores);
		fprintf(file, "%-6s:%06d\n", ranklist[i].names, ranklist[i].scores);
	}
	fclose(file);
	return 0;
}

int print_lose() {//失败重试选择
	char tr;
	string name;
	locate(9, 5);
	cout << "+----------+";
	locate(10, 5);
	cout << "|GAME OVER!|";
	locate(11, 5);
	cout << "+----------+";
	if (score > ranklist[7].scores) {
		ranklist[8].scores = score;
		locate(22, 0);
		cout << "New record!Your name:";
		getline(cin, name);
		if (name.empty()) name = "NONAME";
		strncpy(ranklist[8].names, name.c_str(), 6); //名字截取前6字节长度
		sort(ranklist, ranklist + 9, cmp);//重新排名
		ranklist_save();//保存排行榜
	}
	locate(23, 0);
	cout << "Try again? (Y/N)";
	while (1) {
		tr = getch();
		if (tr == 'y' || tr == 'Y') {
			system("cls");//命令清屏
			return 1;
		}
		if (tr == 'n' || tr == 'N') return 0;
	}
}
int input() { //按键响应
	if (start && clock() - t > 500 - speed * 30) {//控制刷新速度及游戏速度
		t = clock();//记录当下CPU时钟,单位毫秒
		if (pare(x + 1, y)) { //检测下落方块是否产生碰撞,返回1代表安全
			clean(x, y);
			x++;
			print(x, y);
		} else if (paste(x, y)) { //如果触发下落碰撞,转化为背景
			clean(x, y);
			n1 = n21;//n1为当前方块类型值,从上一轮的提示方块n21赋值
			n2 = rand() % 4;//随机产生下一个方块方向
			n21 = rand() % 7;//随机产生下一个方块类型
			print_all();
			x = -3, y = 5;
		} else {
			print_all();
			return 0;
		}
	}//以上代码为无按键输入的自由落体状态
	if (kbhit()) {//检测是否按下键盘
		ch = getch();
		if (start == 0 && ch == 13) {//游戏未开始,按下ENTER键
			start = 1;//游戏开始标识
			level_init();//初始化难度
			print_all();
			locate(6, 22);
			cout << "               ";
			locate(7, 22);
			cout << "               ";
			locate(8, 22);
			cout << "               ";
			locate(9, 22);
			cout << "               ";//清空信息栏
			show();
		}
		if ( ch == -32) {//方向控制键按下,产生两个ch值,-32为系统键值,需要过滤
			ch = getch();//读取第二个真实键值
			switch (ch) {
				case 72://方向键↑
					if (start == 0) {//游戏开始前选择关卡
						speed = speed % 12 + 1;
						show();
						break;
					}
					clean(x, y);//游戏中控制方块变形
					n2 = (n2 + 1) % 4;
					if (!pare(x, y)) {//未检测到碰撞
						if (y + s3[n1][n2] <= 10) n2 = (n2 + 3) % 4;//这里超难!!检测右边界,是否有空间让它变形
						else if (!pare(x, y - (y + s3[n1][n2] - 10))) n2 = (n2 + 3) % 4;//这里超难!!检测背景障碍物,是否有空间让它变形
						else y -= (y + s3[n1][n2] - 10);//这里超难!!变形后如果超出右边界,自动往左弹开,参考竖棍靠右边界变形效果
					}
					print(x, y);
					break;
				case 80://方向键↓可触发无延时加速下落效果
					if (start == 0) {//游戏开始前选择关卡
						speed = speed-- == 1 ? 12 : speed;
						show();
						break;
					}
					if (pare(x + 1, y)) {
						clean(x, y);//游戏中控制方块变形
						x++;
						print(x, y);
					} else if (paste(x, y)) {//如果触发下落碰撞,转化为背景方块
						clean(x, y);
						n1 = n21;//n1为当前方块类型值,从上一轮的提示方块n21赋值
						n2 = rand() % 4;//随机产生下一个方块方向
						n21 = rand() % 7;//随机产生下一个方块类型
						print_all();
						x = -3, y = 5;//初始化下一个方块的下落坐标
					} else {
						print_all();
						return 0;
					}
					break;
				case 75:
					if (start == 0) {
						level = level-- == 1 ? 12 : level;
						show();
						break;
					}
					clean(x, y);
					if (pare(x, y - 1)) y--;
					print(x, y);
					break;
				case 77:
					if (start == 0) {
						level = level % 12 + 1;
						show();
						break;
					}
					clean(x, y);
					if (pare(x, y + 1)) y++;
					print(x, y);
					break;
			}
		}
	}
	return 1;
}

void init() { //初始化游戏
	tim = 0;
	start = 0;
	score = 0;
	ranklist_in();//加载排行榜
	system("cls");//清屏
	hide();//隐藏光标
	memset(s2, 0, sizeof(s2));
	print_wall();//打印边框
	srand(time(0));//初始化随机种子
	n1 = rand() % 7;//产生当前方块类型值
	n2 = rand() % 4;//产生当前方块方向值
	n21 = rand() % 7;//产生下一个方块类型值
	for (int i = 0; i <= 11; i++)
		s2[21][i] = 1;//初始化游戏区下边界
	for (int i = 0; i <= 20; i++) {
		s2[i][0] = 1;
		s2[i][11] = 1;//初始化游戏区左右边界
	}
	show();
	locate(11, 23);
	printf("  Rank List  ");//打印排行榜
	for (int i = 0; i < 8; i++) {
		locate(13 + i, 22);
		printf(" %-6s %06d", ranklist[i].names, ranklist[i].scores);
	}
}

int main() {
	while (1) {
		init();//初始化游戏
		while (input());//检测输入
		if (print_lose() == 0) break; //失败重试选择
	}
	return 0;
}
原文