21xrx.com
2024-11-05 19:26:47 Tuesday
登录
文章检索 我的文章 写文章
C++烟花代码,一键复制即可使用
2023-07-05 06:48:40 深夜i     --     --
C++ 烟花 代码 复制 使用

烟花是一种节日庆典和夜幕之间很受欢迎的表演。烟花的美丽和华丽呈现了人们对生命和爱的热爱之情。而在现代数字时代,人们也可以通过计算机编程来实现烟花效果。

为了让大家更方便实现烟花效果,下面将分享一份C++烟花代码。这个代码可以简单地模拟烟花的绽放过程。只需要一键复制代码,就可以在计算机上实现美丽的烟花效果。

以下是完整的C++烟花代码:


#include <iostream>

#include <fstream>

#include <vector>

#include <cstdlib>

#include <ctime>

#include <cmath>

using namespace std;

#define GRAVITY 9.8

#define AIR_RESISTANCE 1.2

const double PI = 3.14159265358;

struct Particle vy;

class Fireworks {

private:

  Particle firework;

  vector<Particle> particles;

  double randFloat(float min, float max) {

    return ((max - min) * ((float) rand() / RAND_MAX)) + min;

  }

public:

  Fireworks(double x, double y, double z) {

    firework.x = x;

    firework.y = y;

    firework.z = z;

    firework.vx = randFloat(-5, 5);

    firework.vy = randFloat(10, 20);

    firework.vz = randFloat(-5, 5);

    firework.time = 0;

    firework.mass = 5;

    particles.clear();

  }

  int explosion(int rate, double dt) {

    firework.time += dt;

    if (firework.time > 2 && particles.empty())

      return 1;

    

    for (int i = 0; i < rate; i++) {

      double vx = randFloat(-20, 20);

      double vy = randFloat(-20, 20);

      double vz = randFloat(-20, 20);

      Particle particle;

      particle.x = firework.x;

      particle.y = firework.y;

      particle.z = firework.z;

      particle.vx = vx;

      particle.vy = vy;

      particle.vz = vz;

      particle.time = 0;

      particle.mass = 0.05;

      particles.push_back(particle);

    }

    for (vector<Particle>::iterator it = particles.begin(); it != particles.end();) {

      Particle particle = *it;

      particle.x += particle.vx * dt;

      particle.y += particle.vy * dt;

      particle.z += particle.vz * dt;

      double ax = - AIR_RESISTANCE * particle.vx * particle.vx / particle.mass;

      double ay = - AIR_RESISTANCE * particle.vy * particle.vy / particle.mass - GRAVITY;

      double az = - AIR_RESISTANCE * particle.vz * particle.vz / particle.mass;

      particle.vx += ax * dt;

      particle.vy += ay * dt;

      particle.vz += az * dt;

      particle.time += dt;

      if (particle.time > 5) {

        it = particles.erase(it); // remove particle from vector

      } else {

        it++;

      }

    }

    return 0;

  }

  vector<Particle> getParticles()

    return particles;

  

};

int main() {

  ofstream fout;

  fout.open("fireworks.obj");

  srand((unsigned int)time(NULL));

  vector<Fireworks> fireworks;

  fireworks.push_back(Fireworks(0, 0, 0));

  double time = 0;

  double dt = 0.01;

  int rate = 10;

  while (!fireworks.empty()) {

    for (vector<Fireworks>::iterator it = fireworks.begin(); it != fireworks.end();) {

      Fireworks firework = *it;

      if (firework.explosion(rate, dt)) {

        fout << "g Fireworks\n";

        vector<Particle> particles = firework.getParticles();

        for (vector<Particle>::iterator it = particles.begin(); it != particles.end(); it++) {

          Particle particle = *it;

          fout << "v " << particle.x << " " << particle.y << " " << particle.z << "\n";

        }

        for (unsigned int i = 0; i < particles.size() / 3; i++) {

          fout << "f " << i*3+1 << " " << i*3+2 << " " << i*3+3 << "\n";

        }

        it = fireworks.erase(it);

      } else {

        it++;

      }

    }

    time += dt;

  }

  fout.close();

  return 0;

}

这份代码使用了C++的随机数、向量等功能,实现了随机速度方向、空气阻力、重力等元素。通过特定的计算,可以充分模拟出烟花绽放的效果,而代码中的参数可以自行调整,以实现更加细致的烟花效果。

如果您是一名C++爱好者或是初学者,这份代码也能帮助您更好地了解C++的开发方式和思路。

总之,这份代码简单易学、功能强大,只需要一点手动修改和编译思路,就可以实现美丽绝伦的烟花效果。无论是入门学习,还是深入掌握C++编程技巧,这份代码都是您不可错过的利器。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复