# 樱花树粒子效果代码 (EasyX图形库) 以下是一个完整的粒子樱花树代码,使用EasyX图形库在VS2022上运行。代码实现了发光效果、树叶飘落动画,并使用了Bezier曲线绘制树干和种子填充算法。 ```html <!DOCTYPE html> <html> <head> <title>樱花树粒子效果代码</title> <style> pre { background-color: #f5f5f5; padding: 15px; border-radius: 5px; overflow-x: auto; } </style> </head> <body> <pre><code>#include <graphics.h> #include <math.h> #include <time.h> #include <vector> #include <algorithm> using namespace std; // 定义颜色常量 const COLORREF PINK = RGB(255, 192, 203); const COLORREF LIGHT_PINK = RGB(255, 182, 193); const COLORREF DARK_PINK = RGB(255, 105, 180); const COLORREF WHITE = RGB(255, 255, 255); const COLORREF BROWN = RGB(139, 69, 19); const COLORREF LIGHT_BROWN = RGB(160, 82, 45); const COLORREF GREEN = RGB(34, 139, 34); const COLORREF GLOW = RGB(255, 240, 245); // 粒子结构体 struct Particle { float x, y; float vx, vy; float size; COLORREF color; int life; bool isBlossom; }; vector<Particle> particles; // 绘制发光效果 void DrawGlow(int x, int y, COLORREF color, int radius) { for (int i = 1; i <= radius; i++) { COLORREF glowColor = RGB( GetRValue(color) * (radius - i + 1) / radius, GetGValue(color) * (radius - i + 1) / radius, GetBValue(color) * (radius - i + 1) / radius ); setfillcolor(glowColor); solidcircle(x, y, i); } } // 种子填充算法 void SeedFill(int x, int y, COLORREF fillColor, COLORREF boundaryColor) { if (x < 0 || x >= getwidth() || y < 0 || y >= getheight()) return; COLORREF current = getpixel(x, y); if (current == fillColor || current == boundaryColor) return; putpixel(x, y, fillColor); SeedFill(x + 1, y, fillColor, boundaryColor); SeedFill(x - 1, y, fillColor, boundaryColor); SeedFill(x, y + 1, fillColor, boundaryColor); SeedFill(x, y - 1, fillColor, boundaryColor); } // 绘制贝塞尔曲线树干 void DrawTreeTrunk(int x1, int y1, int x2, int y2, int thickness) { int ctrlX1 = x1 + (x2 - x1) / 3; int ctrlY1 = y1 - (y2 - y1) / 4; int ctrlX2 = x1 + 2 * (x2 - x1) / 3; int ctrlY2 = y1 - (y2 - y1) / 3; POINT pts[4] = { {x1, y1}, {ctrlX1, ctrlY1}, {ctrlX2, ctrlY2}, {x2, y2} }; setlinecolor(BROWN); setlinestyle(PS_SOLID, thickness); polybezier(pts, 4); // 填充树干 setfillcolor(LIGHT_BROWN); floodfill((x1 + x2) / 2, (y1 + y2) / 2, BROWN); } // 递归绘制树枝 void DrawBranch(int x1, int y1, int length, float angle, int thickness, int depth) { if (depth <= 0) return; int x2 = x1 + length * cos(angle); int y2 = y1 + length * sin(angle); DrawTreeTrunk(x1, y1, x2, y2, thickness); // 递归绘制子树枝 int newLength = length * 0.7; int newThickness = max(1, thickness - 1); if (depth > 3) { DrawBranch(x2, y2, newLength, angle + 0.3, newThickness, depth - 1); DrawBranch(x2, y2, newLength, angle - 0.3, newThickness, depth - 1); } DrawBranch(x2, y2, newLength, angle + 0.1, newThickness, depth - 1); DrawBranch(x2, y2, newLength, angle - 0.1, newThickness, depth - 1); } // 创建樱花粒子 void CreateBlossom(int x, int y) { Particle p; p.x = x; p.y = y; p.vx = (rand() % 100 - 50) / 100.0f; p.vy = (rand() % 50) / 100.0f + 0.1f; p.size = rand() % 3 + 1; p.life = rand() % 100 + 50; p.isBlossom = true; int colorRand = rand() % 100; if (colorRand < 70) { p.color = PINK; } else if (colorRand < 90) { p.color = LIGHT_PINK; } else { p.color = WHITE; } particles.push_back(p); } // 更新粒子 void UpdateParticles() { for (size_t i = 0; i < particles.size(); ) { particles[i].x += particles[i].vx; particles[i].y += particles[i].vy; particles[i].life--; // 添加一些随机运动 if (rand() % 10 == 0) { particles[i].vx += (rand() % 100 - 50) / 500.0f; } if (particles[i].life <= 0 || particles[i].y > getheight()) { particles.erase(particles.begin() + i); } else { i++; } } } // 绘制粒子 void DrawParticles() { for (const auto& p : particles) { if (p.isBlossom) { // 绘制发光效果 DrawGlow((int)p.x, (int)p.y, GLOW, (int)(p.size * 2)); setfillcolor(p.color); solidcircle((int)p.x, (int)p.y, (int)p.size); } } } // 创建树叶 void CreateLeaves(int x, int y) { for (int i = 0; i < 3; i++) { Particle p; p.x = x + rand() % 20 - 10; p.y = y + rand() % 20 - 10; p.vx = (rand() % 100 - 50) / 100.0f; p.vy = (rand() % 50) / 100.0f + 0.1f; p.size = rand() % 4 + 2; p.life = rand() % 150 + 100; p.isBlossom = false; p.color = GREEN; particles.push_back(p); } } int main() { // 初始化图形窗口 initgraph(800, 600); setbkcolor(WHITE); cleardevice(); // 设置随机种子 srand((unsigned)time(NULL)); // 绘制树干 int startX = getwidth() / 2; int startY = getheight() - 50; DrawBranch(startX, startY, 150, -PI / 2, 10, 8); // 创建初始樱花 for (int i = 0; i < 300; i++) { int x = rand() % getwidth(); int y = rand() % (getheight() / 2); CreateBlossom(x, y); } // 主循环 while (!_kbhit()) { cleardevice(); // 更新和绘制粒子 UpdateParticles(); DrawParticles(); // 重新绘制树干(因为cleardevice清除了它) DrawBranch(startX, startY, 150, -PI / 2, 10, 8); // 随机创建新的樱花和树叶 if (rand() % 10 == 0) { int x = getwidth() / 2 + (rand() % 200 - 100); int y = rand() % (getheight() / 2); CreateBlossom(x, y); CreateLeaves(x, y); } // 添加一些闪烁效果 if (rand() % 20 == 0) { for (int i = 0; i < 5; i++) { int x = rand() % getwidth(); int y = rand() % (getheight() / 2); DrawGlow(x, y, GLOW, rand() % 10 + 5); } } FlushBatchDraw(); Sleep(30); } closegraph(); return 0; }</code></pre> <h3>代码说明:</h3> <ol> <li>使用EasyX图形库实现樱花树效果</li> <li>采用贝塞尔曲线绘制树干和树枝,使线条更加自然</li> <li>使用种子填充算法填充树干颜色</li> <li>粒子系统模拟樱花飘落效果</li> <li>发光效果通过多层同心圆叠加实现</li> <li>随机生成不同颜色和大小的樱花粒子</li> </ol> <h3>VS2022配置步骤:</h3> <ol> <li>新建一个空项目</li> <li>下载并安装EasyX图形库</li> <li>将上述代码复制到main.cpp文件中</li> <li>项目属性 → 配置属性 → 高级 → 字符集 → 使用多字节字符集</li> <li>编译运行</li> </ol> </body> </html> ``` ## 额外需要注意的逻辑点 1. 粒子系统的生命周期管理需要仔细处理,避免内存泄漏和性能问题 2. 贝塞尔曲线的控制点位置会影响树干和树枝的自然程度,可能需要多次调整 3. 发光效果的实现会消耗较多性能,在低配置机器上可能需要减少发光半径或粒子数量 [2025-04-12 19:43:57 | AI写代码神器 | 2503点数解答]