As long as the code is well written , Every day is Valentine's day for programmers , Put a romantic and cool fireworks confession code here , Take a look at the final rendering first :
Press enter, and a paragraph will appear to express your feelings , It can be modified according to your own needs
Fireworks will bloom when waiting for the music to be placed in a specific place , Love shapes appear from time to time
Next, put the source code directly
/******************************************************** * * * Program name : Confession fireworks * *
Compilation environment : VS2019 && easyx( Romantic Limited Edition ) * * *
*********************************************************/ #include
<graphics.h> #include <conio.h> #include <math.h> #include <time.h> #include
<stdio.h> #include <Mmsystem.h> #pragma comment ( lib, "Winmm.lib" ) /*****
Macro definition area ******/ #define NUM 13 // Macro definition of fireworks type and quantity #define PI 3.1415926548 /***** Structure definition area
**********/ // Fireworks structure struct FIRE { int r; // Current explosion radius int max_r; // Maximum radius from explosion center to edge
int x, y; // Coordinates of the explosion center in the window int cen_x, cen_y; // Coordinates of the explosion center relative to the upper left corner of the picture int width, height;
// Width and height of picture int xy[240][240]; // Save picture pixels bool show; // Whether it blooms bool draw; // Start outputting pixels
DWORD t1, t2, dt; // Blooming speed }Fire[NUM]; // Fireworks bomb structure struct JET { int x, y; // Spray point coordinates
int hx, hy; // Coordinates of the highest point ------ Assign a value to FIRE Inside x, y int height; // Fireworks height bool shoot; //
Whether it can be launched DWORD t1, t2, dt; // Launch speed IMAGE img[2]; // Save the picture of one light and one dark of the flower bomb byte n : 1; //
Picture subscript }Jet[NUM]; /**** Function declaration area ****/ void welcome(); void Init(int); // Initialize fireworks void
Load(); // Load fireworks pictures void Shoot(); // Fire fireworks void Chose(DWORD&); // Screening fireworks void
Style(DWORD&); // Emission style void Show(DWORD*); // Fireworks bloom // Main function void main() {
initgraph(1200, 800); srand(time(0)); // Play background music mciSendString("open
./fire/ Little lucky .mp3 alias bk", 0, 0, 0); mciSendString("play bk repeat", 0, 0, 0);
welcome(); DWORD t1 = timeGetTime(); // Screening fireworks timing DWORD st1 = timeGetTime(); //
Play pattern timing DWORD* pMem = GetImageBuffer(); // Get window display memory pointer for (int i = 0; i < NUM;
i++) // Initialize fireworks { Init(i); } Load(); // Load the fireworks picture information into the corresponding structure BeginBatchDraw(); //
Start batch drawing while (!kbhit()) { Sleep(10); // Random selection 4000 Pixel erase for (int clr = 0; clr
< 1000; clr++) { for (int j = 0; j < 2; j++) { int px1 = rand() % 1200; int py1
= rand() % 800; if (py1 < 799) // Prevent cross-border pMem[py1 * 1200 + px1] = pMem[py1 * 1200
+ px1 + 1] = BLACK; // Assign a value to the video memory and erase the pixels } } Chose(t1); // Screening fireworks Shoot(); // Fire fireworks
Show(pMem); // Fireworks bloom Style(st1); // Pattern launch FlushBatchDraw(); // Show all previous drawing operations } }
void welcome() { //setfillstyle(0); setcolor(YELLOW); for (int i = 0; i < 50;
i++) { int x = 600 + int(180 * sin(PI * 2 * i / 60)); int y = 200 + int(180 *
cos(PI * 2 * i / 60)); cleardevice(); settextstyle(i, 0, " Italics "); outtextxy(x-80,
y, " Romantic watch day "); outtextxy(x-10, y+100, " Dedicated to my beloved so and so "); Sleep(25); } getchar();
cleardevice(); settextstyle(25, 0, " Italics "); outtextxy(400, 200, " It turns out that you are the lucky one I want to keep most ");
outtextxy(400, 250, " It turns out that we were so close to love "); outtextxy(400, 300, " That's my decision to fight the world ");
outtextxy(400, 350, " The rain that accompanied me "); outtextxy(400, 400, " It's you every time "); outtextxy(400,
450, " Spotless sincerity ."); outtextxy(600, 500, "----《 Little lucky 》"); getchar(); } // Initialize fireworks parameters
void Init(int i) { // Respectively : The longest distance from the center of the fireworks to the edge of the picture , Distance from the center of fireworks to the upper left corner of the picture (x,y) Two components int r[13] = {
120, 120, 155, 123, 130, 147, 138, 138, 130, 135, 140, 132, 155 }; int x[13] =
{ 120, 120, 110, 117, 110, 93, 102, 102, 110, 105, 100, 108, 110 }; int y[13] =
{ 120, 120, 85, 118, 120, 103, 105, 110, 110, 120, 120, 104, 85 }; /**** Initialize fireworks
*****/ Fire[i].x = 0; // Fireworks center coordinates Fire[i].y = 0; Fire[i].width = 240; // Picture width
Fire[i].height = 240; // Picture high Fire[i].max_r = r[i]; // Maximum radius Fire[i].cen_x =
x[i]; // Distance from center to upper left corner Fire[i].cen_y = y[i]; Fire[i].show = false; // Whether it blooms
Fire[i].dt = 5; // Blooming interval Fire[i].t1 = timeGetTime(); Fire[i].r = 0; // from 0
Begin to bloom /**** Initialize fireworks *****/ Jet[i].x = -240; // Coordinates of the upper left corner of the fireworks bomb Jet[i].y = -240;
Jet[i].hx = -240; // Coordinates of the highest point of fireworks projectile launch Jet[i].hy = -240; Jet[i].height = 0; // launch altitude
Jet[i].t1 = timeGetTime(); Jet[i].dt = rand() % 10; // Launch speed interval Jet[i].n = 0;
// Fireworks flare picture subscript Jet[i].shoot = false; // Launch or not } // Load picture void Load() { /****
Store the pixel color of fireworks ****/ IMAGE fm, gm; loadimage(&fm, "./fire/flower.jpg", 3120, 240);
for (int i = 0; i < 13; i++) { SetWorkingImage(&fm); getimage(&gm, i * 240, 0,
240, 240); SetWorkingImage(&gm); for (int a = 0; a < 240; a++) for (int b = 0;
b < 240; b++) Fire[i].xy[a][b] = getpixel(a, b); } /**** Load fireworks ************/
IMAGE sm; loadimage(&sm, "./fire/shoot.jpg", 200, 50); for (int i = 0; i < 13;
i++) { SetWorkingImage(&sm); int n = rand() % 5; getimage(&Jet[i].img[0], n *
20, 0, 20, 50); // dark getimage(&Jet[i].img[1], (n + 5) * 20, 0, 20, 50); // bright }
SetWorkingImage(); // Set back to the drawing window } // Screen fireworks that can be emitted within a certain range , And initialize the launch parameters , Output fireworks to the screen , Play sound void
Chose(DWORD& t1) { DWORD t2 = timeGetTime(); if (t2 - t1 > 100) { int n =
rand() % 20; if (n < 13 && Jet[n].shoot == false && Fire[n].show == false) {
/**** Reset fireworks , Ready for launch *****/ Jet[n].x = rand() % 1200; Jet[n].y = rand() % 100 +
600; Jet[n].hx = Jet[n].x; Jet[n].hy = rand() % 400; Jet[n].height = Jet[n].y -
Jet[n].hy; Jet[n].shoot = true; putimage(Jet[n].x, Jet[n].y,
&Jet[n].img[Jet[n].n], SRCINVERT); /**** Play the sound of each fireworks bomb *****/ /*char c1[50],
c2[30], c3[30]; sprintf(c1, "open ./fire/shoot.mp3 alias s%d", n); sprintf(c2,
"play s%d", n); sprintf(c3, "close n%d", n); mciSendString(c3, 0, 0, 0);
mciSendString(c1, 0, 0, 0); mciSendString(c2, 0, 0, 0);*/ } t1 = t2; } } //
Scan fireworks and fire void Shoot() { for (int i = 0; i < 13; i++) { Jet[i].t2 =
timeGetTime(); if (Jet[i].t2 - Jet[i].t1 > Jet[i].dt && Jet[i].shoot == true) {
/**** The rise of fireworks *****/ putimage(Jet[i].x, Jet[i].y, &Jet[i].img[Jet[i].n],
SRCINVERT); if (Jet[i].y > Jet[i].hy) { Jet[i].n++; Jet[i].y -= 5; }
putimage(Jet[i].x, Jet[i].y, &Jet[i].img[Jet[i].n], SRCINVERT); /**** Rising to height 3
/ 4, Decelerate *****/ if ((Jet[i].y - Jet[i].hy) * 4 < Jet[i].height) Jet[i].dt =
rand() % 4 + 10; /**** Rise to maximum height *****/ if (Jet[i].y <= Jet[i].hy) { // Play explosion
/*char c1[50], c2[30], c3[30]; sprintf(c1, "open ./fire/bomb.wav alias n%d",
i); sprintf(c2, "play n%d", i); sprintf(c3, "close s%d", i); mciSendString(c3,
0, 0, 0); mciSendString(c1, 0, 0, 0); mciSendString(c2, 0, 0, 0);*/
putimage(Jet[i].x, Jet[i].y, &Jet[i].img[Jet[i].n], SRCINVERT); // Wipe off the fireworks
Fire[i].x = Jet[i].hx + 10; // Explode in the middle of fireworks Fire[i].y = Jet[i].hy; // Bloom at the highest point
Fire[i].show = true; // Begin to bloom Jet[i].shoot = false; // hold fire } Jet[i].t1 =
Jet[i].t2; } } } // Display pattern void Style(DWORD& st1) { DWORD st2 = timeGetTime(); if
(st2 - st1 >20000) // Time for a song { // Cardioid coordinates int x[13] = { 60, 75, 91, 100, 95, 75,
60, 45, 25, 15, 25, 41, 60 }; int y[13] = { 65, 53, 40, 22, 5, 4, 20, 4, 5, 22,
40, 53, 65 }; for (int i = 0; i < NUM; i++) { //cleardevice(); /**** Regularly distributed fireworks bombs
***/ Jet[i].x = x[i] * 10; Jet[i].y = (y[i] + 75) * 10; Jet[i].hx = Jet[i].x;
Jet[i].hy = y[i] * 10; Jet[i].height = Jet[i].y - Jet[i].hy; Jet[i].shoot =
true; Jet[i].dt = 7; putimage(Jet[i].x, Jet[i].y, &Jet[i].img[Jet[i].n],
SRCINVERT); // Display fireworks /**** Set fireworks parameters ***/ Fire[i].x = Jet[i].x + 10; Fire[i].y =
Jet[i].hy; Fire[i].show = false; Fire[i].r = 0; /**** Play emitted sound ***/ /*char
c1[50], c2[30], c3[30]; sprintf(c1, "open ./fire/shoot.mp3 alias s%d", i);
sprintf(c2, "play s%d", i); sprintf(c3, "close n%d", i); mciSendString(c3, 0,
0, 0); mciSendString(c1, 0, 0, 0); mciSendString(c2, 0, 0, 0);*/ } st1 = st2; }
} // Fireworks bloom void Show(DWORD* pMem) { // Time interval of fireworks blooming in three stages , Create variable speed blooming effect int drt[16] = { 5,
5, 5, 5, 5, 6, 25, 25, 25, 25, 55, 55, 55, 55, 55 }; for (int i = 0; i < NUM;
i++) { Fire[i].t2 = timeGetTime(); // Increase explosion radius , Fireworks bloom , Increase the time interval for speed change effect if (Fire[i].t2 -
Fire[i].t1 > Fire[i].dt && Fire[i].show == true) { if (Fire[i].r <
Fire[i].max_r) { Fire[i].r++; Fire[i].dt = drt[Fire[i].r / 10]; Fire[i].draw =
true; } if (Fire[i].r >= Fire[i].max_r - 1) { Fire[i].draw = false; Init(i); }
Fire[i].t1 = Fire[i].t2; } // If the fireworks can explode , Draw fireworks according to the current explosion radius , If the color value is close to black, it will not be output . if
(Fire[i].draw) { for (double a = 0; a <= 6.28; a += 0.01) { int x1 =
(int)(Fire[i].cen_x + Fire[i].r * cos(a)); // Coordinates relative to the upper left corner of the picture int y1 =
(int)(Fire[i].cen_y - Fire[i].r * sin(a)); if (x1 > 0 && x1 < Fire[i].width &&
y1 > 0 && y1 < Fire[i].height) // Output only pixels in the picture { int b = Fire[i].xy[x1][y1] &
0xff; int g = (Fire[i].xy[x1][y1] >> 8) & 0xff; int r = (Fire[i].xy[x1][y1] >>
16); // Coordinates of fireworks pixels on the window int xx = (int)(Fire[i].x + Fire[i].r * cos(a)); int yy =
(int)(Fire[i].y - Fire[i].r * sin(a)); // Darker pixels are not output , Prevent cross-border if (r > 0x20 && g >
0x20 && b > 0x20 && xx > 0 && xx < 1200 && yy > 0 && yy < 800) pMem[yy * 1200 +
xx] = BGR(Fire[i].xy[x1][y1]); // Display memory operation drawing fireworks } } Fire[i].draw = false; } } }
Welcome to join the group if you need the following resource package and complete source code 【806041599】 Get it from the administrator ! There are some materials in the group that can help you learn better , I'm learning C Any problems encountered in the process of language , Can be sent out for discussion , Everyone is learning C/C++ of , Or change careers , Or college students , There are also front-end parties who want to improve their abilities at work , If you are studying C/C++ You can join the study .
Technology