Álbum del 1 de junio||C++ realiza código de fuegos artificiales dinámicos

En primer lugar, ¡les deseo a todos un feliz Día del Niño!

¡En este artículo, enviaré mis mejores deseos a todos los que tienen fuegos artificiales!

El código de fuegos artificiales utilizará la biblioteca de gráficos Easyx, que se puede descargar desde el sitio web oficial: easyx.cn icono-predeterminado.png?t=N4P3http://easyx.cn/

idea de código

1 estructura de fuegos artificiales

2 Inicializar fuegos artificiales

3 fuegos artificiales subiendo

4 explosión de fuegos artificiales

5 Dibujar los fuegos artificiales

No se requieren imágenes ni efectos de sonido (quería agregarlos, pero debe agregarlos usted mismo)

Empieza a escribir

Descargo de responsabilidad por adelantado: ¡el código es original puro!

Los archivos de encabezado y las macros que deben usarse:

1

2

3

4

5

6

7

8

#include <graphics.h>

#include <math.h>

#include <time.h>

#include <stdio.h>

#define MAXNUM 15

#define WIDTH 640

#define HEIGHT 480

#define PI 3.1415926

graphics.h Biblioteca de gráficos Easyx

math.h calcula la posición de los fuegos artificiales

time.h seleccionar semilla aleatoria

Archivo de encabezado de entrada y salida estándar stdio.h

Número MÁXIMO de fuegos artificiales

ANCHO , ALTO El ancho y alto de la ventana

PI pi constante, combinada con la biblioteca matemática para calcular la posición de los fuegos artificiales

1 estructura de fuegos artificiales

Los parámetros definidos son los siguientes:

nowx , nowy coordenadas actuales

altura de explosión final

radio de explosión de radio

explotar explosión progreso

rgb[3] color de fuegos artificiales valor rgb

color de fuegos artificiales

1

2

3

4

5

6

7

8

9

10

struct Fire

{

    int nowx;

    int nowy;

    int endy;

    int radio;

    int explode;

    int rgb[3];

    COLORREF color;

}fire[MAXNUM];

2 Inicializar fuegos artificiales

nowx , nowy posición aleatoria fuera de la ventana

altura aleatoria final

radio aleatorio de radio

explotar se establece inicialmente en 0

rgb[3] Algunos colores específicos se seleccionan al azar

color según el valor rgb

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

dieciséis

17

void Init()

{

    for (int i = 0; i < MAXNUM; i++)

    {

        fire[i].nowx = rand() % WIDTH;

        fire[i].nowy = HEIGHT + rand() % 250 + 50;

        fire[i].endy = rand() % 100 + 10;

        fire[i].radio = rand() % 50 + 120;

        fire[i].explode = 0;

        int c[][3] = { {255, 0, 0}, {210, 190, 255}, {255, 120, 0}, {255, 0, 150}, {255, 240, 100}, {10, 255, 255}, {160, 10, 255}, {255, 200, 60} };

        int n = rand() % 8;

        fire[i].color = RGB(c[n][0], c[n][1], c[n][2]);

        fire[i].rgb[0] = c[n][0];

        fire[i].rgb[1] = c[n][1];

        fire[i].rgb[2] = c[n][2];

    }

}

3 fuegos artificiales subiendo

La coordenada y sigue subiendo hasta que alcanza la altura de explosión.

1

2

3

4

5

6

7

for (int i = 0; i < MAXNUM; i++)

{

    if (fire[i].nowy > fire[i].endy)

    {

        fire[i].nowy -= 3;

    }

}

4 explosión de fuegos artificiales

El progreso de la explosión aumenta constantemente y, si alcanza el radio de explosión, se reinicializará. (Continuación de la declaración if anterior)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

dieciséis

17

18

else

