21xrx.com
2024-12-27 13:42:46 Friday
登录
文章检索 我的文章 写文章
C++烟花代码:简单完整实现
2023-07-06 12:00:57 深夜i     --     --
C++ 烟花 代码 实现 完整

近年来,烟花音乐会一直是人们喜爱的文化活动,而制作烟花程序已成为了程序员们热衷的领域之一。本文将简单介绍一下使用C++实现烟花程序的过程。

首先,我们需要定义烟花的基本属性,如颜色、位置、大小、速度等。这些属性可以定义为结构体或类中的成员。例如:


struct Firework vy;//粒子速度

  double radius;//粒子半径

  double red;

接着,我们需要定义烟花的生成函数和爆炸函数。生成函数会在屏幕上产生一颗烟花,爆炸函数会将烟花粒子扩散开来,并渐渐消失。以下是生成函数和爆炸函数的代码:


void generateFirework(double x, double y){

  Firework firework;

  firework.x = x;

  firework.y = y;

  firework.vx = (rand()%10 - 5.0)/2.0;

  firework.vy = -5 - rand()%5;

  firework.radius = 3;

  firework.red = (double)rand()/RAND_MAX;

  firework.green = (double)rand()/RAND_MAX;

  firework.blue = (double)rand()/RAND_MAX;

  fireworks.push_back(firework);

}

void explodeFirework(Firework firework){

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

    Firework particle;

    particle.x = firework.x;

    particle.y = firework.y;

    double velocity = (double)rand()/RAND_MAX * 5 + 5;

    double angle = (double)rand()/RAND_MAX * 2 * 3.1415926;

    particle.vx = velocity * cos(angle);

    particle.vy = velocity * sin(angle);

    particle.radius = 2;

    particle.red = firework.red;

    particle.green = firework.green;

    particle.blue = firework.blue;

    particles.push_back(particle);

  }

}

最后,我们需要循环更新屏幕上的烟花粒子。在每一帧中,我们会先画出爆炸产生的粒子,然后更新烟花粒子的位置,并画出它们。如果烟花的速度为0,便说明烟花已经达到了顶点,需要进行爆炸操作。以下是主循环的代码:


while(!glShouldClose(window)){

  glClearColor(0.0, 0.0, 0.0, 1.0);

  glClear(GL_COLOR_BUFFER_BIT);

  

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

    particles[i].x += particles[i].vx * 0.1;

    particles[i].y += particles[i].vy * 0.1;

    particles[i].vx *= 0.985;

    particles[i].vy *= 0.985;

    glBegin(GL_POINTS);

    glColor3f(particles[i].red, particles[i].green, particles[i].blue);

    glVertex2f(particles[i].x, particles[i].y);

    glEnd();

  }

  for(int i=0; i<fireworks.size(); i++){

    fireworks[i].x += fireworks[i].vx * 0.1;

    fireworks[i].y += fireworks[i].vy * 0.1;

    fireworks[i].vy += 0.05;

    glBegin(GL_POINTS);

    glColor3f(fireworks[i].red, fireworks[i].green, fireworks[i].blue);

    glVertex2f(fireworks[i].x, fireworks[i].y);

    glEnd();

    if(fireworks[i].vy > 0 && !fireworks[i].exploded){

      explodeFirework(fireworks[i]);

      fireworks[i].exploded = true;

    }

  }

  for(int i=0; i<fireworks.size(); i++){

    if(fireworks[i].vy >= 0 && fireworks[i].exploded && particles.empty()){

      fireworks.erase(fireworks.begin() + i);

    }

  }

  glfwSwapBuffers(window);

  glfwPollEvents();

}

在定义完以上内容后,我们只需要在main函数中创建窗口并调用主循环即可。完整代码如下:


#include <GL/glew.h>

#include <GLFW/glfw3.h>

#include <stdlib.h>

#include <vector>

#include <math.h>

#include <time.h>

using namespace std;

struct Firework blue;

  bool exploded = false;

;

vector<Firework> fireworks;

vector<Firework> particles;

void generateFirework(double x, double y){

  Firework firework;

  firework.x = x;

  firework.y = y;

  firework.vx = (rand()%10 - 5.0)/2.0;

  firework.vy = -5 - rand()%5;

  firework.radius = 3;

  firework.red = (double)rand()/RAND_MAX;

  firework.green = (double)rand()/RAND_MAX;

  firework.blue = (double)rand()/RAND_MAX;

  fireworks.push_back(firework);

}

void explodeFirework(Firework firework){

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

    Firework particle;

    particle.x = firework.x;

    particle.y = firework.y;

    double velocity = (double)rand()/RAND_MAX * 5 + 5;

    double angle = (double)rand()/RAND_MAX * 2 * 3.1415926;

    particle.vx = velocity * cos(angle);

    particle.vy = velocity * sin(angle);

    particle.radius = 2;

    particle.red = firework.red;

    particle.green = firework.green;

    particle.blue = firework.blue;

    particles.push_back(particle);

  }

}

int main(){

  if(!glfwInit()){

    exit(EXIT_FAILURE);

  }

  GLFWwindow* window = glfwCreateWindow(640, 480, "Fireworks", NULL, NULL);

  if(!window){

    glfwTerminate();

    exit(EXIT_FAILURE);

  }

  glewExperimental = true;

  if(glewInit() != GLEW_OK){

    glfwTerminate();

    exit(EXIT_FAILURE);

  }

  srand(time(NULL));

  generateFirework(320, 50);

  while(!glfwWindowShouldClose(window)){

    glClearColor(0.0, 0.0, 0.0, 1.0);

    glClear(GL_COLOR_BUFFER_BIT);

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

      particles[i].x += particles[i].vx * 0.1;

      particles[i].y += particles[i].vy * 0.1;

      particles[i].vx *= 0.985;

      particles[i].vy *= 0.985;

      glBegin(GL_POINTS);

      glColor3f(particles[i].red, particles[i].green, particles[i].blue);

      glVertex2f(particles[i].x, particles[i].y);

      glEnd();

    }

    for(int i=0; i<fireworks.size(); i++){

      fireworks[i].x += fireworks[i].vx * 0.1;

      fireworks[i].y += fireworks[i].vy * 0.1;

      fireworks[i].vy += 0.05;

      glBegin(GL_POINTS);

      glColor3f(fireworks[i].red, fireworks[i].green, fireworks[i].blue);

      glVertex2f(fireworks[i].x, fireworks[i].y);

      glEnd();

      if(fireworks[i].vy > 0 && !fireworks[i].exploded){

        explodeFirework(fireworks[i]);

        fireworks[i].exploded = true;

      }

    }

    for(int i=0; i<fireworks.size(); i++){

      if(fireworks[i].vy >= 0 && fireworks[i].exploded && particles.empty()){

        fireworks.erase(fireworks.begin() + i);

      }

    }

    glfwSwapBuffers(window);

    glfwPollEvents();

  }

  glfwDestroyWindow(window);

  glfwTerminate();

  exit(EXIT_SUCCESS);

}

以上便是使用C++实现烟花程序的简单过程,读者可以根据自己的需求进行修改和扩展。

  
  

评论区

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