你要的C语言实现雪花纷纷

看图

前期准备:

  1. 首先需要去安装 easyx,请移步到官网 easyx官网
  2. 打开安装到对应的开发环境

在这里插入图片描述

程序

不懂得,评论欢迎来问

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

using namespace std;

#define MAXSnow 2000	// 雪花总数
#define SCREEN_W 1000	// 窗口宽度
#define SCREEN_H 500	// 窗口高度
#define SOWN_RADIO 3	// 雪花大小
#define SNOW_SLEEP 0	// 雪花下落速度

struct Snow
{
	double	x;		//坐标
	int		y;
	double	step;	//速度
	int		color;	//颜色
	int radui;		//大小
};

Snow snow[MAXSnow];//保存所有雪花

// 初始化雪花
void InitStar(int i)
{
	snow[i].x = rand() % SCREEN_W;
	snow[i].y = rand() % SCREEN_H;
	snow[i].radui = rand() % SOWN_RADIO+1;
	snow[i].step = (rand() % 5000) / 1000.0 + 1;
	snow[i].color = (int)(snow[i].step * 255 / 6.0 + 0.5);	// 速度越快,颜色越亮
	snow[i].color = RGB(snow[i].color, snow[i].color, snow[i].color);
}

// 移动雪花
void MoveStar(int i)
{
	setlinecolor(RGB(0, 0, 0));
	setfillcolor(RGB(0, 0, 0));
	// 擦掉原来的
	fillcircle((int)snow[i].x, snow[i].y, snow[i].radui);

	// 计算新位置
	snow[i].y += snow[i].step;
	if (snow[i].y > SCREEN_H)	InitStar(i);

	// 画新雪花
	setfillcolor(snow[i].color);
	setlinecolor(snow[i].color);
	fillcircle((int)snow[i].x, snow[i].y, snow[i].radui);

}

// 主函数
int main()
{
	srand((unsigned)time(NULL));// 随机种子
	initgraph(SCREEN_W, SCREEN_H);// 创建绘图窗口

	
	// 初始化所有雪花
	for (int i = 0; i < MAXSnow; i++)
	{
		InitStar(i);
		snow[i].x = rand() % SCREEN_W;
	}

	// 绘制雪花,按任意键退出
	while (!_kbhit())
	{
		for (int i = 0; i < MAXSnow; i++)
			MoveStar(i);
		Sleep(SNOW_SLEEP);
	}
	
	
	closegraph();// 关闭绘图窗口
}

发布了89 篇原创文章 · 获赞 13 · 访问量 7704

猜你喜欢

转载自blog.csdn.net/printf123scanf/article/details/103940444