C语言实现黑客帝国 数字雨

看了大神C语言Plus的视频。敲了这段代码。

#include
#include
#define WIDTH 1440
#define HEIGHT 810
#define STR_SIZE  20

#define STR_NUM  192  
#define STR_WIDTH  15

//定义结构体
struct rain {
	int x;
	int y;
	int speed;//下落速度
	char str[STR_SIZE];//存储数组
}rain[STR_NUM];

//创建数字
char  createch() {
	char temp = 0;
	int flag = rand() % 3;//0    1      2
	if (flag == 0) {
		temp = rand() % 26 + 'a';
	}
	else if (flag == 1) {
		temp = rand() % 26 + 'A';
	}
	else {
		temp = rand() % 10 + '0';
	}
	return temp;
}

void gameinit() {
	//初始化基础数据
	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;   //5->10
	}
	//二维数组,赋初值
	for (int i = 0; i < STR_NUM; i++)
	{
		for (int j = 0; j < STR_SIZE; j++)
		{
			rain[i].str[j] = createch();
		}
	}
}
void gamedraw() {
	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-13*j, 0));//作渐变色
		}
	}
}

void gameplay() {
	//掉落窗口外的解决-令他高度为0
	for (int i = 0; i < STR_NUM; i++)
	{
		rain[i].y += rain[i].speed;
		if (rain[i].y+STR_SIZE*STR_WIDTH> HEIGHT) {
			rain[i].y = 0;
		}
	}
}

int main() {
	initgraph(WIDTH, HEIGHT);
	srand(GetTickCount());
	gameinit();
	DWORD t1, t2;
	t2=t1 = GetTickCount();
	while (1) {
		BeginBatchDraw();//双缓冲
		gamedraw();
		if (t2 - t1 > 20) {
			gameplay();
			t1 = t2;
		}
		t2 = GetTickCount();
		EndBatchDraw();
	}
	
	return  0;
}

效果如下:

C语言实现黑客帝国 数字雨_第1张图片

 

你可能感兴趣的:(c语言,c++,算法)