基于easyX实现俄罗斯方块

一:效果展示

 

二:环境准备

1,安装VS,目前最新版本是VS2022,安装方法可以参考我的博客

手把手教你安装VS2022_ftzchina的博客-CSDN博客一:官网下载地址Visual Studio: 面向软件开发人员和 Teams 的 IDE 和代码编辑器Visual Studio 开发工具和服务让任何开发人员在任何平台和语言的应用开发都更加轻松。 随时随地免费使用代码编辑器或 IDE 进行开发。https://visualstudio.microsoft.com/zh-hans/?rr=https://www.microsoft.com/zh-cn/二:下载VS下载选择的时候需要注意有三个选项,Professional是个人试用版,Ente..https://blog.csdn.net/qq_27071221/article/details/123387110?spm=1001.2014.3001.55022,安装easyX图形库

2.1 官网下载安装程序

EasyX Graphics Library for C++EasyX Graphics Library 是针对 Visual C++ 的绘图库,支持 VC6.0 ~ VC2019,简单易用,学习成本极低,应用领域广泛。目前已有许多大学将 EasyX 应用在教学当中。https://easyx.cn/

 2.2 安装easyX

我本地已经提前安装好了VS2022,这时候安装就非常简单了,直接点击安装即可

         我原来用的是vscode搞了很长时间,easyX根本不能识别,当然网上也有很多资料怎么手动去安装,我也尝试了尽力了,还是不行,最后还是选择安装vs2022,图省事的用easyX可以先安装vs,然后安装easyX就可以直接用了,当然有大神怎么把vscode和easyX结合起来的,也可以教教我

 三:俄罗斯方块游戏代码

 下面的代码来自,里面也有代码的详细解释C语言实现俄罗斯方块_霸道小明的博客-CSDN博客_c语言设计俄罗斯方块代码使用C语言完成俄罗斯方块。《俄罗斯方块》的基本规则是移动、旋转和摆放游戏自动输出的各种方块,使之排列成完整的一行或多行并且消除得分。由小方块组成的不同形状的板块陆续从屏幕上方落下来,玩家通过调整板块的位置和方向,使它们在屏幕底部拼出完整的一条或几条。这些完整的横条会随即消失,给新落下来的板块腾出空间,与此同时,玩家得到分数奖励。没有被消除掉的方块不断堆积起来,一旦堆到屏幕顶端,玩家便告输,游戏结束。顾名思义,俄罗斯方块自然是俄罗斯人发明的。https://blog.csdn.net/qq_54169998/article/details/122800521

 不过在我本地还是微调了一把,刚运行有错误

outtextxy和setfont没有参数匹配的重载函数

 修正方法:字符集需要改成使用多字节字符,在工程点右键,最下面的属性

点高级, 字符集改成使用多字节字符

 另外方向键也不好使,可能是平台的原因,我的是win10,需要调整方向键的宏值

 假如你也遇到了方向键不好使的问题,可以将方向键的值打出来,然后做出调整

 可直接运行的代码

#include<graphics.h>
#include<stdio.h>
#include<time.h>
#include<conio.h>	//kbhit()


int score = 0;	//总分
int rank = 0;	//等级

#define BLOCK_COUNT 5
#define BLOCK_WIDTH 5
#define BLOCK_HEIGHT 5

#define UNIT_SIZE 20	//小方块宽度

#define START_X 130		//方块降落框,方块降落起始位置
#define START_Y 30

#define KEY_UP  72//用户操作
#define KEY_LEFT 75
#define KEY_RIGHT 77
#define KEY_DOWN 80
#define KEY_SPACE 32

#define MinX 30		//游戏左上角位置
#define MinY 30
int speed = 500;	//方块降落速度


int NextIndex = -1;		//下一个方块
int BlockIndex = -1;		//当前方块

typedef enum {		//方块朝向
	BLOCK_UP,
	BLOCK_RIGHT,
	BLOCK_LEFT,
	BLOCK_DOWN
}block_dir_t;

