Very beautiful New Year wishes! C language achieves beautiful fireworks effect
- 2020-06-03 07:30:46
- OfStack
This article examples for you to share C language to achieve beautiful fireworks display specific code, for your reference, the specific content is as follows
Program name: bless fireworks, bless friends
Compile environment: VC++6.0
&
&
easyx(Start of Winter)
#include <graphics.h>
#include <conio.h>
#include <iostream.h>
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <Mmsystem.h> // Files that must be included to use the timer
#pragma comment ( lib, "Winmm.lib" )
/***** Macro definition area ******/
#define NUM 13 // Firework type quantity macro definition
/***** Structure-defined region **********/
// The fireworks structure
struct FIRE
{
int r; // Current explosion radius
int max_r; // Maximum radius from the explosion center to the edge
int x, y; // The coordinates of the explosion center in the window
int cen_x, cen_y; // The coordinates of the center of the explosion relative to the upper left corner of the image
int width, height; // The width and height of the picture
int xy[240][240]; // Save image pixels
bool show; // Whether the blossom
bool draw; // Start outputting pixels
DWORD t1, t2, dt; // Bloom speed
}Fire[NUM];
// Smoke bomb structure
struct JET
{
int x, y; // Ejector coordinates
int hx, hy; // Highest point coordinates ------ Will be assigned to FIRE The inside of the x, y
int height; // Fireworks height
bool shoot; // Is it possible to launch
DWORD t1, t2, dt; // Rate of fire
IMAGE img[2]; // Store spend playing 1 bright 1 Dark picture
byte n : 1; // Picture the subscript
}Jet[NUM];
// happy birthday Artistic word structure
struct HAPPY // storage Happy Birthday Art word picture
{
int x, y; // The upper-left coordinates of the picture of each word
IMAGE img; // Save a single word picture
static int num; // The letter number
}Happy[NUM];
int HAPPY::num = 0;
// For.2012 の die young woman structure
struct WISH // Scroll blessing image structure
{
int x, y; // Picture top left coordinates
DWORD t1, t2, dt; // Picture movement time interval
IMAGE img; // Store image
int dxy; // The amount of movement per time
byte dir : 1; // Limit two scrolling directions
}Wish;
/**** Function declaration area ****/
void Init ( int ); // Initializing fireworks
void Load ( ); // Pictures of loading fireworks
void Shoot ( ); // Launch the fireworks
void Chose ( DWORD& ); // Screening of fireworks
void Wishing( ); // Scroll to bless
void Style ( DWORD& ); // Launch the style
void Show ( DWORD* ); // Bloom of fireworks
// The main function
void main()
{
initgraph( 1200, 800 );
srand( time(0) );
// Playing background music
mciSendString( "open ./fire/bk.mp3 alias bk", 0, 0, 0 );
mciSendString( "play bk repeat", 0, 0, 0 );
setfillstyle( 0 );
setfont ( 36, 0, " Regular script " );
setcolor ( YELLOW );
outtextxy ( 370, 100, "yy � � __ The factory .... ^_^" );
DWORD t1 = timeGetTime(); // Screen firework timing
DWORD st1 = timeGetTime(); // Play pattern timing
DWORD* pMem = GetImageBuffer(); // Gets the window memory pointer
for ( int i = 0; i < NUM; i++ ) // Initializing fireworks
{
Init( i );
}
Load(); // The picture information of fireworks is loaded into the corresponding structure
BeginBatchDraw(); // Start batch drawing
while ( !kbhit() )
{
Sleep( 10 );
// Random selection 4000 Erase pixels
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 ) // To prevent cross-border
pMem[py1 * 1200 + px1] = pMem[py1 * 1200 + px1 + 1] = BLACK; // Erase pixels from the video memory assignment
}
}
Chose ( t1 ); // Screening of fireworks
Shoot ( ); // Launch the fireworks
Show ( pMem ); // Bloom of fireworks
Wishing ( ); // Rolling characters
Style ( st1 ); // Figure launch
FlushBatchDraw( ); // Displays all previous drawing actions
}
}
// Initialize fireworks parameters
void Init( int i )
{
// The distance from the fireworks center to the edge of the picture and the distance from the fireworks center to the upper left corner of the picture are respectively (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 };
/**** Initializing fireworks *****/
Fire[i].x = 0; // Fireworks center coordinates
Fire[i].y = 0;
Fire[i].width = 240; // Image width
Fire[i].height = 240; // Pictures of high
Fire[i].max_r = r[i]; // The largest radius
Fire[i].cen_x = x[i]; // Center distance from upper left corner
Fire[i].cen_y = y[i];
Fire[i].show = false; // Whether the blossom
Fire[i].dt = 5; // Bloom time interval
Fire[i].t1 = timeGetTime();
Fire[i].r = 0; // from 0 Begin to bloom
/**** Initializes the smoke bomb *****/
Jet[i].x = -240; // The upper left corner coordinates of a smoke bomb
Jet[i].y = -240;
Jet[i].hx = -240; // Coordinates of highest point of smoke bomb launch
Jet[i].hy = -240;
Jet[i].height = 0; // Launch height
Jet[i].t1 = timeGetTime();
Jet[i].dt = rand() % 10; // Time interval of transmission velocity
Jet[i].n = 0; // Bullet flash image subscript
Jet[i].shoot = false; // Whether to launch
}
// Loading pictures
void Load()
{
/**** Store the pixel color of the 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 );
}
/**** Loaded with smoke bombs ************/
IMAGE sm;
loadimage( &sm, "./fire/shoot.jpg", 200, 50 );
for ( 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
}
/***** loading Happy Birthday The picture ********/
IMAGE hm;
loadimage( &hm, "./fire/happy.jpg", 689, 115 );
SetWorkingImage( &hm );
for ( i = 0; i < 13; i++ )
{
Happy[i].x = i * 90;
Happy[i].y = rand() % 100 + 500;
getimage( &Happy[i].img, i * 53, 0, 53, 115 );
}
/***** loading For.2012 の die young woman The picture *********/
Wish.x = 0;
Wish.y = 100;
Wish.t1 = timeGetTime();
Wish.dt = 46;
Wish.dir = 0;
Wish.dxy = rand() % 8 + 1;
loadimage( &Wish.img, "./fire/yaojing.jpg", 490, 100 );
putimage( Wish.x, Wish.y, &Wish.img, SRCINVERT );
SetWorkingImage(); // Sets the callback window
}
// in 1 Select the fireworks that can be launched within a certain range, and initialize the launch parameters, output the smoke shells to the screen, play the 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 the smoke bomb and prepare to fire *****/
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 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 for smoke bombs 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 the smoke bomb *****/
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 , slow down *****/
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 )
{
// Sound of explosions
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 smoke bombs
Fire[i].x = Jet[i].hx + 10; // It exploded in the middle of a cloud of smoke
Fire[i].y = Jet[i].hy; // Bloom at the highest point
Fire[i].show = true; // Begin to bloom
Jet[i].shoot = false; // Stop firing
// Display corresponding letters
putimage( Happy[HAPPY::num].x, Happy[HAPPY::num].y, &Happy[HAPPY::num].img, SRCINVERT );
HAPPY::num++;
if ( HAPPY::num > 12 )
HAPPY::num = 0;
}
Jet[i].t1 = Jet[i].t2;
}
}
}
// Make pictures that move horizontally and vertically
void Wishing()
{
Wish.t2 = timeGetTime();
if ( Wish.t2 - Wish.t1 > Wish.dt )
{
/**** Image movement *****/
putimage( Wish.x, Wish.y, &Wish.img, SRCINVERT );
if ( Wish.dir == 0 )
Wish.x += Wish.dxy;
else
Wish.y -= Wish.dxy;
putimage( Wish.x, Wish.y, &Wish.img, SRCINVERT );
/**** Picture out of bounds *****/
if ( Wish.x > 1200 || Wish.y < 0 )
{
Wish.dir = rand() % 2; // Get random direction
if ( Wish.dir == 0 ) // If you move left and right
{
Wish.y = rand() % 700; // The initial position
Wish.x = -490;
Wish.dxy = rand() % 7 + 1; // Random motion component
}
else // Up and down
{
Wish.dxy = rand() % 7 + 1;
Wish.x = rand() % 700;
Wish.y = 800;
}
}
Wish.t1 = Wish.t2;
}
}
// According to figure
void Style( DWORD& st1 )
{
DWORD st2 = timeGetTime();
if ( st2 - st1 > 266000 ) // 1 The time of the song
{
// Heart-shaped 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();
/**** Regular distribution of smoke 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 the smoke bomb
/**** 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 the transmitting 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;
}
}
// Bloom of fireworks
void Show( DWORD* pMem )
{
// Fireworks stage blooming time interval, production of 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 blast radius, explode fireworks, increase time interval for variable speed 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 cracker is explodable, draw the firework according to the current explosion radius, and the color value is close to black.
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 image
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 within the image
{
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 );
// The coordinates of fireworks pixels on a 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 outputted to prevent crossing boundaries
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 operation to draw fireworks
}
}
Fire[i].draw = false;
}
}
}