欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

致敬黑客帝国(最初使我萌发对IT行业兴趣的引路人)——用EasyX和C实现字符雨

程序员文章站 2022-04-08 08:45:59
...

静态字符阵的创建

致敬黑客帝国(最初使我萌发对IT行业兴趣的引路人)——用EasyX和C实现字符雨

#include<graphics.h>
#include<time.h>
#include<conio.h>

#define high 800
#define width 1000
#define size 25

int main()
{
	int highNum = high / size;				//宽
	int widthNum = width / size;			//长

	int CharRain[width / size][high / size];
	int CNum[width / size];					//每列的有效字符个数
	int i, j, x, y;
	srand((unsigned)time(NULL));		//设随机函数seed

	for(i = 0; i < widthNum; i++)
	{
		CNum[i] = (rand() % (highNum * 9 / 10)) + highNum / 10;
		for(j = 0; j < CNum[i]; j++)
			CharRain[j][i] = (rand() % 26) + 65;	//产生A~Z的random ASCII码
	}

	initgraph(width, high);
	BeginBatchDraw();
	setfont(25, 10, "Courier");
	setcolor(GREEN);

	for(i = 0; i < widthNum; i++)
	{
		x = i * size;
		for(j = 0; j < CNum[i]; j++)
		{
			y = j * size;
			outtextxy(x, y, CharRain[j][i]);
		}
	}

	FlushBatchDraw();
	EndBatchDraw();
	getch();
	closegraph();
	return 0;
}

注意:由于这里定义了随机种子,所以每次运行得到的字符阵是不一样的

用代码求一场字符雨

致敬黑客帝国(最初使我萌发对IT行业兴趣的引路人)——用EasyX和C实现字符雨

#include<graphics.h>
#include<time.h>
#include<conio.h>

#define high 800						//高
#define width 1000						//宽
#define size 25

int main()
{
	int highNum = high / size;				 
	int widthNum = width / size;			 
	int CharRain[width / size][high / size];	//字符array
	int CNum[width / size];					//每列字符个数
	int i, j, x, y;
	srand((unsigned)time(NULL));		//设随机函数seed

	for(i = 0; i < widthNum; i++)
	{
		CNum[i] = (rand() % (highNum * 9 / 10)) + highNum / 10;
		for(j = 0; j < CNum[i]; j++)
			CharRain[j][i] = (rand() % 26) + 65;	//产生A~Z的random ASCII码
	}

	initgraph(width, high);
	BeginBatchDraw();
	setfont(25, 10, "Courier");
	setcolor(GREEN);

	while(1)
	{
		for(i = 0; i < widthNum; i++)
		{
			if(CNum[i] < highNum - 1)
			{
				for(j = CNum[i] - 1; j >= 0; j--)
				{
					CharRain[j + 1][i] = CharRain[j][i];
				}
				CharRain[0][i] = (rand() % 26) + 65;
				CNum[i]++;
			}
		}


		for(i = 0; i < widthNum; i++)
		{
			x = i * size;
			for(j = 0; j < CNum[i]; j++)
			{
				y = j * size;
				outtextxy(x, y, CharRain[j][i]);
			}
		}

		FlushBatchDraw();
		Sleep(1000);
		clearrectangle(0, 0, width - 1, high - 1);
	}
	EndBatchDraw();
	getch();
	closegraph();
	return 0;
}

终极版字符雨动画

致敬黑客帝国(最初使我萌发对IT行业兴趣的引路人)——用EasyX和C实现字符雨

注:因为黑客帝国里字符雨是绿色的,所以关于颜色的变化参数只要选择RGB里中间那个,RGB里是红绿蓝,所以选绿色

详细代码如下:

#include<graphics.h>
#include<time.h>
#include<conio.h>

#define high 800						//高
#define width 1000						//宽
#define size 25

int main()
{
	int highNum = high / size;				 
	int widthNum = width / size;			 
	int CharRain[width / size][high / size];	//字符array
	int CNum[width / size];					//每列字符个数
	int i, j, x, y;
	int color[width / size];			//存储每列的颜色
	srand((unsigned)time(NULL));		//设随机函数seed

	for(i = 0; i < widthNum; i++)
	{
		CNum[i] = (rand() % (highNum * 9 / 10)) + highNum / 10;
		color[i] = 255;					//先都定为纯绿255
		for(j = 0; j < CNum[i]; j++)
			CharRain[j][i] = (rand() % 26) + 65;	//产生A~Z的random ASCII码
	}

	initgraph(width, high);
	BeginBatchDraw();
	setfont(25, 10, "Courier");

	while(1)
	{
		for(i = 0; i < widthNum; i++)
		{
			if(CNum[i] < highNum - 1)			//使得每列字符保持饱和状态
			{
				for(j = CNum[i] - 1; j >= 0; j--)
				{
					CharRain[j + 1][i] = CharRain[j][i];		//字符向下移动
				}
				CharRain[0][i] = (rand() % 26) + 65;
				CNum[i]++;
			}
			else				//已处于饱和状态
			{
				if(color[i] > 40)
					color[i] -= 20;		//变暗
				else			//太暗了就初始化该列字符
				{
					CNum[i] = (rand() % (highNum * 9 / 10)) + highNum / 10;
					color[i] = rand() % 75 + 180;
					for(j = 0; j < CNum[i]; j++)
						CharRain[j][i] = (rand() % 26) + 65;
				}
			}
		}


		for(i = 0; i < widthNum; i++)
		{
			x = i * size;
			for(j = 0; j < CNum[i]; j++)
			{
				y = j * size;
				setcolor(RGB(0, color[i], 0));
				outtextxy(x, y, CharRain[j][i]);
			}
		}

		FlushBatchDraw();
		Sleep(100);
		clearrectangle(0, 0, width - 1, high - 1);
	}
	EndBatchDraw();
	getch();
	closegraph();
	return 0;
}

如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!

相关标签: C语言小游戏