typedef enum {		//方块移动方向
	MOVE_DOWN,
	MOVE_LEFT,
	MOVE_RIGHT
}move_dir_t;

//方块颜色
int color[BLOCK_COUNT] = {
	GREEN,
	CYAN,
	MAGENTA,
	YELLOW,
	BROWN
};
int visit[30][15];	//访问数组visit[i][j] = 1表示该位置有方块
int markColor[30][15];	//对应位置颜色
int block[BLOCK_COUNT * 4][BLOCK_WIDTH][BLOCK_HEIGHT] = {
	// | 形方块
	{ 0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0 },
	{ 0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0 },
	{ 0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0 },
	{ 0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0 },
	// L 形方块
   { 0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0,0 },
   { 0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,1,0,0,0,0,0,0,0,0 },
   { 0,0,0,0,0,0,1,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0 },
   { 0,0,0,0,0,0,0,0,1,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0 },
   // 田 形方块
   { 0,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0 },
   { 0,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0 },
   { 0,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0 },
   { 0,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0 },
   // T 形方块
   { 0,0,0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0 },
   { 0,0,0,0,0,0,0,0,1,0,0,0,1,1,0,0,0,0,1,0,0,0,0,0,0 },
   { 0,0,0,0,0,0,0,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0 },
   { 0,0,0,0,0,0,1,0,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0,0,0 },
   // Z 形方块
   { 0,0,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0 },
   { 0,0,0,0,0,0,0,1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0,0,0 },
   { 0,0,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0 },
   { 0,0,0,0,0,0,0,1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0,0,0 },
};

/***************************
 * 功能:欢迎页面
 * 输入:
 *		无
 * 返回:
 *		无
 **************************/
void welcome() {
	//1.初始化画布
	initgraph(550, 660);
	//2.设置窗口标题
	HWND window = GetHWnd();//获得窗口,获得当前窗口
	SetWindowText(window, _T("俄罗斯方块"));	//设置标题

	//3.设置游戏初始页面
	setfont(40, 0, _T("微软雅黑"));		//设置文本的字体样式(高,宽(0表示自适应),字体)
	setcolor(WHITE);	// 设置颜色
	outtextxy(205, 200, _T("俄罗斯方块"));

	setfont(20, 0, _T("楷体"));
	setcolor(WHITE);	// 设置颜色
	outtextxy(150, 300, _T("支援俄罗斯,从俄罗斯方块开始"));

	Sleep(3000);
}

/***************************
 * 功能:初始化游戏场景
 * 输入:
 *		无
 * 返回:
 *		无
 **************************/
void initGameSceen() {
	char str[16];	//存放分数
	//1.清屏
	cleardevice();
	//2.画场景
	rectangle(27, 27, 336, 635);	//方块降落框外框
	rectangle(29, 29, 334, 633);	//方块降落框内框
	rectangle(370, 50, 515, 195);	//方块提示框

	setfont(24, 0, _T("楷体"));		//写“下一个”
	setcolor(LIGHTGRAY);	//灰色
	outtextxy(405, 215, _T("下一个:"));

	setcolor(RED);					//写分数
	outtextxy(405, 280, _T("分数:"));

	//按指定格式,将score写入str
	sprintf_s(str, 16, "%d", score);
	//这里设置字符集为多字符,保证outtextxy可以写出变量str
	outtextxy(415, 310, str);

	outtextxy(405, 375, _T("等级:"));	//等级

	//按指定格式,将rank写入str
	sprintf_s(str, 16, "%d", rank);
	//这里设置字符集为多字符,保证outtextxy可以写出变量str
	outtextxy(415, 405, str);

	setcolor(LIGHTBLUE);	//操作说明
	outtextxy(390, 475, "操作说明:");
	outtextxy(390, 500, "↑: 旋转");
	outtextxy(390, 525, "↓: 下降");
	outtextxy(390, 550, "←: 左移");
	outtextxy(390, 575, "→: 右移");
	outtextxy(390, 600, "空格: 暂停");
}

