vc++2010的烟花代码
时间: 2024-03-18 22:35:18 浏览: 12
根据提供的引用内容,以下是VC++2010的烟花代码:
```cpp
#include <iostream>
#include <graphics.h>
#include <time.h>
#include <math.h>
using namespace std;
const int WIDTH = 800;
const int HEIGHT = 600;
const int MAX_PARTICLES = 1000;
const int MAX_FIREWORKS = 20;
struct Particle {
float x, y;
float vx, vy;
float ax, ay;
float age;
float life;
int r, g, b;
};
struct Firework {
Particle particles[MAX_PARTICLES];
float x, y;
float vx, vy;
float age;
float life;
int r, g, b;
bool exploded;
};
void initFirework(Firework& firework) {
firework.x = rand() % WIDTH;
firework.y = HEIGHT;
firework.vx = 0;
firework.vy = -rand() % 5 - 1;
firework.age = 0;
firework.life = rand() % 60 + 120;
firework.r = rand() % 256;
firework.g = rand() % 256;
firework.b = rand() % 256;
firework.exploded = false;
for (int i = 0; i < MAX_PARTICLES; i++) {
firework.particles[i].x = firework.x;
firework.particles[i].y = firework.y;
firework.particles[i].vx = (rand() % 10 - 5) / 10.0;
firework.particles[i].vy = (rand() % 10 - 5) / 10.0;
firework.particles[i].ax = 0;
firework.particles[i].ay = 0.05;
firework.particles[i].age = 0;
firework.particles[i].life = rand() % 30 + 60;
firework.particles[i].r = firework.r;
firework.particles[i].g = firework.g;
firework.particles[i].b = firework.b;
}
}
void updateFirework(Firework& firework) {
if (!firework.exploded) {
firework.x += firework.vx;
firework.y += firework.vy;
firework.vy += 0.1;
firework.age++;
if (firework.age >= firework.life) {
firework.exploded = true;
for (int i = 0; i < MAX_PARTICLES; i++) {
firework.particles[i].x = firework.x;
firework.particles[i].y = firework.y;
firework.particles[i].vx = (rand() % 10 - 5) / 10.0;
firework.particles[i].vy = (rand() % 10 - 5) / 10.0;
firework.particles[i].ax = 0;
firework.particles[i].ay = 0.05;
firework.particles[i].age = 0;
firework.particles[i].life = rand() % 30 + 60;
firework.particles[i].r = firework.r;
firework.particles[i].g = firework.g;
firework.particles[i].b = firework.b;
}
}
}
else {
for (int i = 0; i < MAX_PARTICLES; i++) {
firework.particles[i].vx += firework.particles[i].ax;
firework.particles[i].vy += firework.particles[i].ay;
firework.particles[i].x += firework.particles[i].vx;
firework.particles[i].y += firework.particles[i].vy;
firework.particles[i].age++;
if (firework.particles[i].age >= firework.particles[i].life) {
firework.particles[i].r = 0;
firework.particles[i].g = 0;
firework.particles[i].b = 0;
}
}
}
}
void drawFirework(Firework& firework) {
if (!firework.exploded) {
setcolor(COLOR(firework.r, firework.g, firework.b));
setfillstyle(SOLID_FILL, COLOR(firework.r, firework.g, firework.b));
fillellipse(firework.x, firework.y, 3, 3);
}
else {
for (int i = 0; i < MAX_PARTICLES; i++) {
setcolor(COLOR(firework.particles[i].r, firework.particles[i].g, firework.particles[i].b));
setfillstyle(SOLID_FILL, COLOR(firework.particles[i].r, firework.particles[i].g, firework.particles[i].b));
fillellipse(firework.particles[i].x, firework.particles[i].y, 2, 2);
}
}
}
int main() {
initgraph(WIDTH, HEIGHT);
srand(time(NULL));
Firework fireworks[MAX_FIREWORKS];
for (int i = 0; i < MAX_FIREWORKS; i++) {
initFirework(fireworks[i]);
}
while (true) {
cleardevice();
for (int i = 0; i < MAX_FIREWORKS; i++) {
updateFirework(fireworks[i]);
drawFirework(fireworks[i]);
}
Sleep(10);
}
closegraph();
return 0;
}
```