Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write fireworks to the person you like in C language

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to write fireworks to the person you like in C language". Friends who are interested might as well take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to write fireworks to the person you like in C language".

I. structure

First of all, of course, it is our old friend structure, in which there is no need for me to say more about the art character structure. I should know what to write, ha.

/ / fireworks structure struct FIRE {int r; / / current explosion radius int max_r; / / the 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 int width of the explosion center relative to the coordinates of the upper left corner of the image, height; / / the width and height of the image int xy [240] [240]; / / storing the image pixel bool show; / / whether to bloom bool draw / / start outputting pixel DWORD T1, T2, dt; / / blooming speed} Fire [NUM]; / / fireworks projectile structure struct JET {int x, y; / / injection point coordinates int hx, hy / / highest point coordinates-will be assigned to x, y int height; / / fireworks height bool shoot; / / whether DWORD T1, T2, dt can be emitted in FIRE / / launch speed IMAGE img [2]; / / store a bright and dark picture byte n: 1; / / picture subscript} Jet [NUM] / / happy birthday WordArt structure struct HAPPY / / Storage Happy Birthday WordArt Picture {int x, y; / / Upper left coordinates of each word Picture IMAGE img; / / Save single word Picture static int num / / letter number} Happy [NUM]; 2. Initialization

Initialize the fireworks parameters and complete all the preparations

/ / initialize the fireworks parameter void Init (int I) {/ / are: the furthest distance from the center of the fireworks to the edge of the picture, and the distance from the center of the 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,117,110,93,102,102,110,105,100,108,110}; int y [13] = {120,120,85,118,120,103,105,110,110,120,104,85}; / * initialize fireworks * / fireworks [I] .x = 0 / / fireworks center coordinates Fire[ I] .y = 0; fireworks [I] .width = 240; / / Picture width Fire[ I] .height = 240 / / the picture is high [I] .max _ r = r [I]; / / maximum radius Fire[ I] .cen _ x = x [I]; / / distance from the center to the upper left corner Fire[ I] .cen _ y = y [I] Fire[ I] .show = false; / / whether to bloom Fire[ I] .dt = 5; / / blooming interval Fire[ I] .t1 = timeGetTime (); Fire[ I] .r = 0 / / start blooming from 0 / initialize fireworks bomb * / Jet [I] .x =-240; / / coordinates of upper left corner of fireworks bomb Jet [I] .y =-240 Jet [I] .hx =-240; / fireworks projectile launch highest point coordinates Jet [I] .hy =-240; Jet [I] .height = 0; / / launch height Jet [I] .t1 = timeGetTime () Rand [I] .dt = Jet ()% 10; / / launch speed interval Jet [I] .n = 0; / / fireworks flicker picture subscript Jet [I]. Fireworks = false; / / whether to launch} 3, fireworks loading function

Load pictures and store pixel colors of fireworks

/ / load picture void Load () {/ * store pixel colors 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 ); } 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 ); getimage( &Jet[i].img[1], (n + 5) * 20, 0, 20, 50 ); } 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 ); } 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();}四、烟花发射函数 扫描烟花弹并发射,控制开始与停止发射 // 扫描烟花弹并发射void Shoot(){ for ( int i = 0; i < 13; i++ ) { Jet[i].t2 = timeGetTime(); if ( Jet[i].t2 - Jet[i].t1 >

True [I] .dt & & Jet [I] .fireworks bomb rise * / 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); / * rise to 3 / 4 of height, decelerate * / if ((Jet [I]. Y-Jet [I] .hy) * 4

< Jet[i].height ) Jet[i].dt = rand() % 4 + 10 ; /**** 上升到最大高度 *****/ if ( Jet[i].y 12 ) HAPPY::num = 0; } Jet[i].t1 = Jet[i].t2; } }}五、烟花显示函数 显示花样,如何让烟花变得更好看 // 显示花样void Style( DWORD& st1 ){ DWORD st2 = timeGetTime(); if ( st2 - st1 >

266000) / / time of a 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(); /**** 规律分布烟花弹 ***/ 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 ); // 显示烟花弹 /**** 设置烟花参数 ***/ Fire[i].x = Jet[i].x + 10; Fire[i].y = Jet[i].hy; Fire[i].show = false; Fire[i].r = 0; /**** 播放发射声音 ***/ 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; }}六、烟花绽放函数 绽放烟花,烟花个阶段绽放时间间隔,制作变速绽放效果等等,这里大家也可以自己去优化,去增加自己的东西 // 绽放烟花void Show( DWORD* pMem ){ // 烟花个阶段绽放时间间隔,制作变速绽放效果 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(); // 增加爆炸半径,绽放烟花,增加时间间隔做变速效果 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, and do not output if the color value is close to black. If (Fire [I]. Draw) {for (double a = 0; a 0 & & x1)

< Fire[i].width && y1 >

0 & & Y1

< Fire[i].height ) // 只输出图片内的像素点 { int b = Fire[i].xy[x1][y1] & 0xff; int g = ( Fire[i].xy[x1][y1] >

> 8) & 0xff; int r = (fireworks [I] .XY [x1] [y1] > > 16); / / coordinates of fireworks pixels on the window int xx = (int) (Fire[ I] .x + fireworks [I] .r * cos (a)) Int yy = (int) (Fire [I]. Y-Fire [I]. R * sin (a)); / / darker pixels are not output to prevent if from crossing the boundary (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 to draw fireworks}} fireworks. Draw = false 7. Void main () {initgraph (1200); srand (time (0)); / / play background music mciSendString ("open. / fire/bk.mp3 alias bk", 0,0,0); mciSendString ("play bk repeat", 0,0,0); setfillstyle (0) Setfont (36,0, "italics"); setcolor (YELLOW); outtextxy (370,100, "yy shipment _ _). ^ _ ^ "); DWORD T1 = timeGetTime (); / / screening fireworks timing DWORD st1 = timeGetTime (); / / playback pattern timing DWORD* pMem = GetImageBuffer (); / / get window display memory pointer for (int I = 0; I < NUM) Init +) / / initialize fireworks {Init (I);} Load (); / / load fireworks picture information into the corresponding structure BeginBatchDraw () / / start batch drawing while (! kbhit ()) {Sleep (10); / / randomly select 4000 pixels to erase for (int clr = 0; clr < 1000; clr++) {for (int j = 0) J < 2; int py1 +) {int px1 = rand ()% 1200; int py1 = rand ()% 800 If (py1 < 799) / / prevent crossing the boundary pMem [py1 * 1200 + px1] = pMem [py1 * 1200 + px1 + 1] = BLACK / / A pair of video memory assignment to wipe out pixels}} Chose (T1); / / screen fireworks Shoot (); / / launch fireworks Show (pMem) / / fireworks Wishing (); / / scroll character Style (st1); / / pattern launch FlushBatchDraw () / / show all the previous drawing operations}} so far, I believe you have a deeper understanding of "how to write fireworks to the person you like in C language". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report