/*****************************************
 * 功能:清空方块提示框里的方块
 * 输入:
 *		无
 * 返回:
 *		无
 ****************************************/
void clearBlock() {
	setcolor(BLACK);
	setfont(23, 0, "楷体");
	for (int i = 0; i < BLOCK_HEIGHT; ++i) {
		for (int j = 0; j < BLOCK_WIDTH; ++j) {
			int x = 391 + j * UNIT_SIZE;
			int y = 71 + i * UNIT_SIZE;
			outtextxy(x, y, "■");
		}
	}
}

/*****************************************
 * 功能:清除降落过程中的方块
 * 输入:
 *		x,y - 方块的坐标(二维数组左上角位置)
 *		block_dir_t - 方块方向
 * 返回:
 *		无
 ****************************************/
void clearBlock(int x, int y, block_dir_t blockDir) {
	setcolor(BLACK);
	//	setfont(23, 0, "楷体");
	int id = BlockIndex * 4 + blockDir;
	for (int i = 0; i < BLOCK_HEIGHT; ++i) {
		for (int j = 0; j < BLOCK_WIDTH; ++j) {
			if (block[id][i][j] == 1) {
				int x2 = x + j * UNIT_SIZE;
				int y2 = y + i * UNIT_SIZE;
				outtextxy(x2, y2, "■");
			}
		}
	}
}

/*****************************************
 * 功能:在提示框 与 降落框的起始位置画方块
 * 输入:
 *		x,y - 方块的坐标(二维数组左上角位置)
 * 返回:
 *		无
 ****************************************/
void drawBlock(int x, int y) {
	setcolor(color[NextIndex]);
	setfont(23, 0, "楷体");
	for (int i = 0; i < BLOCK_HEIGHT; ++i) {
		for (int j = 0; j < BLOCK_WIDTH; ++j) {
			if (block[NextIndex * 4][i][j] == 1) {
				int x2 = x + j * UNIT_SIZE;
				int y2 = y + i * UNIT_SIZE;
				outtextxy(x2, y2, "■");
			}
		}
	}
}

/*****************************************
 *功能:绘制下降过程中的方块
 *输入:
 *		x,y - 方块的坐标(二维数组左上角位置)
 *		block_dir_t - 方块方向
 * 返回:
 *		无
 ****************************************/
void drawBlock(int x, int y, block_dir_t dir) {
	setcolor(color[BlockIndex]);
	setfont(23, 0, "楷体");
	int id = BlockIndex * 4 + dir;
	for (int i = 0; i < BLOCK_HEIGHT; ++i) {
		for (int j = 0; j < BLOCK_WIDTH; ++j) {
			if (block[id][i][j] == 1) {
				//擦除该方块的第i行第j列
				outtextxy(x + j * UNIT_SIZE, y + i * UNIT_SIZE, "■");
			}
		}
	}
}

/*****************************************
 *功能:方块提示框中产生新方块
 *输入:
 *		无
 *返回:
 *		无
 ****************************************/
void nextblock() {
	clearBlock();
	//产生随机数,随机选择方块
	srand((unsigned)time(NULL));	//使用时间函数的返回值,来作为随机种子
	NextIndex = rand() % BLOCK_COUNT;	//产生0~5的随机数
	drawBlock(391, 71);
}

/*****************************************
 *功能:判断在指定位置向指定方向是否可以移动
 *输入:
 *		x,y - 方块位置
 *		moveDir - 下一步想要移动的方向
 *		blockDir - 当前方块的方向
 * 返回:
 *		true - 可以移动
 *		false - 不可以移动
 ****************************************/
