当前位置:   article > 正文

【c++】新年烟花完整代码_跨年的c++代码

跨年的c++代码

分享一下我的烟花代码,图片可以去百度自己搜索,如想要可以私信找我取

  1. #include <iostream>
  2. #include <graphics.h>
  3. #include <easyx.h>
  4. #include "math.h"
  5. #include "time.h"
  6. //烟花弹
  7. struct Jet {
  8. int x = 0, y = 0;
  9. int hx = 0, hy = 0;
  10. unsigned long t1 = 0, t2 = 0, dt = 0;
  11. IMAGE img;
  12. bool isshoot = 1;
  13. }jet;
  14. Jet jet1, jet2, jet3;
  15. //烟花
  16. struct Fire {
  17. int r = 0; //当前半径
  18. int maxr = 0; //最大半径
  19. int x = 0, y = 0; //中心点坐标(窗口)
  20. int cx = 0, cy = 0; //中心点坐标(图画)
  21. int xy[240][240]; //保存像素点
  22. bool isboom = 0; //是否爆炸
  23. bool isdraw = 0; //是否显示
  24. unsigned long t1 = 0, t2 = 0, dt = 0; //爆炸速度
  25. }fire;
  26. Fire fire1, fire2, fire3;
  27. void jet_init(Jet* jetn) //烟花弹的初始化
  28. {
  29. jetn->x = rand() % 1100 - 20;
  30. jetn->y = rand() % 50 + 700;
  31. jetn->hx = jetn->x;
  32. jetn->hy = rand() % 450;
  33. jetn->t1 = GetTickCount(); //获取系统时间
  34. jetn->isshoot = true;
  35. jetn->dt = rand() % 10 + 1;
  36. }
  37. void fire_init(Fire* firen)
  38. {
  39. firen->r = 0;
  40. firen->maxr = 120;
  41. firen->cx = 120; firen->cy = 120; //中心点的坐标(图片)
  42. firen->isboom = true; firen->isdraw = false;
  43. firen->t1 = GetTickCount();
  44. firen->dt = 5; //烟花爆炸速度
  45. IMAGE fimg;
  46. loadimage(&fimg, L"烟花4.jpg", 240, 240);
  47. SetWorkingImage(&fimg);
  48. for (int a = 0; a < 240; a++)
  49. {
  50. for (int b = 0; b < 240; b++)
  51. {
  52. firen->xy[a][b] = getpixel(a, b); //传入像素点
  53. }
  54. }
  55. SetWorkingImage();
  56. }
  57. //函数测试
  58. void fire_text(Jet* jetn, Fire* firen)
  59. {
  60. DWORD* pmem = GetImageBuffer();
  61. jetn->t2 = GetTickCount();
  62. if (jetn->t2 - jetn->t1 > jetn->dt && jetn->isshoot == true)
  63. {
  64. putimage(jetn->x, jetn->y, &jetn->img, SRCINVERT);
  65. if (jetn->y > jetn->hy)
  66. {
  67. jetn->y -= 5;
  68. putimage(jetn->x, jetn->y, &jetn->img, SRCINVERT);
  69. }
  70. if (jetn->y <= jetn->hy) //烟花弹移动出界
  71. {
  72. jetn->isshoot = false;
  73. putimage(jetn->x, jetn->y, &jetn->img, SRCINVERT);
  74. fire_init(firen); //烟花的初始化
  75. firen->x = jetn->hx + 10;
  76. firen->y = jetn->hy - 20;
  77. }
  78. if (jetn->isshoot == false && firen->isboom == true && firen->isdraw == 0)
  79. {
  80. jet_init(jetn);
  81. putimage(jetn->x, jetn->y, &jetn->img, SRCINVERT);
  82. }
  83. jetn->t1 = jetn->t2;
  84. //烟花
  85. int drt[13] = { 5,5,5,10,10,15,15,25,35,40,40,55,55 }; //不一样的速度
  86. firen->t2 = GetTickCount();
  87. if (firen->t2 - firen->t1 > firen->dt && firen->isboom == true)
  88. {
  89. if (firen->r < firen->maxr)
  90. {
  91. firen->r++;
  92. firen->dt = drt[firen->r / 10];
  93. firen->isdraw = true;
  94. }
  95. if (firen->r > firen->maxr - 1)
  96. {
  97. firen->isdraw = false;
  98. firen->isboom = false;
  99. //初始化烟花
  100. firen->r = 0;
  101. firen->maxr = 120;
  102. }
  103. firen->t1 = firen->t2;
  104. if (firen->isdraw = true)
  105. {
  106. for (double a = 0; a < 6.28; a += 0.01)
  107. {
  108. for (int m = 0; m <= firen->r; m++)
  109. //x1 y1 来自像素点的628个像素坐标
  110. {
  111. int x2 = firen->cx + m * cos(a);
  112. int y2 = firen->cy - m * sin(a);
  113. //如果数据正常,获取像素点的三原色系
  114. if (x2 > 0 && x2 < 240 && y2 > 0 && y2 < 240)
  115. {
  116. int b = firen->xy[x2][y2] & 0xff;
  117. int g = (firen->xy[x2][y2] >> 8) & 0xff;
  118. int r = firen->xy[x2][y2] >> 16;
  119. //求圈上的点在窗体上的坐标
  120. int xx2 = firen->x + m * cos(a);
  121. int yy2 = firen->y - m * sin(a);
  122. if (/*b > 0x20 && g > 0x20 && r > 0x20 &&*/ xx2 < 1200 && xx2 > 0 && yy2 > 0 && yy2 <= 600)
  123. {
  124. pmem[yy2 * 1100 + xx2] = BGR(firen->xy[x2][y2]);
  125. }
  126. }
  127. }
  128. }
  129. firen->isdraw == false;
  130. }
  131. }
  132. }
  133. }
  134. int ss = 0;
  135. int Fire_Finally() //烟花主函数
  136. {
  137. initgraph(1100, 800);
  138. srand(time(0));
  139. /*-----------------------------------------图片------------------------------------*/
  140. jet_init(&jet);
  141. loadimage(&jet.img, L"烟花弹2.jpg", 20, 50);
  142. putimage(jet.x, jet.y, &jet.img, SRCINVERT);
  143. DWORD* pmem = GetImageBuffer();
  144. BeginBatchDraw();
  145. while (1)
  146. {
  147. for (int clr = 0; clr < 1000; clr++)
  148. {
  149. for (int j = 0; j < 2; j++)
  150. {
  151. int px1 = rand() % 1200;
  152. int py1 = rand() % 650;
  153. if (py1 < 790)
  154. {
  155. pmem[py1 * 1200 + px1] = pmem[py1 * 1200 + px1 + 1] = BLACK;
  156. }
  157. }
  158. }
  159. fire_text(&jet, &fire);
  160. FlushBatchDraw();
  161. }
  162. EndBatchDraw();
  163. closegraph();
  164. return 0;
  165. }
  166. int main()
  167. {
  168. Fire_Finally();
  169. system("pause");
  170. return 0;
  171. }

这是一个烟花连续放的程序,如果要更多的烟花道理相同,如果不想自己写可以私信我哦。

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