六一专辑||C++实现动态烟花代码

首先,祝大家儿童节快乐!

在这篇文章中,将用烟花致以大家最好的祝福!

烟花代码将会用到 Easyx 图形库,可以去官网下载:easyx.cnicon-default.png?t=N4P3http://easyx.cn/

代码思路

1 烟花结构体

2 初始化烟花

3 烟花上升

4 烟花爆炸

5 绘制烟花

不需要任何图片、音效(本来想加的,你要加自己加吧)

开始编写

提前声明:代码纯原创!

需要用到的头文件以及宏:

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 Easyx图形库

math.h 计算烟花位置

time.h 选取随机种子

stdio.h 标准输入输出头文件

MAXNUM 烟花数量

WIDTH , HEIGHT 窗口的宽、高

PI 圆周率常量,结合数学库计算烟花位置

1 烟花结构体

定义的参数如下:

nowx , nowy 现在坐标

endy 爆炸高度

radio 爆炸半径

explode 爆炸进度

rgb[3] 烟花颜色rgb值

color 烟花颜色

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 初始化烟花

nowx , nowy 窗口外的随机位置

endy 随即高度

radio 随即半径

explode 一开始设为0

rgb[3] 一些特定的颜色随机取

color 按照rgb值

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

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 烟花上升

y坐标 还没到达爆炸高度时一直上升

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 烟花爆炸

爆炸进度 explode 不断增加,如果到达爆炸半径,就重新初始化。(接上面的if语句)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

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 绘制烟花

上升

绘制5个圆,大小从大到小,颜色从深到浅(增加rgb值)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

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);

        }

    }

}

爆炸

重头戏!重头戏!重头戏!需要一些简(fù)单(zá)的数(gāo)学(děng)常(shù)识(xúe)!

颜色渐变的实现方式跟前面一样,只不过这里要用三角函数计算坐标,再用万有引力、抛物线等改变 y坐标,让它看得更真实!(牛顿:算你小子识相)

然后是万有引力,大概需要高度乘以 0.98 ,这里就相当于减去 0.1

由于 easyx 坐标上方为 y = 0,所以,应该是:

1

y + (HEIGHT - y) * 0.1

再根据抛物线:y = x^2 ,修改一下就行了

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

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);

        }

    }

}

完整代码

最后,把函数封装一下,main 函数写出来(有手就行!):

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

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

65

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;

}

猜你喜欢

转载自blog.csdn.net/m0_69824302/article/details/131030745