{

    if (fire[i].explode >= fire[i].radio)

    {

        fire[i].nowx = rand() % WIDTH;

        fire[i].nowy = HEIGHT + rand() % 250 + 50;

        fire[i].endy = rand() % 100 + 10;

        fire[i].radio = rand() % 50 + 120;

        fire[i].explode = 0;

        int c[][3] = { {255, 0, 0}, {210, 190, 255}, {255, 120, 0}, {255, 0, 150}, {255, 240, 100}, {10, 255, 255}, {160, 10, 255}, {255, 200, 60} };

        int n = rand() % 8;

        fire[i].color = RGB(c[n][0], c[n][1], c[n][2]);

        fire[i].rgb[0] = c[n][0];

        fire[i].rgb[1] = c[n][1];

        fire[i].rgb[2] = c[n][2];

    }

    else fire[i].explode++;

}

5 Dibujar los fuegos artificiales

elevar

Dibuja 5 círculos, el tamaño es de grande a pequeño y el color es de oscuro a claro (aumenta el valor rgb)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

dieciséis

17

for (int i = 0; i < MAXNUM; i++)

{

    if (fire[i].nowy > fire[i].endy)

    {

        for (int size = 5; size > 0; size--)

        {

            int temp[] = { fire[i].rgb[0], fire[i].rgb[1], fire[i].rgb[2] };

            for (int k = 0; k < 3; k++)

            {

                temp[k] += 50 * (5 - size);

                if (temp[k] > 255) temp[k] = 255;

            }

            setfillcolor(RGB(temp[0], temp[1], temp[2]));

            solidcircle(fire[i].nowx, fire[i].nowy + 15*(10 - size), size);

        }

    }

}

explotar

¡Lo más destacado! ¡Lo más destacado! ¡Lo más destacado! ¡Necesita un poco de (fù) simple (zá) matemática (gāo) aprendizaje (děng) conocimiento común (shù) (xúe)!

La implementación del degradado de color es la misma que antes, excepto que las coordenadas se calculan con funciones trigonométricas, y luego las coordenadas y se cambian con gravedad, parábola, etc. ¡para que parezca más real! (Newton: Considérate sensato)

Luego está la fuerza gravitacional, que probablemente necesite multiplicar la altura por 0,98, lo que equivale a restar 0,1

Dado que y = 0 por encima de las coordenadas easyx, debería ser:

1

y + (HEIGHT - y) * 0.1

Luego, de acuerdo con la parábola: y = x^2, solo modifíquelo

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

dieciséis

17

18

19

else

{

    for (int a = 0; a < 360; a += 30)

    {

        for (int size = 5; size > 0; size--)

        {

            int x = cos(a * PI / 180.0) * (fire[i].explode + size * 10) + fire[i].nowx;

            int y = sin(a * PI / 180.0) * (fire[i].explode + size * 10) + fire[i].nowy + fire[i].radio / 2;

            int temp[] = { fire[i].rgb[0], fire[i].rgb[1], fire[i].rgb[2] };

            for (int k = 0; k < 3; k++)

            {

                temp[k] += 50 * (5 - size);

                if (temp[k] > 255) temp[k] = 255;

            }

            setfillcolor(RGB(temp[0], temp[1], temp[2]));

            solidcircle(x, y + (HEIGHT - y) * 0.1 + size * (size - 2), size);

        }

    }

}

código completo

Finalmente, encapsule la función y escriba la función principal (¡si tiene manos!):

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

dieciséis

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

sesenta y cinco

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

/*******************************

* 项目名称:新年烟花

* 开发环境:vs2022 + Easyx

* 作者:轩

* 代码长度:137 行

* 完成时间:2023.1.20

* 用时:2.2 小时

*******************************/

#include <graphics.h>

#include <math.h>

#include <time.h>

#include <stdio.h>

#define MAXNUM 15

#define WIDTH 640

#define HEIGHT 480

#define PI 3.1415926

struct Fire

{

    int nowx;

    int nowy;

    int endy;

    int radio;

    int explode;

    int rgb[3];

