一、前言
C++实现的放烟花程序 用到了EGE图形库,没有的需要自行安装 可调项:背景图和背景音乐、粒子模糊度、亮度以及上升速度的参数。 实现的动态烟花非常好看,可以做给女朋友或者表白用hhh 自己做出来玩玩也挺有意思的
二、代码
fire.h
#pragma once
#ifndef FIREWORKS_H_
#define FIREWORKS_H_
#define myrand(m) ((float)rand() * m / 36565)
#include <graphics.h>
struct Speed {
double x, y;
};
struct Pos {
double x, y;
};
struct Particle
{
Pos pos;
Speed speed;
};
#define GROUND 580
class Fireworks
{
private:
static const int NUM_PARTICLE = 200;
static const double particleSpeed;
Particle p[NUM_PARTICLE];
color_t color;
int delayTime;
int riseTime;
int bloomTime;
Pos risePos;
Speed riseSpeed;
public:
Fireworks();
void init();
void update();
void draw(PIMAGE pimg = NULL);
};
#endif
main.cpp
在这里插入代码片
#include <time.h>
#include <graphics.h>
#include "fire.h"
#define NUM_FIREWORKS 10
int main()
{
initgraph(1080, 720, INIT_RENDERMANUAL);
srand((unsigned)time(NULL));
Fireworks* fireworks = new Fireworks[NUM_FIREWORKS];
PIMAGE bgPimg = newimage();
getimage(bgPimg, "夜晚.jpg");
putimage(0, 0, bgPimg);
delay_ms(0);
MUSIC bgMusic;
bgMusic.OpenFile("MELANCHOLY.mp3");
bgMusic.SetVolume(1.0f);
if (bgMusic.IsOpen()) {
bgMusic.Play(0);
}
PIMAGE cachePimg = newimage(800, 800);
int timeCount = 0;
for (; is_run(); delay_fps(60))
{
if ((++timeCount % 60 == 0) && (bgMusic.GetPlayStatus() == MUSIC_MODE_STOP)) {
bgMusic.Play(0);
}
for (int i = 0; i < NUM_FIREWORKS; i++) {
fireworks[i].update();
}
putimage(0, 0, bgPimg);
for (int i = 0; i < NUM_FIREWORKS; i++) {
fireworks[i].draw(cachePimg);
}
imagefilter_blurring(cachePimg, 0x0b, 0xff);
putimage(0, 0, cachePimg, SRCPAINT);
}
delete[] fireworks;
delimage(bgPimg);
delimage(cachePimg);
bgMusic.Close();
closegraph();
return 0;
}
fire.cpp
#include <cmath>
#define SHOW_CONSOLE
#include "fire.h"
const double Fireworks::particleSpeed = 3.0f;
Fireworks::Fireworks()
{
init();
}
void Fireworks::init()
{
delayTime = rand() % 300 + 20;
riseTime = rand() % 80 + 160;
bloomTime = 160;
risePos.x = rand() % 450 + 300.0f;
risePos.y = GROUND;
riseSpeed.y = myrand(1.0f) - 3.0f;
riseSpeed.x = myrand(0.4f) - 0.2f;
color = HSVtoRGB(myrand(360.0f), 1.0f, 1.0f);
for (int i = 0; i < NUM_PARTICLE - 1; i += 2)
{
double levelAngle = randomf() * 360;
double verticalAngle = randomf() * 360;
double xySpeed = particleSpeed * cos(verticalAngle);
p[i].speed.x = xySpeed * cos(levelAngle);
p[i].speed.y = xySpeed * sin(levelAngle);
if (i + 1 < NUM_PARTICLE)
{
p[i + 1].speed.x = -p[i].speed.x;
p[i + 1].speed.y = -p[i].speed.y;
}
}
}
void Fireworks::draw(PIMAGE pimg)
{
if (delayTime > 0)
return;
else if (riseTime > 0)
{
setfillcolor(color, pimg);
bar(risePos.x, risePos.y, risePos.x + 2, risePos.y + 2, pimg);
}
else
{
setfillcolor(color, pimg);
for (int i = 0; i < NUM_PARTICLE; i++)
{
bar(p[i].pos.x, p[i].pos.y, p[i].pos.x + 2, p[i].pos.y + 2, pimg);
}
}
}
void Fireworks::update()
{
if (delayTime-- > 0)
return;
else if (riseTime > 0)
{
risePos.x += riseSpeed.x;
risePos.y += riseSpeed.y;
riseSpeed.y += 0.005;
if (--riseTime <= 0)
{
for (int i = 0; i < NUM_PARTICLE; i++)
{
p[i].pos.x = risePos.x;
p[i].pos.y = risePos.y;
}
}
}
else if (bloomTime-- > 0)
{
for (int i = 0; i < NUM_PARTICLE; i++)
{
p[i].pos.x += p[i].speed.x;
p[i].pos.y += p[i].speed.y;
p[i].speed.y += 0.005;
p[i].speed.x *= 0.982;
p[i].speed.y *= 0.982;
}
}
else
{
init();
}
}
三、实现效果
由于烟花是动态的,截图出来不是很好看,但是做出来动态的烟花其实是很好看的,大家可以去试试 觉得这个烟花程序做的还不错的麻烦大家点个赞收藏下呀,对程序有其他改进想法或者遇到问题的话也可以在评论区里提出来
|