赞
踩
前期准备:

#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();// 关闭绘图窗口 }

完整代码,如下:
#include <graphics.h> #include <time.h> #include <stdio.h> #include <conio.h> #include <vector> using namespace std; //雪花常量 #define MAX_SNOW_SIZE 2000 //雪花总数 #define SCREEN_W 1520 // 窗口宽度 #define SCREEN_H 800 // 窗口高度 #define SOWN_RADIO 5 // 雪花大小 #define SNOW_SLEEP 0 // 雪花下落速度 //文字常量 #define DIRECTION_LEFT 1 //方向:左 #define DIRECTION_RIGHT 2 //方向:右 #define DIRECTION_RIGHT 2 //方向:右 #define MAX_TEXT_SIZE 20 //最大文字字体 #define MAX_TEXT_COUNT 5 //同时显示的文字数量 #define TEXT_CONTENT_COUNT 15 //句子数量 #define TEXT_CONTENT_BUFFER 255 //每一句最大字符个数 /// <summary> /// 雪花颗粒-结构体 /// </summary> struct Snow { double x; //坐标 int y; double step; //速度 int color; //颜色 int radui; //大小 }; /// <summary> /// 文字-结构体 /// </summary> struct TextContent { int x; //坐标 int y; int textWidth;//句子长度 int textHeight; double step; //速度 int color; //颜色 int size; //文字大小 int direction = DIRECTION_RIGHT;//方向:默认右边滚动到左边 WCHAR* content; //文字内容 }; /// <summary> /// 定义你想输出的优美句子,有没中国话,但是句子长度不能超过 TEXT_CONTENT_BUFFER 个字符,句子数量不能超过 TEXT_CONTENT_COUNT /// </summary> WCHAR CONTEXT_LIST[TEXT_CONTENT_COUNT][TEXT_CONTENT_BUFFER] = { _T("村长"), _T("圣诞节快乐"), _T("JsonLi"), _T("C语言"), _T("CSDN"), _T("Easyx"), _T("欢迎大家!"), _T("兄弟,我歇逼了"), _T("年终总结"), _T("展望2022ヾ(◍°∇°◍)ノ゙"), _T("虎虎生威"), _T("寻寻觅觅,冷冷清清是岁月衰老的凄凉"), _T("梧桐更兼细雨,到黄昏、点点滴滴。"), _T("海内存知己,天涯若比邻。"), _T("新年新气象啊,兄弟们") }; /// <summary> /// 设定你想要的字体,window 系统同名字体即可 /// </summary> LPCTSTR textType = (LPCTSTR) ("微软雅黑"); Snow snow[MAX_SNOW_SIZE]; //保存所有雪花 TextContent text[MAX_TEXT_SIZE]; //存储所有文字数据 / 雪花 /// <summary> /// 对某一个雪花赋予新的属性值 /// </summary> void setSnowValueByIndex(int index) { snow[index].x = rand() % SCREEN_W; snow[index].y = rand() % SCREEN_H; snow[index].radui = rand() % SOWN_RADIO + 1; snow[index].step = (rand() % 5000) / 1000.0 + 1; int color = (int)(snow[index].step * 255 / 6.0 + 0.5); // 速度越快,颜色越亮 snow[index].color = RGB(color, color, color); } /// <summary> /// 初始化雪花 /// </summary> void InitSnow() { for (int i = 0; i < MAX_SNOW_SIZE; i++) { setSnowValueByIndex(i); } } /// <summary> /// 移动雪花 /// </summary> void MoveSnow() { for (int i = 0; i < MAX_SNOW_SIZE; 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) { setSnowValueByIndex(i); } // 画新雪花 setfillcolor(snow[i].color); setlinecolor(snow[i].color); fillcircle((int)snow[i].x, snow[i].y, snow[i].radui); } } /// <summary> /// 初始化文字 /// </summary> /// <param name="index"></param> void setTextValueByIndex(int index) { text[index].x = SCREEN_W; text[index].y = rand() % SCREEN_H; text[index].size = rand() % MAX_TEXT_SIZE + 10;//最小文字大小 5 个像素 text[index].step = (rand()%20)+2; text[index].direction = (rand() % 2) + 1;//随机生成 1、2 text[index].content = CONTEXT_LIST[(rand() % TEXT_CONTENT_COUNT)]; text[index].textWidth = textwidth(text[index].content); text[index].textHeight = textheight(text[index].content); int color = (int)(text[index].step * 255 / 6.0 + 0.5); // 速度越快,颜色越亮 text[index].color = RGB((rand() % 255), (rand() % 255), (rand() % 255)); } /// <summary> /// 初始化文字 /// </summary> void InitText() { for (int i = 0; i < TEXT_CONTENT_COUNT; i++){ setTextValueByIndex(i); } } /// <summary> /// 移动文字 /// </summary> void MoveText() { for (int i = 0; i < TEXT_CONTENT_COUNT; i++) { // 擦掉原来的 int textHeight = textheight(text[i].content); int textWidth = textwidth(text[i].content); settextcolor(RGB(0,0,0)); settextstyle(text[i].size, text[i].size, textType); outtextxy(text[i].x, text[i].y, text[i].content); // 计算新位置 text[i].x -= text[i].step; if ((text[i].x + textWidth) <= 0) { setTextValueByIndex(i); } settextcolor(text[i].color); settextstyle(text[i].size,0,textType); outtextxy(text[i].x, text[i].y, text[i].content); } } /// <summary> /// 运行程序 /// </summary> void runProgram() { // 初始化所有雪花 InitSnow(); // 初始化所有文字 InitText(); // 绘制雪花,按任意键退出 while (!_kbhit()) { BeginBatchDraw(); MoveText(); MoveSnow(); FlushBatchDraw(); Sleep(5); } EndBatchDraw(); } /// <summary> /// 主函数 /// </summary> /// <returns></returns> int main() { srand((unsigned)time(NULL)); // 随机种子 initgraph(SCREEN_W, SCREEN_H); // 创建绘图窗口 runProgram(); // 运行 closegraph(); // 关闭绘图窗口 }
更新中… …
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。