当前位置:   article > 正文

C语言烟花代码—兔年顶呱呱

C语言烟花代码—兔年顶呱呱

        废话不多说,直接上码

代码,歌曲,烟花图片都在压缩包里
链接: https://pan.baidu.com/s/1_459s0fFCAX1DcQa_BnHMQ?pwd=qw12 
提取码: qw12

要看效果的也可以看我抖音:

1210246294

  1. #include<stdio.h>
  2. #include<easyx.h>
  3. #include<time.h>
  4. #include<stdlib.h>
  5. #include <Windows.h>
  6. #include<math.h>
  7. #include <conio.h>
  8. #pragma comment(lib,"winmm.lib")
  9. #include<mmsystem.h>
  10. IMAGE img;
  11. IMAGE img1;
  12. IMAGE img2;
  13. IMAGE img3;
  14. #define num 10
  15. #define PI 3.14
  16. struct Fire //创建烟花弹结构体类型
  17. {
  18. int x, y;//初始坐标
  19. int max_x, max_y;//烟花最大高度
  20. IMAGE Img[2];//保存图片
  21. bool boom;//是否要爆开
  22. };
  23. struct Fire fire[num];
  24. struct Show
  25. {
  26. int x, y;//绽放的位置
  27. int cx, cy;//烟花中心点坐标
  28. int r;//当前的半径
  29. int max_r;//最大半径
  30. int pixel[200][200];//图片像素数组
  31. bool isshow; // 是否绽放
  32. bool isdraw; // 开始输出像素点
  33. DWORD t1, t2, dt;
  34. int width, height;
  35. };
  36. struct Show show[num];
  37. void initshow(int i)//初始化烟花
  38. {
  39. show[i].cx = 100;
  40. show[i].cy = 100;
  41. show[i].r = 0;
  42. show[i].max_r = 100;
  43. show[i].width = 200;
  44. show[i].height = 200;
  45. show[i].isshow = false;
  46. show[i].dt = 5;
  47. show[i].t1 = timeGetTime();//时间获取速度
  48. }
  49. void initfire(int i)//初始化烟花弹
  50. {
  51. fire[i].x = rand() % 700 + 100;//烟花弹坐标初始化x=100-800;y=100-400;
  52. fire[i].y = 600;
  53. fire[i].max_x = fire[i].x;
  54. fire[i].max_y = rand() % 300 + 100;
  55. fire[i].boom = false;
  56. loadimage(&fire[i].Img[0], "烟花弹.png", 20, 60);
  57. }
  58. void load()//加载烟花图片
  59. {
  60. for (int k = 0; k < num; k++)
  61. { int a = rand() % 3 + 1;
  62. loadimage(&img1, "烟花1.png", 200, 200);//三种不同的烟花
  63. loadimage(&img2, "烟花2.png", 200, 200);
  64. loadimage(&img3, "烟花3.png", 200, 200);
  65. if(a==1)
  66. SetWorkingImage(&img1);
  67. else
  68. if(a==2)
  69. SetWorkingImage(&img2);
  70. else
  71. SetWorkingImage(&img3);
  72. for (int i = 0; i < 200; i++)
  73. {
  74. for (int j = 0; j < 200; j++)
  75. {
  76. show[k].pixel[i][j] = getpixel(i, j);//把图片像素点放到数组中
  77. }
  78. }
  79. }
  80. SetWorkingImage(NULL);
  81. }
  82. void Draw(int i, DWORD* pMem)//绘制一圈像素点
  83. {
  84. if (show[i].isdraw)
  85. {
  86. for (double a = 0; a <= 2*PI; a += 0.01) //一圈628个像素点
  87. {
  88. //(x1,y1)是相对于烟花小图片的 像素 坐标点
  89. int x1 = (int)(show[i].cx + show[i].r * cos(a));
  90. int y1 = (int)(show[i].cy + show[i].r * sin(a));
  91. if (x1 > 0 && x1 < show[i].width && y1>0 && y1 < show[i].height)
  92. {
  93. int b = show[i].pixel[x1][y1] & 0xff; //blue
  94. int g = (show[i].pixel[x1][y1] >> 8) & 0xff; //green
  95. int r = show[i].pixel[x1][y1] >> 16; //red
  96. //(xx,yy)是相对于窗口的 像素 坐标点
  97. int xx = (int)(show[i].x + show[i].r * cos(a));
  98. int yy = (int)(show[i].y + show[i].r * sin(a));
  99. if (r > 0x20 && g > 0x20 && b > 0x20 && xx < 1000 && xx>0 && yy > 0 && yy < 600)
  100. {
  101. //把(x1,y1)坐标上的像素点 赋值 给(xx,yy)坐标点
  102. pMem[yy * 1000 + xx] = BGR(show[i].pixel[x1][y1]);
  103. }
  104. }
  105. }
  106. show[i].isdraw = false;
  107. }
  108. }
  109. void fire_boom(DWORD* pMem)//烟花绽放
  110. {
  111. int drt[16] = { 5, 5, 5, 5, 15, 15, 25, 25, 35, 35, 55, 55, 65, 65, 75, 75 };
  112. for (int i = 0; i < num; i++)
  113. {
  114. show[i].t2 = timeGetTime();
  115. if (show[i].t2 - show[i].t1 > show[i].dt && show[i].isshow == true)
  116. {
  117. if (show[i].r < show[i].max_r)
  118. {
  119. show[i].r++;
  120. show[i].dt = drt[show[i].r / 10]; //每十个烟花像素点改变一下烟花绽放的速度
  121. show[i].isdraw = true;
  122. }
  123. if (show[i].r >= show[i].max_r - 1)
  124. {
  125. show[i].isdraw = false;
  126. initshow(i);
  127. initfire(i);
  128. }
  129. //更新时间
  130. show[i].t1 = show[i].t2;
  131. //可以绽放的状态
  132. Draw(i,pMem);
  133. }
  134. }
  135. }
  136. void fire_up()//烟花弹上升
  137. {
  138. for (int i = 0; i < num; i++)
  139. {
  140. putimage(fire[i].x, fire[i].y, &fire[i].Img[0], SRCINVERT);//消除残影
  141. if (fire[i].y > fire[i].max_y)
  142. {
  143. fire[i].y -= 10;//向上移动
  144. }
  145. else
  146. { //已到达最高点,准备绽放
  147. show[i].x = fire[i].x+10;
  148. show[i].y = fire[i].y;
  149. fire[i].boom = true;
  150. show[i].isshow = true;
  151. }
  152. putimage(fire[i].x, fire[i].y, &fire[i].Img[0], SRCINVERT);
  153. }
  154. }
  155. void word()//文字提醒-“请按任意键...”
  156. {
  157. settextcolor(RED);
  158. settextstyle(25, 0, "宋体");
  159. outtextxy(400, 550, "请");
  160. Sleep(500);
  161. outtextxy(425, 550, "按");
  162. Sleep(500);
  163. outtextxy(450, 550, "任");
  164. Sleep(500);
  165. outtextxy(475, 550, "意");
  166. Sleep(500);
  167. outtextxy(500, 550, "键");
  168. Sleep(500);
  169. outtextxy(525, 550, ".");
  170. Sleep(500);
  171. outtextxy(550, 550, ".");
  172. Sleep(500);
  173. outtextxy(575, 550, ".");
  174. }
  175. int main()
  176. {
  177. int i = 0;
  178. srand((unsigned int)time(NULL));
  179. initgraph(1000, 600);//初始化图形界面
  180. loadimage(&img, "2023.png", 1000, 600);
  181. putimage(0,0,&img,SRCINVERT);//背景图
  182. mciSendString("open 打上花火.mp3",0,0,0);//播放—“打上花火”
  183. mciSendString("play 打上花火.mp3", 0, 0, 0);
  184. Sleep(1000);
  185. word();//文字提醒-“请按任意键...”
  186. _getch();//输入任意键继续
  187. DWORD* pMem = GetImageBuffer();
  188. for (int i = 0; i < num; i++)//初始化烟花弹和烟花
  189. {
  190. initfire(i);
  191. initshow(i);
  192. }
  193. load();//加载烟花图片
  194. while (1)
  195. {
  196. for (int i = 0; i < 3000; i++)
  197. {
  198. int px1 = rand() % 1000; // 0..1199
  199. int py1 = rand() % 600; // 0.799
  200. pMem[py1 * 1000 + px1] = BLACK;
  201. pMem[py1 * 1000 + px1 + 1] = BLACK; // 对显存赋值擦出像素点
  202. }
  203. fire_up();//发射烟花弹
  204. fire_boom(pMem);//烟花绽放
  205. Sleep(50);
  206. }
  207. return 0;
  208. }

   运行效果如下:

5a662066fbf2479a8abf2adc16b4f20b.png

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/115896?site
推荐阅读
相关标签
  

闽ICP备14008679号