graphics.h实现代码雨

网上看到了html版本的就试着用C再现了一下(感觉性能上不如html版本的?)

#undef UNICODE
#include<stdio.h>
#include<Windows.h>
#include<graphics.h>
#include<conio.h>
#include<time.h>
#include<math.h>
#define GRAPH_LENGTH 1600
#define GRAPH_WIDTH 900
#define VISCOSITY 15 //稠密度
typedef struct
{
	int length;
	int count;
}CodeRainInfo;
typedef struct
{
	char ch;
	int GreenVal;
}ScreenBlock;
char randchar()
{
	char ch;
	int a=rand() % 36;
	if (a < 10)
	{
		ch = '0';
		return ch + a;
	}
	else
	{
		ch = 'A';
		return ch + a - 10;
	}
}
void InitCodeRain(CodeRainInfo&r,int len)
{
	r.count = 0;
	r.length = rand()%(len-5)+5;
}
void InitScreenBlook(ScreenBlock**&b,int row,int rank)
{
	b = (ScreenBlock**)malloc(sizeof(ScreenBlock*) * row);
	for (int i = 0; i < row; i++)
		b[i] = (ScreenBlock*)malloc(sizeof(ScreenBlock) * rank);
	for (int i = 0; i < row; i++)
		for (int j = 0; j < rank; j++)
		{
			b[i][j].ch = NULL;
			b[i][j].GreenVal =0;
		}
}
int main()
{
	initgraph(GRAPH_LENGTH, GRAPH_WIDTH);
	LOGFONT f;
	gettextstyle(&f);
	f.lfHeight = 18;
	f.lfWidth = 9;
	f.lfCharSet = DEFAULT_CHARSET;
	settextstyle(&f);
	int Row,Rank,Height,Width;//根据图框和字体大小划分的列表块
	ScreenBlock** Screen;
	CodeRainInfo* R;
	Height = f.lfHeight;
	Width = f.lfWidth;
	Row = GRAPH_WIDTH /Height;
	Rank= GRAPH_LENGTH /Width;
	R = (CodeRainInfo*)malloc(sizeof(CodeRainInfo) * Rank);
	for (int i = 0; i < Rank; i++)
		InitCodeRain(R[i],Row);
	InitScreenBlook(Screen,Row,Rank);
	while (1)
	{
		for (int i = 0; i < Rank; i++)
		{
			Screen[R[i].count][i].ch = randchar();
			Screen[R[i].count][i].GreenVal = 255;
			R[i].count++;
			if (R[i].count == R[i].length)InitCodeRain(R[i], Row);
		}//更新
		for (int i = 0; i < Row; i++)
		{
			for (int j = 0; j <Rank; j++)
			{
				if (Screen[i][j].ch != NULL)
				{
					settextcolor(RGB(0, Screen[i][j].GreenVal, 0));
					outtextxy(j *Width, i * Height,Screen[i][j].ch);
					Screen[i][j].GreenVal -= VISCOSITY;
					if (Screen[i][j].GreenVal <0)
					{
						settextcolor(RGB(0, 0, 0));
						outtextxy(j * Width, i * Height, Screen[i][j].ch);
						Screen[i][j].ch = NULL;
					}
				}
			}
		}//一帧
		Sleep(10); //降落速度
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值