俄罗斯方块渲染类

Posted xiaoshengyaodiao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了俄罗斯方块渲染类相关的知识,希望对你有一定的参考价值。

渲染类Render

私有成员

int color;//颜色色号
int hposition;//存入地图在窗口上的横坐标
int zposition;//存入地图在窗口上的纵坐标
char g_bg[ROWS][COLS];//背景数组

成员函数

Render(int h, int z, int C = -1) { hposition = h, zposition = z, color = C; }
void paintsquare_1(HDC hdc);
void paintsquare_2(HDC hdc);
void paintsquare_3(HDC hdc);
void copyblock(Block& b);
void copybg(Block& b);
bool judgeGameover();

Render(int h, int z, int C = -1)
{ hposition = h, zposition = z, color = C; }

对于储存的画图横纵坐标进行初始化,以进行不同位置的画图进程

void Render::paintsquare_1(HDC hdc)
{
	Rectangle(hdc, hposition, zposition, hposition + 300, zposition + +600);
	int R = 0, G = 0, B = 0;
	switch (color)
	{
	case 0: R = 238, G = 248, B = 173; break;//淡黄
	case 1: R = 149; G = 240; B = 172; break;//青
	case 2: R = 245; G = 171; B = 158; break;//浅红
	case 3: R = 125; G = 190; B = 255; break;//淡蓝
	case 4: R = 250; G = 224; B = 165; break;//淡橘
	case 5: R = 255; G = 159; B = 207; break;//淡粉
	case 6: R = 208; G = 162; B = 255; break;//淡紫
	}
	HBRUSH hNewBrush = CreateSolidBrush(RGB(R, G, B));
	HBRUSH hOldBrush = (HBRUSH)SelectObject(hdc, hNewBrush);
	for (int i = 0; i < ROWS; i++)
	{
		for (int j = 0; j < COLS; j++)
		{

			if (g_bg[i][j] == 1)
			{
				Rectangle(hdc, hposition + j * 30 + 1, i * 30 + 1, hposition + j * 30 + 30 - 1, i * 30 + 30 - 1);
			}
		}
	}
	hNewBrush = (HBRUSH)SelectObject(hdc, hOldBrush);
	DeleteObject(hNewBrush);
}

对于正在下落的方块进行绘画,根据之前储存颜色型号对不同种类的方块绘不同的颜色

void Render::paintsquare_2(HDC hdc)
{
	HBRUSH hOldBrush;
	HBRUSH hNewBrush = CreateSolidBrush(RGB(99, 80, 205));
	hOldBrush = (HBRUSH)SelectObject(hdc, hNewBrush);
	for (int i = 0; i < ROWS; i++)
	{
		for (int j = 0; j < COLS; j++)
		{

			if (g_bg[i][j] == 2)
			{
				Rectangle(hdc, hposition + j * 30 + 1, i * 30 + 1, hposition + j * 30 + 30 - 1, i * 30 + 30 - 1);
			}
		}
	}
	hNewBrush = (HBRUSH)SelectObject(hdc, hOldBrush);
	DeleteObject(hNewBrush);
}

对于已经下落固定的方块进行绘画,绘以固定颜色

void Render::paintsquare_3(HDC hdc)
{
	Rectangle(hdc, hposition + 340, zposition + 250, hposition + 450, zposition + 350);
}

对于窗口上显示出来的文本画出一个框用来框上

void Render::copyblock(Block& b)
{
	for (int i = 0; i < HEIGH; i++)
	{
		if (b.shape == 6) {
			if (i == 1) continue;
			else
				for (int i = 0; i < WIDTH; i++)
				{
					b.faker_bg[0][i + 3] = b.g_square[0][i];
					g_bg[0][i + 3] = b.g_square[0][i];
				}
		}
		else
		{
			if ((g_bg[1][3] == 2 && b.g_square[1][0] == 1) || (g_bg[1][4] == 2 && b.g_square[1][1] == 1) || (g_bg[1][5] == 2 && b.g_square[1][2] == 1) || (g_bg[1][6] == 2 && b.g_square[1][3] == 1))
			{
				for (int i = 0; i < WIDTH; i++)
				{
					b.faker_bg[0][i + 3] = b.g_square[1][i];
					g_bg[0][i + 3] = b.g_square[1][i];
				}
				break;
			}
			else
			{
				for (int j = 0; j < WIDTH; j++)
				{//把初始方块同时贴在真假背景上
					b.faker_bg[i][j + 3] = b.g_square[i][j];
					g_bg[i][j + 3] = b.g_square[i][j];
				}
			}
		}
	}
	color = b.shape;//在这里进行赋值,这样方块颜色变化才比较正常
}

根据不同的形状,将方块数组中储存的数据转入block类中的假背景数组(faker_bg)中

void Render::copybg(Block& b) //把假背景复制到真背景上
{
	for (int i = ROWS - 1; i >= 0; i--)//遍历真背景数组
	{
		for (int j = 0; j < COLS; j++)
		{
			g_bg[i][j] = b.faker_bg[i][j];//把假背景数组faker_bg的值赋给真背景数组g_bg
		}
	}
}

将Block类中的假背景数组复制到Render类中的真背景数组

bool Render::judgeGameover()
{
	for (int i = 0; i < COLS; i++)
	{
		if (g_bg[0][i] == 2) {
			return true;
		}
	}
	return false;
}

根据渲染类中的背景数组进行判断游戏是否结束,以最顶层即背景数组的第一行进行遍历,一旦数组中有为2的数据则判断游戏结束

以上是关于俄罗斯方块渲染类的主要内容,如果未能解决你的问题,请参考以下文章

C++俄罗斯方块

java的俄罗斯方块代码

求用JAVA编写俄罗斯方块游戏的源代码

DirectX 12 版 俄罗斯方块

Tetris(俄罗斯方块)

Tetris(俄罗斯方块)