C / C ++ to achieve the code rain effect in the movie Matrix

C / C ++ code rain effect

Explanation

  • Recently, I sorted out the computer information, read the previously written code, and sorted it to the blog by the way, as a backup record

  • First look at the static effect

    Picture failure

  • Need to be divided into the following steps to achieve

    • Generate code string
    • Draw the code string into the window
    • Use double buffering mechanism to avoid screen flicker
    • Move the code string down
    • Randomly select random character positions in the code string for random modification

Description of the library used


Test code

#include <stdio.h>
#include <time.h>
#include <Windows.h>
#include <graphics.h>

#define WIDTH 960
#define HEIGHT 640
#define STR_SIZE 20
#define STR_NUM 128
#define STR_WIDTH 15


typedef struct Rain
{
	int x;
	int y;
	int speed;
	TCHAR str[STR_SIZE];
}Rain; 
Rain rain[STR_NUM];

// 随机生成一个字符
char CreateCh()
{
	char ch = 0;
	int flag = rand() % 3; // 0~2
	if (flag == 0)
		ch = rand() % 26 + 'a'; // 产生小写字母a~z
	else if (flag == 1)
		ch = rand() % 26 + 'A'; // 产生大写字母A~Z
	else
		ch = rand() % 10 + '0'; // 产生数字0~9

	return ch;
}
// 初始化结构体成员
void InitRain()
{
	// 初始化字符的位置和速度
	for (int i = 0; i < STR_NUM; ++i)
	{
		rain[i].x = i*STR_WIDTH;
		rain[i].y = rand() % HEIGHT;
		rain[i].speed = rand() % 5 + 5;
	}
	// 获取随机字符填充进数组
	for (int i = 0; i < STR_NUM; i++)
	{
		for (int j = 0; j < STR_SIZE; j++)
		{
			rain[i].str[j] = CreateCh();
		}
	}
}
// 输出到窗口中
void DrawRain()
{
	cleardevice();
	for (int i = 0; i < STR_NUM; i++)
	{
		for (int j = 0; j < STR_SIZE; j++)
		{
			outtextxy(rain[i].x, rain[i].y-STR_WIDTH*j, rain[i].str[j]);
			settextcolor(RGB(0, 255-j*13, 0));
		}
	}
}
// 移动字符
void MoveRain()
{
	for (int i = 0; i < STR_NUM; i++)
	{
		rain[i].y += rain[i].speed;
		if (rain[i].y- STR_WIDTH*STR_SIZE > HEIGHT)
		{
			rain[i].y = 0;
		}
	}
}
// 随机改变字符
void ChangeCh()
{
	for (int i = 0; i < STR_NUM; i++)
	{
		rain[rand() % STR_NUM].str[rand() % STR_SIZE] = CreateCh();
	}
}
int main()
{
	srand((unsigned int)time(NULL)); // 初始化随机数种子
	initgraph(WIDTH, HEIGHT);
	InitRain();
	DWORD t1, t2;
	t1 = GetTickCount();
	t2 = t1;
	while (1)
	{
		BeginBatchDraw(); // 双缓冲机制
		DrawRain();
		ChangeCh();
		if (t2 - t1 > 20)
		{
			MoveRain();
			t1 = t2;
		}
		t2 = GetTickCount();
		EndBatchDraw();
	}
	getchar();
	closegraph();
	return 0;
}


Renderings

Picture failure

Guess you like

Origin www.cnblogs.com/veis/p/12737876.html