easyx放烟花加爆炸声
时间: 2023-07-09 17:33:59 浏览: 217
C++ easyX 跨年烟花代码
以下是使用EasyX库实现放烟花和爆炸声的示例代码:
```c++
#include <graphics.h>
#include <conio.h>
#include <ctime>
#include <cmath>
#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
const int MAX_FIREWORKS = 10;
const int MAX_PARTICLES = 200;
const int FIREWORKS_INTERVAL = 5000;
struct Particle {
float x, y;
float vx, vy;
float ax, ay;
float life;
COLORREF color;
};
struct Firework {
float x, y;
float vx, vy;
float ax, ay;
float life;
COLORREF color;
int particlesCount;
Particle particles[MAX_PARTICLES];
};
int main() {
initgraph(SCREEN_WIDTH, SCREEN_HEIGHT);
srand(time(NULL));
Firework fireworks[MAX_FIREWORKS];
int lastFireworkTime = timeGetTime();
int lastParticleUpdateTime = timeGetTime();
while (true) {
cleardevice();
// 更新烟花
int currentTime = timeGetTime();
if (currentTime - lastFireworkTime > FIREWORKS_INTERVAL) {
lastFireworkTime = currentTime;
// 随机生成烟花
for (int i = 0; i < MAX_FIREWORKS; i++) {
fireworks[i].x = rand() % SCREEN_WIDTH;
fireworks[i].y = SCREEN_HEIGHT;
fireworks[i].vx = 0;
fireworks[i].vy = -200 - rand() % 200;
fireworks[i].ax = 0;
fireworks[i].ay = 400;
fireworks[i].life = 2.0f + rand() % 3;
fireworks[i].color = RGB(rand() % 256, rand() % 256, rand() % 256);
fireworks[i].particlesCount = rand() % MAX_PARTICLES;
for (int j = 0; j < fireworks[i].particlesCount; j++) {
fireworks[i].particles[j].x = fireworks[i].x;
fireworks[i].particles[j].y = fireworks[i].y;
float angle = (float)(rand() % 360) / 180.0f * 3.1415926f;
float speed = 100.0f + rand() % 100;
fireworks[i].particles[j].vx = speed * cos(angle);
fireworks[i].particles[j].vy = speed * sin(angle);
fireworks[i].particles[j].ax = 0;
fireworks[i].particles[j].ay = 400;
fireworks[i].particles[j].life = 1.0f + (float)(rand() % 10) / 10.0f;
fireworks[i].particles[j].color = fireworks[i].color;
}
}
// 播放爆炸声音
PlaySound(TEXT("explosion.wav"), NULL, SND_FILENAME | SND_ASYNC);
}
// 更新粒子
float deltaTime = (currentTime - lastParticleUpdateTime) / 1000.0f;
for (int i = 0; i < MAX_FIREWORKS; i++) {
if (fireworks[i].life > 0) {
fireworks[i].vx += fireworks[i].ax * deltaTime;
fireworks[i].vy += fireworks[i].ay * deltaTime;
fireworks[i].x += fireworks[i].vx * deltaTime;
fireworks[i].y += fireworks[i].vy * deltaTime;
fireworks[i].life -= deltaTime;
for (int j = 0; j < fireworks[i].particlesCount; j++) {
if (fireworks[i].particles[j].life > 0) {
fireworks[i].particles[j].vx += fireworks[i].particles[j].ax * deltaTime;
fireworks[i].particles[j].vy += fireworks[i].particles[j].ay * deltaTime;
fireworks[i].particles[j].x += fireworks[i].particles[j].vx * deltaTime;
fireworks[i].particles[j].y += fireworks[i].particles[j].vy * deltaTime;
fireworks[i].particles[j].life -= deltaTime;
}
}
}
}
lastParticleUpdateTime = currentTime;
// 绘制烟花
for (int i = 0; i < MAX_FIREWORKS; i++) {
if (fireworks[i].life > 0) {
setlinecolor(fireworks[i].color);
setfillcolor(fireworks[i].color);
fillcircle(fireworks[i].x, fireworks[i].y, 5);
for (int j = 0; j < fireworks[i].particlesCount; j++) {
if (fireworks[i].particles[j].life > 0) {
setlinecolor(fireworks[i].particles[j].color);
setfillcolor(fireworks[i].particles[j].color);
fillcircle(fireworks[i].particles[j].x, fireworks[i].particles[j].y, 2);
}
}
}
}
Sleep(10);
}
closegraph();
return 0;
}
```
此示例代码使用随机生成的方式产生烟花,每个烟花会产生若干个粒子,随机生成粒子的速度和方向。烟花会在一定时间后消失,粒子也会在一定时间后消失。同时,播放了一个爆炸声音(需要放在与可执行文件同一目录下的名为“explosion.wav”的音频文件)。
阅读全文