bool moveable(int x0, int y0, move_dir_t moveDir, block_dir_t blockDir) {
	//计算方块左上角在30×15的游戏区位置(第多少行, 第多少列)
	int x = (y0 - MinY) / UNIT_SIZE;
	int y = (x0 - MinX) / UNIT_SIZE;
	int ret = 1;
	int id = BlockIndex * 4 + blockDir;
	if (moveDir == MOVE_DOWN) {
		for (int i = 0; i < BLOCK_HEIGHT; ++i) {
			for (int j = 0; j < BLOCK_WIDTH; ++j) {
				//向下不能运动的条件:实心方块已经达到底部(x+i+1==30),或者底部已有方块	
				if (block[id][i][j] == 1 &&
					(x + i + 1 == 30 || visit[x + i + 1][y + j] == 1)) {
					ret = 0;
				}
			}
		}
	}
	else if (moveDir == MOVE_LEFT) {
		for (int i = 0; i < BLOCK_HEIGHT; ++i) {
			for (int j = 0; j < BLOCK_WIDTH; ++j) {
				//向左不能运动的条件:实心方块已经达到左边界(y+j==0),或者左边已有方块
				if (block[id][i][j] == 1 &&
					(y + j <= 0 || visit[x + i][y + j - 1] == 1)) {
					ret = 0;
				}
			}
		}
	}
	else if (moveDir == MOVE_RIGHT) {
		for (int i = 0; i < BLOCK_HEIGHT; ++i) {
			for (int j = 0; j < BLOCK_WIDTH; ++j) {
				//向下不能运动的条件:实心方块已经达到右边界(y+j+1>=15),或者右边已有方块
				if (block[id][i][j] == 1 &&
					(y + j + 1 >= 15 || visit[x + i][y + j + 1] == 1)) {
					ret = 0;
				}
			}
		}
	}
	return ret;
}

/*****************************
 *功能:检测游戏是否结束
 *输入:
 *		无
 * 返回:
 *		无
 *****************************/
void failCheck() {
	//游戏结束条件是顶部新被绘制出的方块就要“固化”,顶部新绘制的方块方向朝上,运动方向朝下
	if (!moveable(START_X, START_Y, MOVE_DOWN, (block_dir_t)BLOCK_UP)) {
		setcolor(WHITE);
		setfont(45, 0, "隶体");
		outtextxy(75, 300, "Game Over!");
		Sleep(1000);
		system("pause");
		closegraph();
		exit(0);
	}
}

/**************************
 * 功能:延时等待
 * 输入:
 *
 * 返回:
 *		无
 *************************/
void wait(int interval) {
	int count = interval / 10;
	for (int i = 0; i < count; ++i) {
		Sleep(10);
		//如果休眠期间用户有按键,则结束休眠
		if (_kbhit()) {
			return;
		}
	}
}

/*****************************************
 * 功能:判断当前方块是否可以向指定方向旋转
 * 输入:
 *		x,y - 方块位置(二维数组坐标)
 *		dir - 方块旋转方向
 * 返回:
 *		true - 可以旋转
 *		false - 不可以旋转
 ****************************************/
bool rotatable(int x, int y, block_dir_t dir) {
	//首先判断是否可以继续向下移动
	if (!moveable(x, y, MOVE_DOWN, dir)) {
		return false;
	}
	int x2 = (y - MinY) / UNIT_SIZE;
	int y2 = (x - MinX) / UNIT_SIZE;
	int id = BlockIndex * 4 + dir;
	for (int i = 0; i < BLOCK_HEIGHT; ++i) {
		for (int j = 0; j < BLOCK_WIDTH; ++j) {
			//不能旋转条件:左右边界越界或者已有方块“阻挡”
			if (block[id][i][j] == 1 && (y2 + j < 0 || y2 + j >= 15 || visit[x2 + i][y2 + j] == 1)) {
				return false;
			}
		}
	}
	return true;
}

/*****************************************
 * 功能:
 * 输入:
 *
 * 返回:
 *		无
 ****************************************/
