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

80行代码还原《黑客帝国》数字雨效果!基于C+EasyX实现!

程序员文章站 2022-03-06 14:26:51
...

代码不是太难,就是那几十行代码,希望对初学者有所帮助 ^ _ ^

效果如下所示:
80行代码还原《黑客帝国》数字雨效果!基于C+EasyX实现!


代码如下所示:

#include <graphics.h>
#include <Windows.h>
#include <ctime>
#include <cstdlib>

class my_ob_Rain
{
private:
	struct my_str_Rain
	{
		int x;
		int y;
	};

	const static int NUM = 20;
	my_str_Rain arr[NUM];

public:
	void InitGraSizeAndRain();
	void Init_rain(int n);
	void Print_Rain(int n);
	void Rain_Move();
};

int main()
{
	srand((unsigned)time(0));
	my_ob_Rain rain;

	rain.InitGraSizeAndRain();
	DWORD t1 = GetTickCount(), tt1;
	BeginBatchDraw();
	while (1)
	{
		tt1 = GetTickCount();
		if (tt1 - t1 > 20) {
			cleardevice();
			rain.Rain_Move();
			t1 = tt1;
			EndBatchDraw();
		}
	}

	return 0;
}


void my_ob_Rain::InitGraSizeAndRain()
{
	initgraph(640, 480);

	for (int i = 0; i < NUM; i++)
		Init_rain(i);
}


void my_ob_Rain::Init_rain(int n)
{
	arr[n].x = rand() % 640;
	arr[n].y = rand() % 480;
}

void my_ob_Rain::Print_Rain(int n)
{
	settextcolor(GREEN);
	for (int i = 0; i < 6; i++)
	{
		int x = 255 - i * 40;
		settextcolor(RGB(0, x, 0));
		outtextxy(arr[n].x, arr[n].y - i * 20, rand() % 2 + 48);
	}
}

void my_ob_Rain::Rain_Move()
{
	for (int i = 0; i < NUM; i++)
	{
		Print_Rain(i);
		arr[i].y += 5;

		if (arr[i].y > 600)
		{
			arr[i].x = rand() % 640;
			arr[i].y = rand() % 50 - 70;
		}
	}
}