# 粒子樱花树代码 (使用EasyX图形库) 以下是一个完整的粒子樱花树实现,包含发光效果、树叶飘落动画,使用Bezier曲线绘制树干和树枝,种子填充算法填充树叶区域。 ```c #include <graphics.h> #include <conio.h> #include <math.h> #include <time.h> #include <stdlib.h> #define PI 3.1415926535 #define WIDTH 800 #define HEIGHT 600 #define MAX_PETALS 500 // 花瓣结构体 typedef struct { float x, y; // 位置 float vx, vy; // 速度 float life; // 生命周期 float size; // 大小 COLORREF color; // 颜色 } Petal; Petal petals[MAX_PETALS]; int petalCount = 0; // 初始化花瓣 void initPetal(Petal* p, float x, float y) { p->x = x; p->y = y; p->vx = (rand() % 100 - 50) / 50.0f; p->vy = -(rand() % 30) / 20.0f - 0.5f; p->life = rand() % 100 / 100.0f * 2.0f + 1.0f; p->size = rand() % 10 + 5; // 随机粉色系颜色 int r = 230 + rand() % 25; int g = 100 + rand() % 100; int b = 130 + rand() % 100; p->color = RGB(r, g, b); } // 更新花瓣位置 void updatePetal(Petal* p) { p->x += p->vx; p->y += p->vy; p->vy += 0.02f; // 重力 p->vx *= 0.99f; // 空气阻力 p->life -= 0.01f; } // 绘制花瓣 void drawPetal(Petal* p) { // 发光效果 for (int i = 0; i < 3; i++) { setfillcolor(RGB( GetRValue(p->color) + i * 20, GetGValue(p->color) + i * 10, GetBValue(p->color) + i * 10 )); solidcircle((int)p->x, (int)p->y, (int)(p->size * (1.0f - i * 0.2f))); } } // 种子填充算法 (泛洪填充) void floodFill(int x, int y, COLORREF oldColor, COLORREF newColor) { if (x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT) return; if (getpixel(x, y) != oldColor) return; putpixel(x, y, newColor); floodFill(x + 1, y, oldColor, newColor); floodFill(x - 1, y, oldColor, newColor); floodFill(x, y + 1, oldColor, newColor); floodFill(x, y - 1, oldColor, newColor); } // 绘制樱花树 void drawCherryBlossomTree() { // 树干 (使用Bezier曲线) POINT trunkPoints[] = { {WIDTH / 2, HEIGHT}, {WIDTH / 2 - 30, HEIGHT - 150}, {WIDTH / 2 + 20, HEIGHT - 300}, {WIDTH / 2, HEIGHT - 450} }; setlinestyle(PS_SOLID, 30); setlinecolor(RGB(101, 67, 33)); polybezier(trunkPoints, 4); // 树枝 setlinestyle(PS_SOLID, 15); for (int i = 0; i < 5; i++) { POINT branchPoints[] = { {WIDTH / 2 + (i % 2 == 0 ? -1 : 1) * (50 + rand() % 50), HEIGHT - 300 - i * 50}, {WIDTH / 2 + (i % 2 == 0 ? -1 : 1) * (150 + rand() % 50), HEIGHT - 350 - i * 50}, {WIDTH / 2 + (i % 2 == 0 ? -1 : 1) * (100 + rand() % 100), HEIGHT - 400 - i * 50} }; polybezier(branchPoints, 3); } // 树叶区域 (先绘制轮廓) setlinestyle(PS_SOLID, 1); setlinecolor(RGB(200, 200, 200)); setfillcolor(RGB(200, 200, 200)); // 树冠轮廓 (多个椭圆组成) for (int i = 0; i < 3; i++) { fillellipse( WIDTH / 2 - 150 + i * 50, HEIGHT - 500 - i * 50, WIDTH / 2 + 150 - i * 50, HEIGHT - 300 + i * 50 ); } // 使用种子填充算法填充树叶 floodFill(WIDTH / 2, HEIGHT - 400, RGB(200, 200, 200), RGB(200, 230, 230)); // 添加一些花朵 for (int i = 0; i < 100; i++) { int x = WIDTH / 2 - 100 + rand() % 200; int y = HEIGHT - 450 + rand() % 150; if (getpixel(x, y) == RGB(200, 230, 230)) { // 花朵发光效果 for (int j = 3; j > 0; j--) { setfillcolor(RGB(255, 200 - j * 30, 200 - j * 20)); solidcircle(x, y, 5 + j * 2); } // 偶尔生成飘落的花瓣 if (rand() % 20 == 0 && petalCount < MAX_PETALS) { initPetal(&petals[petalCount], x, y); petalCount++; } } } } int main() { // 初始化图形窗口 initgraph(WIDTH, HEIGHT); setbkcolor(RGB(135, 206, 235)); // 天空蓝 cleardevice(); // 随机种子 srand((unsigned)time(NULL)); // 主循环 while (!_kbhit()) { cleardevice(); // 绘制樱花树 drawCherryBlossomTree(); // 更新和绘制花瓣 for (int i = 0; i < petalCount; i++) { updatePetal(&petals[i]); drawPetal(&petals[i]); // 移除生命周期结束的花瓣 if (petals[i].life <= 0 || petals[i].y > HEIGHT) { petals[i] = petals[petalCount - 1]; petalCount--; i--; } } // 随机添加新花瓣 if (rand() % 5 == 0 && petalCount < MAX_PETALS) { initPetal(&petals[petalCount], WIDTH / 2 - 100 + rand() % 200, HEIGHT - 450 + rand() % 50); petalCount++; } // 添加地面 setfillcolor(RGB(100, 200, 100)); solidrectangle(0, HEIGHT - 20, WIDTH, HEIGHT); // 显示一些落在地上的花瓣 for (int i = 0; i < 30; i++) { int x = rand() % WIDTH; if (getpixel(x, HEIGHT - 10) == RGB(100, 200, 100)) { setfillcolor(RGB(230 + rand() % 25, 100 + rand() % 100, 130 + rand() % 100)); solidcircle(x, HEIGHT - 10, 3 + rand() % 5); } } // 刷新画面 FlushBatchDraw(); Sleep(30); } closegraph(); return 0; } ``` ## 代码说明 1. 粒子系统:使用结构体数组实现花瓣粒子系统,每个花瓣有位置、速度、生命周期等属性。 2. Bezier曲线:使用`polybezier`函数绘制树干和树枝,模拟自然生长的曲线。 3. 种子填充算法:实现`floodFill`函数用于填充树叶区域,先绘制轮廓再填充。 4. 发光效果:通过多层绘制不同大小的圆并逐渐改变颜色实现发光效果。 5. 物理模拟:花瓣受重力和空气阻力影响,实现自然飘落效果。 ## 使用说明 1. 确保已安装Visual Studio 2022和EasyX图形库。 2. 在VS2022中创建空项目,添加此代码。 3. 项目属性中设置使用多字节字符集。 4. 编译运行即可看到樱花树动画。 ## 三条额外需要注意的逻辑点 1. 种子填充算法使用递归实现,对于大区域可能导致栈溢出,可以考虑使用非递归实现优化。 2. 粒子系统使用固定大小数组,当花瓣数量超过MAX_PETALS时会丢失新花瓣,可根据需要调整大小。 3. 发光效果通过多层绘制实现,会增加绘制负担,在性能较弱的设备上可能需要减少层数。 [2025-04-06 00:51:24 | AI写代码神器 | 2213点数解答]