void mark(int x, int y, block_dir_t dir) {
	int id = BlockIndex * 4 + dir;
	int x2 = (y - MinY) / UNIT_SIZE;
	int y2 = (x - MinX) / UNIT_SIZE;
	for (int i = 0; i < BLOCK_HEIGHT; ++i) {
		for (int j = 0; j < BLOCK_WIDTH; ++j) {
			if (block[id][i][j] == 1) {
				visit[x2 + i][y2 + j] = 1;
				markColor[x2 + i][y2 + j] = color[BlockIndex];
			}
		}
	}
}

/*****************************************
 * 功能:读取用户操作,时时更新降落的方块
 * 输入:
 *		无
 * 返回:
 *		无
 ****************************************/
void move() {
	int x = START_X;	//方块起始位置
	int y = START_Y;
	int k = 0;
	block_dir_t blockDir = (block_dir_t)BLOCK_UP;
	int curSpeed = speed;	//定义当前方块降落速度
	//读取用户操作前判断游戏是否结束
	failCheck();
	//持续向下降落
	while (1) 
	{
		int curSpeed = speed;	//定义当前方块降落速度
		//清除方块
		clearBlock(x, k + y, blockDir);
		//判断选择的方向
		if (_kbhit()) {
			int key = _getch();
			//printf("%d\n", key);
			if (key == KEY_SPACE) {
				system("pause");
			}
			else if (key == KEY_UP) {
				block_dir_t nextDir = (block_dir_t)((blockDir + 1) % 4);
				if (rotatable(x, y + k, nextDir)) {
					blockDir = nextDir;
				}
			}
			else if (key == KEY_LEFT) {
				if (moveable(x, y + k + 20, MOVE_LEFT, blockDir)) {
					x -= UNIT_SIZE;
				}
			}
			else if (key == KEY_RIGHT) {
				if (moveable(x, y + k + 20, MOVE_RIGHT, blockDir)) {
					x += UNIT_SIZE;
				}
			}
			else if (key == KEY_DOWN) {
				curSpeed = 50;
			}
		}
		k += 20;
		//绘制方块
		drawBlock(x, y + k, blockDir);
		//休眠
		wait(curSpeed);
		//方块的固化处理,方块固定后结束循环,当前一个方块的move执行完毕
		if (!moveable(x, y + k, MOVE_DOWN, blockDir)) {
			mark(x, y + k, blockDir);
			break;
		}
	}
}

/*****************************************
 *功能:绘制刚从顶部降落的方块,更新提示框内的方块,调用方块降落函数move()
 *输入:
 *		无
 * 返回:
 *		无
 ****************************************/
void newblock() {
	BlockIndex = NextIndex;
	//绘制刚从顶部下降的方块
	drawBlock(START_X, START_Y);
	//让新出现方块暂停一会
	Sleep(200);
	//右上角区域绘制下一个方块
	nextblock();
	//方块降落
	move();
}

/*****************************************
 * 功能:消除第i行,并把上面的行都往下移
 * 输入:
 *		无
 * 返回:
 *		无
 ****************************************/
void down(int x) {
	for (int i = x; i > 0; --i) {
		for (int j = 0; j < 15; ++j) {
			if (visit[i - 1][j] == 1) {
				visit[i][j] = 1;
				markColor[i][j] = markColor[i - 1][j];
				setcolor(markColor[i][j]);
				outtextxy(20 * j + MinX, 20 * i + MinY, "■");
			}
			else {
				visit[i][j] = 0;
				setcolor(BLACK);
				outtextxy(20 * j + MinX, 20 * i + MinY, "■");
			}
		}
	}
	//清除最顶层方格
	setcolor(BLACK);
	for (int j = 0; j < 15; ++j) {
		visit[0][j] = 0;
		outtextxy(20 * j + MinX, MinY, "■");
	}
}

/*****************************************
 * 功能:更新分数
 * 输入:
 *		无
 * 返回:
 *		无
 ****************************************/
void addScore(int lines) {
	char str[32];
	score += lines * 10;
	sprintf_s(str, 32, "%d", score);
	setcolor(RED);
	outtextxy(415, 310, str);

}

