2024年2月5日发(作者:)
烟花雨代码c语言
intstate;//烟花的状态(1=正常,2=爆炸中,3=已消失)
int numParticles; // 烟花所分裂出来的粒子数
int color; // 烟花的颜色
int particles[MAX_PARTICLES][6]; // 烟花所分裂出来的粒子(每个粒子的颜色、位置和速度)
} Firework;
int fireworksCount = 0; // 当前烟花数
Firework fireworks[MAX_FIREWORKS]; // 所有烟花
typedef struct {
int x, y; // 爆炸中心的位置
int age; // 爆炸的年龄
int state; // 爆炸的状态(1=正常,2=已消失)
int numParticles; // 爆炸所分裂出来的粒子数
int color; // 爆炸的颜色
int particles[MAX_PARTICLES][6]; // 爆炸所分裂出来的粒子(每个粒子的颜色、位置和速度)
} Explosion;
int explosionsCount = 0; // 当前爆炸数
Explosion explosions[MAX_EXPLOSIONS]; // 所有爆炸
int screenWidth = 800; // 屏幕宽度
int screenHeight = 600; // 屏幕高度
- 1 -
int main() {
initwindow(screenWidth, screenHeight, '烟花雨代码C语言');
srand(time(NULL));
while (1) {
cleardevice();
// 生成烟花
if (fireworksCount < MAX_FIREWORKS) {
fireworks[fireworksCount].x = rand() % screenWidth;
fireworks[fireworksCount].y = screenHeight;
fireworks[fireworksCount].vx = (rand() % 5 - 2) * 2;
fireworks[fireworksCount].vy = -(rand() % 10 + 10);
fireworks[fireworksCount].fuse = rand() % 30 + 30;
fireworks[fireworksCount].age = 0;
fireworks[fireworksCount].state = 1;
fireworks[fireworksCount].numParticles = rand() % 30 + 70;
fireworks[fireworksCount].color = rand() % 15 + 1;
for (int i = 0; i < fireworks[fireworksCount].numParticles;
i++) {
fireworks[fireworksCount].particles[i][0] = rand() % 15 +
1;
fireworks[fireworksCount].particles[i][1] =
- 2 -
fireworks[fireworksCount].x;
fireworks[fireworksCount].particles[i][2] =
fireworks[fireworksCount].y;
fireworks[fireworksCount].particles[i][3] = (rand() % 5 -
2) * 2;
fireworks[fireworksCount].particles[i][4] = -(rand() % 10
+ 10);
fireworks[fireworksCount].particles[i][5] = rand() % 30 +
30;
}
fireworksCount++;
}
// 更新烟花状态和位置
for (int i = 0; i < fireworksCount; i++) {
if (fireworks[i].state == 1) {
fireworks[i].x += fireworks[i].vx;
fireworks[i].y += fireworks[i].vy;
fireworks[i].vy += 1;
fireworks[i].fuse--;
if (fireworks[i].fuse <= 0) {
fireworks[i].state = 2;
if (explosionsCount < MAX_EXPLOSIONS) {
- 3 -
explosions[explosionsCount].x = fireworks[i].x;
explosions[explosionsCount].y = fireworks[i].y;
explosions[explosionsCount].age = 0;
explosions[explosionsCount].state = 1;
explosions[explosionsCount].numParticles =
fireworks[i].numParticles;
explosions[explosionsCount].color = fireworks[i].color;
for (int j = 0; j <
explosions[explosionsCount].numParticles; j++) {
explosions[explosionsCount].particles[j][0] = rand() % 15
+ 1;
explosions[explosionsCount].particles[j][1] =
explosions[explosionsCount].x;
explosions[explosionsCount].particles[j][2] =
explosions[explosionsCount].y;
explosions[explosionsCount].particles[j][3] = (rand() % 5
- 2) * 2;
explosions[explosionsCount].particles[j][4] = (rand() % 5
- 2) * 2;
explosions[explosionsCount].particles[j][5] = rand() % 30
+ 30;
}
- 4 -
explosionsCount++;
}
}
} else if (fireworks[i].state == 2) {
fireworks[i].age++;
if (fireworks[i].age >= 30) {
fireworks[i].state = 3;
}
}
}
// 更新爆炸状态和位置
for (int i = 0; i < explosionsCount; i++) {
if (explosions[i].state == 1) {
explosions[i].age++;
if (explosions[i].age >= 30) {
explosions[i].state = 2;
}
}
}
// 绘制烟花和爆炸
for (int i = 0; i < fireworksCount; i++) {
if (fireworks[i].state == 1) {
- 5 -
setcolor(fireworks[i].color);
line(fireworks[i].x, fireworks[i].y, fireworks[i].x +
fireworks[i].vx, fireworks[i].y + fireworks[i].vy);
} else if (fireworks[i].state == 2) {
for (int j = 0; j < fireworks[i].numParticles; j++) {
setcolor(fireworks[i].particles[j][0]);
line(fireworks[i].particles[j][1],
fireworks[i].particles[j][2], fireworks[i].particles[j][1] +
fireworks[i].particles[j][3], fireworks[i].particles[j][2] +
fireworks[i].particles[j][4]);
}
}
}
for (int i = 0; i < explosionsCount; i++) {
if (explosions[i].state == 1) {
for (int j = 0; j < explosions[i].numParticles; j++) {
setcolor(explosions[i].color);
line(explosions[i].particles[j][1],
explosions[i].particles[j][2], explosions[i].particles[j][1]
+ explosions[i].particles[j][3],
explosions[i].particles[j][2] +
explosions[i].particles[j][4]);
- 6 -
}
}
}
// 移除已消失的烟花和爆炸
for (int i = 0; i < fireworksCount; i++) {
if (fireworks[i].state == 3) {
for (int j = i; j < fireworksCount - 1; j++) {
fireworks[j] = fireworks[j + 1];
}
fireworksCount--;
i--;
}
}
for (int i = 0; i < explosionsCount; i++) {
if (explosions[i].state == 2) {
for (int j = i; j < explosionsCount - 1; j++) {
explosions[j] = explosions[j + 1];
}
explosionsCount--;
i--;
}
}
- 7 -
delay(20);
}
return 0;
}。
- 8 -
发布者:admin,转转请注明出处:http://www.yc00.com/news/1707120298a1477988.html
评论列表(0条)