    COLORREF color;

}fire[MAXNUM];

void Init()

{

    for (int i = 0; i < MAXNUM; i++)

    {

        fire[i].nowx = rand() % WIDTH;

        fire[i].nowy = HEIGHT + rand() % 250 + 50;

        fire[i].endy = rand() % 100 + 10;

        fire[i].radio = rand() % 50 + 120;

        fire[i].explode = 0;

        int c[][3] = { {255, 0, 0}, {210, 190, 255}, {255, 120, 0}, {255, 0, 150}, {255, 240, 100}, {10, 255, 255}, {160, 10, 255}, {255, 200, 60} };

        int n = rand() % 8;

        fire[i].color = RGB(c[n][0], c[n][1], c[n][2]);

        fire[i].rgb[0] = c[n][0];

        fire[i].rgb[1] = c[n][1];

        fire[i].rgb[2] = c[n][2];

    }

}

void Draw()

{

    for (int i = 0; i < MAXNUM; i++)

    {

        if (fire[i].nowy > fire[i].endy)

        {

            for (int size = 5; size > 0; size--)

            {

                int temp[] = { fire[i].rgb[0], fire[i].rgb[1], fire[i].rgb[2] };

                for (int k = 0; k < 3; k++)

                {

                    temp[k] += 50 * (5 - size);

                    if (temp[k] > 255) temp[k] = 255;

                }

                setfillcolor(RGB(temp[0], temp[1], temp[2]));

                solidcircle(fire[i].nowx, fire[i].nowy + 15*(10 - size), size);

            }

        }

        else

        {

            for (int a = 0; a < 360; a += 30)

            {

                for (int size = 5; size > 0; size--)

                {

                    int x = cos(a * PI / 180.0) * (fire[i].explode + size * 10) + fire[i].nowx;

                    int y = sin(a * PI / 180.0) * (fire[i].explode + size * 10) + fire[i].nowy + fire[i].radio / 2;

                    int temp[] = { fire[i].rgb[0], fire[i].rgb[1], fire[i].rgb[2] };

                    for (int k = 0; k < 3; k++)

                    {

                        temp[k] += 50 * (5 - size);

                        if (temp[k] > 255) temp[k] = 255;

                    }

                    setfillcolor(RGB(temp[0], temp[1], temp[2]));

                    solidcircle(x, y + (HEIGHT - y) * 0.1 + size * (size - 2), size);

                }

            }

        }

    }

}

void Move()

{

    for (int i = 0; i < MAXNUM; i++)

    {

        if (fire[i].nowy > fire[i].endy)

        {

            fire[i].nowy -= 3;

        }

        else

        {

            if (fire[i].explode >= fire[i].radio)

            {

                fire[i].nowx = rand() % WIDTH;

                fire[i].nowy = HEIGHT + rand() % 250 + 50;

                fire[i].endy = rand() % 100 + 10;

                fire[i].radio = rand() % 50 + 120;

                fire[i].explode = 0;

                int c[][3] = { {255, 0, 0}, {210, 190, 255}, {255, 120, 0}, {255, 0, 150}, {255, 240, 100}, {10, 255, 255}, {160, 10, 255}, {255, 200, 60} };

                int n = rand() % 8;

                fire[i].color = RGB(c[n][0], c[n][1], c[n][2]);

                fire[i].rgb[0] = c[n][0];

                fire[i].rgb[1] = c[n][1];

                fire[i].rgb[2] = c[n][2];

            }

            else fire[i].explode++;

        }

    }

}

int main()

{

    srand(time(NULL));

    initgraph(640, 480);

    Init();

    BeginBatchDraw();

    while (true)

    {

        cleardevice();

        Draw();

        Move();

        FlushBatchDraw();

        Sleep(2);

    }

    EndBatchDraw();

    closegraph();

    return 0;

}

 

Supongo que te gusta

Origin blog.csdn.net/m0_69824302/article/details/131030745
Recomendado
Clasificación