/*************************
 * 功能:更新等级
 * 输入:
 *		无
 * 返回:
 *		无
 *************************/
void updateGrade() {
	//更新等级
	//假设50分一级
	rank = score / 50;
	char str[32];
	sprintf_s(str, 32, "%d", rank);
	setcolor(RED);
	outtextxy(415, 405, str);
	//更新速度
	if (speed <= 100) {
		speed = 100;
	}
	else {
		speed = 500 - rank * 20;
	}
}

/*************************
 * 功能:检查是否有满行方块
 * 输入:
 *		无
 * 返回:
 *		无
 *************************/
void check() {
	int i, j;
	int clearLines = 0;
	for (i = 29; i >= 0; i--) {
		// 检查第i行有没有满
		for (j = 0; j < 15 && visit[i][j]; j++);
		//执行到此处时,有两种情况:
		// 1. 第i行没有满,即表示有空位 此时 j<15
		// 2. 第i行已满了,此时 j>=15
		if (j >= 15) {
			// 此时,第i行已经满了,就需要消除第i行
			down(i);  //消除第i行,并把上面的行都下移
			i++;  // 因为最外层的循环中有 i--, 所以我们先i++, 使得下次循环时,再把这一行检查一下
			clearLines++;
		}
	}
	// 更新分数
	addScore(clearLines);

	// 更新等级(更新等级提示,更新速度)
	updateGrade();
}

