????????廢話不多說,直接上碼
代碼,歌曲,煙花圖片都在壓縮包里
鏈接: https://pan.baidu.com/s/1_459s0fFCAX1DcQa_BnHMQ?pwd=qw12?
提取碼: qw12
要看效果的也可以看我抖音:
1210246294
#include<stdio.h>
#include<easyx.h>
#include<time.h>
#include<stdlib.h>
#include <Windows.h>
#include<math.h>
#include <conio.h>
#pragma comment(lib,"winmm.lib")
#include<mmsystem.h>
IMAGE img;
IMAGE img1;
IMAGE img2;
IMAGE img3;
#define num 10
#define PI 3.14
struct Fire //創(chuàng)建煙花彈結(jié)構(gòu)體類型
{
int x, y;//初始坐標
int max_x, max_y;//煙花最大高度
IMAGE Img[2];//保存圖片
bool boom;//是否要爆開
};
struct Fire fire[num];
struct Show
{
int x, y;//綻放的位置
int cx, cy;//煙花中心點坐標
int r;//當前的半徑
int max_r;//最大半徑
int pixel[200][200];//圖片像素數(shù)組
bool isshow; // 是否綻放
bool isdraw; // 開始輸出像素點
DWORD t1, t2, dt;
int width, height;
};
struct Show show[num];
void initshow(int i)//初始化煙花
{
show[i].cx = 100;
show[i].cy = 100;
show[i].r = 0;
show[i].max_r = 100;
show[i].width = 200;
show[i].height = 200;
show[i].isshow = false;
show[i].dt = 5;
show[i].t1 = timeGetTime();//時間獲取速度
}
void initfire(int i)//初始化煙花彈
{
fire[i].x = rand() % 700 + 100;//煙花彈坐標初始化x=100-800;y=100-400;
fire[i].y = 600;
fire[i].max_x = fire[i].x;
fire[i].max_y = rand() % 300 + 100;
fire[i].boom = false;
loadimage(&fire[i].Img[0], "煙花彈.png", 20, 60);
}
void load()//加載煙花圖片
{
for (int k = 0; k < num; k++)
{ int a = rand() % 3 + 1;
loadimage(&img1, "煙花1.png", 200, 200);//三種不同的煙花
loadimage(&img2, "煙花2.png", 200, 200);
loadimage(&img3, "煙花3.png", 200, 200);
if(a==1)
SetWorkingImage(&img1);
else
if(a==2)
SetWorkingImage(&img2);
else
SetWorkingImage(&img3);
for (int i = 0; i < 200; i++)
{
for (int j = 0; j < 200; j++)
{
show[k].pixel[i][j] = getpixel(i, j);//把圖片像素點放到數(shù)組中
}
}
}
SetWorkingImage(NULL);
}
void Draw(int i, DWORD* pMem)//繪制一圈像素點
{
if (show[i].isdraw)
{
for (double a = 0; a <= 2*PI; a += 0.01) //一圈628個像素點
{
//(x1,y1)是相對于煙花小圖片的 像素 坐標點
int x1 = (int)(show[i].cx + show[i].r * cos(a));
int y1 = (int)(show[i].cy + show[i].r * sin(a));
if (x1 > 0 && x1 < show[i].width && y1>0 && y1 < show[i].height)
{
int b = show[i].pixel[x1][y1] & 0xff; //blue
int g = (show[i].pixel[x1][y1] >> 8) & 0xff; //green
int r = show[i].pixel[x1][y1] >> 16; //red
//(xx,yy)是相對于窗口的 像素 坐標點
int xx = (int)(show[i].x + show[i].r * cos(a));
int yy = (int)(show[i].y + show[i].r * sin(a));
if (r > 0x20 && g > 0x20 && b > 0x20 && xx < 1000 && xx>0 && yy > 0 && yy < 600)
{
//把(x1,y1)坐標上的像素點 賦值 給(xx,yy)坐標點
pMem[yy * 1000 + xx] = BGR(show[i].pixel[x1][y1]);
}
}
}
show[i].isdraw = false;
}
}
void fire_boom(DWORD* pMem)//煙花綻放
{
int drt[16] = { 5, 5, 5, 5, 15, 15, 25, 25, 35, 35, 55, 55, 65, 65, 75, 75 };
for (int i = 0; i < num; i++)
{
show[i].t2 = timeGetTime();
if (show[i].t2 - show[i].t1 > show[i].dt && show[i].isshow == true)
{
if (show[i].r < show[i].max_r)
{
show[i].r++;
show[i].dt = drt[show[i].r / 10]; //每十個煙花像素點改變一下煙花綻放的速度
show[i].isdraw = true;
}
if (show[i].r >= show[i].max_r - 1)
{
show[i].isdraw = false;
initshow(i);
initfire(i);
}
//更新時間
show[i].t1 = show[i].t2;
//可以綻放的狀態(tài)
Draw(i,pMem);
}
}
}
void fire_up()//煙花彈上升
{
for (int i = 0; i < num; i++)
{
putimage(fire[i].x, fire[i].y, &fire[i].Img[0], SRCINVERT);//消除殘影
if (fire[i].y > fire[i].max_y)
{
fire[i].y -= 10;//向上移動
}
else
{ //已到達最高點,準備綻放
show[i].x = fire[i].x+10;
show[i].y = fire[i].y;
fire[i].boom = true;
show[i].isshow = true;
}
putimage(fire[i].x, fire[i].y, &fire[i].Img[0], SRCINVERT);
}
}
void word()//文字提醒-“請按任意鍵...”
{
settextcolor(RED);
settextstyle(25, 0, "宋體");
outtextxy(400, 550, "請");
Sleep(500);
outtextxy(425, 550, "按");
Sleep(500);
outtextxy(450, 550, "任");
Sleep(500);
outtextxy(475, 550, "意");
Sleep(500);
outtextxy(500, 550, "鍵");
Sleep(500);
outtextxy(525, 550, ".");
Sleep(500);
outtextxy(550, 550, ".");
Sleep(500);
outtextxy(575, 550, ".");
}
int main()
{
int i = 0;
srand((unsigned int)time(NULL));
initgraph(1000, 600);//初始化圖形界面
loadimage(&img, "2023.png", 1000, 600);
putimage(0,0,&img,SRCINVERT);//背景圖
mciSendString("open 打上花火.mp3",0,0,0);//播放—“打上花火”
mciSendString("play 打上花火.mp3", 0, 0, 0);
Sleep(1000);
word();//文字提醒-“請按任意鍵...”
_getch();//輸入任意鍵繼續(xù)
DWORD* pMem = GetImageBuffer();
for (int i = 0; i < num; i++)//初始化煙花彈和煙花
{
initfire(i);
initshow(i);
}
load();//加載煙花圖片
while (1)
{
for (int i = 0; i < 3000; i++)
{
int px1 = rand() % 1000; // 0..1199
int py1 = rand() % 600; // 0.799
pMem[py1 * 1000 + px1] = BLACK;
pMem[py1 * 1000 + px1 + 1] = BLACK; // 對顯存賦值擦出像素點
}
fire_up();//發(fā)射煙花彈
fire_boom(pMem);//煙花綻放
Sleep(50);
}
return 0;
}
???運行效果如下:文章來源:http://www.zghlxwxcb.cn/news/detail-721921.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-721921.html
到了這里,關于C語言煙花代碼—兔年頂呱呱的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!