int main() {
	welcome();
	initGameSceen();
	//产生新方块
	nextblock();
	//	system("pause");
	Sleep(800);

	//初始化访问数组
	memset(visit, 0, sizeof(visit));

	while (1) {
		newblock();
		//消除满行,并更新分数和速度
		check();
	}
	system("pause");
	closegraph();
	return 0;
}

  • 11
    点赞
  • 99
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
好的,我可以为您提供一些关于基于EasyX俄罗斯方块的代码示例。 首先,您需要安装EasyX图形库并配置好开发环境。然后,您可以创建一个新的EasyX项目,编写以下代码来实现俄罗斯方块的基本功能: ```C++ #include <graphics.h> #include <conio.h> #include <time.h> #include <stdlib.h> const int BLOCK_SIZE = 20; // 方块大小 const int BOARD_WIDTH = 10; // 棋盘宽度 const int BOARD_HEIGHT = 20; // 棋盘高度 const int BOARD_LEFT = 100; // 棋盘左侧位置 const int BOARD_TOP = 50; // 棋盘顶部位置 const int WINDOW_WIDTH = 500; // 窗口宽度 const int WINDOW_HEIGHT = 600; // 窗口高度 const int FPS = 60; // 帧率 int board[BOARD_HEIGHT][BOARD_WIDTH] = {0}; // 棋盘数组 int currentBlock[4][4] = {0}; // 当前方块数组 int currentBlockType = 0; // 当前方块类型 int currentBlockX = 0; // 当前方块x坐标 int currentBlockY = 0; // 当前方块y坐标 int score = 0; // 得分 bool gameOver = false; // 游戏是否结束 // 方块类型及其形状 int blockTypes[7][4][4] = { { { 0, 0, 0, 0 }, { 1, 1, 1, 1 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } }, { { 0, 2, 2, 0 }, { 0, 2, 2, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } }, { { 0, 3, 0, 0 }, { 3, 3, 3, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } }, { { 4, 4, 0, 0 }, { 0, 4, 4, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } }, { { 0, 5, 5, 0 }, { 5, 5, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } }, { { 6, 6, 6, 0 }, { 0, 0, 6, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } }, { { 7, 7, 7, 0 }, { 7, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } } }; // 绘制方块 void drawBlock(int x, int y, int type) { setfillcolor(type + 1); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (blockTypes[type][i][j]) { solidrectangle(BOARD_LEFT + (x + j) * BLOCK_SIZE, BOARD_TOP + (y + i) * BLOCK_SIZE, BOARD_LEFT + (x + j + 1) * BLOCK_SIZE, BOARD_TOP + (y + i + 1) * BLOCK_SIZE); } } } } // 绘制棋盘 void drawBoard() { setfillcolor(WHITE); solidrectangle(BOARD_LEFT, BOARD_TOP, BOARD_LEFT + BOARD_WIDTH * BLOCK_SIZE, BOARD_TOP + BOARD_HEIGHT * BLOCK_SIZE); for (int i = 0; i < BOARD_HEIGHT; i++) { for (int j = 0; j < BOARD_WIDTH; j++) { if (board[i][j]) { drawBlock(j, i, board[i][j] - 1); } } } } // 检查方块是否出界或与已有方块重叠 bool checkBlock(int x, int y, int type) { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (blockTypes[type][i][j]) { int newX = x + j; int newY = y + i; if (newX < 0 || newX >= BOARD_WIDTH || newY < 0 || newY >= BOARD_HEIGHT || board[newY][newX]) { return false; } } } } return true; } // 将当前方块放入棋盘中 void placeBlock() { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (blockTypes[currentBlockType][i][j]) { board[currentBlockY + i][currentBlockX + j] = currentBlockType + 1; } } } } // 移除已满行并计分 void removeFullRows() { for (int i = BOARD_HEIGHT - 1; i >= 0; i--) { bool rowFull = true; for (int j = 0; j < BOARD_WIDTH; j++) { if (!board[i][j]) { rowFull = false; break; } } if (rowFull) { for (int k = i; k > 0; k--) { for (int j = 0; j < BOARD_WIDTH; j++) { board[k][j] = board[k - 1][j]; } } i++; score += 100; } } } // 生成新的方块 void generateNewBlock() { currentBlockType = rand() % 7; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { currentBlock[i][j] = blockTypes[currentBlockType][i][j]; } } currentBlockX = BOARD_WIDTH / 2 - 2; currentBlockY = 0; if (!checkBlock(currentBlockX, currentBlockY, currentBlockType)) { gameOver = true; } } // 消息循环 void gameLoop() { while (!gameOver) { cleardevice(); drawBoard(); drawBlock(currentBlockX, currentBlockY, currentBlockType); char scoreStr[20]; sprintf_s(scoreStr, "Score: %d", score); outtextxy(10, 10, scoreStr); Sleep(1000 / FPS); if (kbhit()) { int key = getch(); switch (key) { case 'a': if (checkBlock(currentBlockX - 1, currentBlockY, currentBlockType)) { currentBlockX--; } break; case 'd': if (checkBlock(currentBlockX + 1, currentBlockY, currentBlockType)) { currentBlockX++; } break; case 's': if (checkBlock(currentBlockX, currentBlockY + 1, currentBlockType)) { currentBlockY++; } break; case 'w': int newType = (currentBlockType + 1) % 7; if (checkBlock(currentBlockX, currentBlockY, newType)) { currentBlockType = newType; } break; case ' ': while (checkBlock(currentBlockX, currentBlockY + 1, currentBlockType)) { currentBlockY++; } break; } } if (!checkBlock(currentBlockX, currentBlockY + 1, currentBlockType)) { placeBlock(); removeFullRows(); generateNewBlock(); } else { currentBlockY++; } } outtextxy(WINDOW_WIDTH / 2 - 50, WINDOW_HEIGHT / 2 - 10, "Game Over"); } // 主函数 int main() { initgraph(WINDOW_WIDTH, WINDOW_HEIGHT); srand((unsigned)time(NULL)); generateNewBlock(); gameLoop(); getch(); closegraph(); return 0; } ``` 以上代码实现了基本的俄罗斯方块游戏功能,包括绘制棋盘和方块、检查方块是否出界或与已有方块重叠、将当前方块放入棋盘中、移除已满行并计分、生成新的方块等。您可以根据需要进行修改和扩展。 希望这个示例对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ftzchina

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值