c++實(shí)現(xiàn)新年煙花效果完整代碼
分享一下我的煙花代碼,圖片可以去百度自己搜索
完整代碼:
#include <iostream> #include <graphics.h> #include <easyx.h> #include "math.h" #include "time.h" //煙花彈 struct Jet { int x = 0, y = 0; int hx = 0, hy = 0; unsigned long t1 = 0, t2 = 0, dt = 0; IMAGE img; bool isshoot = 1; }jet; Jet jet1, jet2, jet3; //煙花 struct Fire { int r = 0; //當(dāng)前半徑 int maxr = 0; //最大半徑 int x = 0, y = 0; //中心點(diǎn)坐標(biāo)(窗口) int cx = 0, cy = 0; //中心點(diǎn)坐標(biāo)(圖畫) int xy[240][240]; //保存像素點(diǎn) bool isboom = 0; //是否爆炸 bool isdraw = 0; //是否顯示 unsigned long t1 = 0, t2 = 0, dt = 0; //爆炸速度 }fire; Fire fire1, fire2, fire3; void jet_init(Jet* jetn) //煙花彈的初始化 { jetn->x = rand() % 1100 - 20; jetn->y = rand() % 50 + 700; jetn->hx = jetn->x; jetn->hy = rand() % 450; jetn->t1 = GetTickCount(); //獲取系統(tǒng)時(shí)間 jetn->isshoot = true; jetn->dt = rand() % 10 + 1; } void fire_init(Fire* firen) { firen->r = 0; firen->maxr = 120; firen->cx = 120; firen->cy = 120; //中心點(diǎn)的坐標(biāo)(圖片) firen->isboom = true; firen->isdraw = false; firen->t1 = GetTickCount(); firen->dt = 5; //煙花爆炸速度 IMAGE fimg; loadimage(&fimg, L"煙花4.jpg", 240, 240); SetWorkingImage(&fimg); for (int a = 0; a < 240; a++) { for (int b = 0; b < 240; b++) { firen->xy[a][b] = getpixel(a, b); //傳入像素點(diǎn) } } SetWorkingImage(); } //函數(shù)測(cè)試 void fire_text(Jet* jetn, Fire* firen) { DWORD* pmem = GetImageBuffer(); jetn->t2 = GetTickCount(); if (jetn->t2 - jetn->t1 > jetn->dt && jetn->isshoot == true) { putimage(jetn->x, jetn->y, &jetn->img, SRCINVERT); if (jetn->y > jetn->hy) { jetn->y -= 5; putimage(jetn->x, jetn->y, &jetn->img, SRCINVERT); } if (jetn->y <= jetn->hy) //煙花彈移動(dòng)出界 { jetn->isshoot = false; putimage(jetn->x, jetn->y, &jetn->img, SRCINVERT); fire_init(firen); //煙花的初始化 firen->x = jetn->hx + 10; firen->y = jetn->hy - 20; } if (jetn->isshoot == false && firen->isboom == true && firen->isdraw == 0) { jet_init(jetn); putimage(jetn->x, jetn->y, &jetn->img, SRCINVERT); } jetn->t1 = jetn->t2; //煙花 int drt[13] = { 5,5,5,10,10,15,15,25,35,40,40,55,55 }; //不一樣的速度 firen->t2 = GetTickCount(); if (firen->t2 - firen->t1 > firen->dt && firen->isboom == true) { if (firen->r < firen->maxr) { firen->r++; firen->dt = drt[firen->r / 10]; firen->isdraw = true; } if (firen->r > firen->maxr - 1) { firen->isdraw = false; firen->isboom = false; //初始化煙花 firen->r = 0; firen->maxr = 120; } firen->t1 = firen->t2; if (firen->isdraw = true) { for (double a = 0; a < 6.28; a += 0.01) { for (int m = 0; m <= firen->r; m++) //x1 y1 來自像素點(diǎn)的628個(gè)像素坐標(biāo) { int x2 = firen->cx + m * cos(a); int y2 = firen->cy - m * sin(a); //如果數(shù)據(jù)正常,獲取像素點(diǎn)的三原色系 if (x2 > 0 && x2 < 240 && y2 > 0 && y2 < 240) { int b = firen->xy[x2][y2] & 0xff; int g = (firen->xy[x2][y2] >> 8) & 0xff; int r = firen->xy[x2][y2] >> 16; //求圈上的點(diǎn)在窗體上的坐標(biāo) int xx2 = firen->x + m * cos(a); int yy2 = firen->y - m * sin(a); if (/*b > 0x20 && g > 0x20 && r > 0x20 &&*/ xx2 < 1200 && xx2 > 0 && yy2 > 0 && yy2 <= 600) { pmem[yy2 * 1100 + xx2] = BGR(firen->xy[x2][y2]); } } } } firen->isdraw == false; } } } } int ss = 0; int Fire_Finally() //煙花主函數(shù) { initgraph(1100, 800); srand(time(0)); /*-----------------------------------------圖片------------------------------------*/ jet_init(&jet); loadimage(&jet.img, L"煙花彈2.jpg", 20, 50); putimage(jet.x, jet.y, &jet.img, SRCINVERT); DWORD* pmem = GetImageBuffer(); BeginBatchDraw(); while (1) { for (int clr = 0; clr < 1000; clr++) { for (int j = 0; j < 2; j++) { int px1 = rand() % 1200; int py1 = rand() % 650; if (py1 < 790) { pmem[py1 * 1200 + px1] = pmem[py1 * 1200 + px1 + 1] = BLACK; } } } fire_text(&jet, &fire); FlushBatchDraw(); } EndBatchDraw(); closegraph(); return 0; } int main() { Fire_Finally(); system("pause"); return 0; }
這是一個(gè)煙花連續(xù)放的程序,如果要更多的煙花道理相同,如果不想自己寫可以私信我哦。
總結(jié)
到此這篇關(guān)于c++實(shí)現(xiàn)新年煙花效果完整代碼的文章就介紹到這了,更多相關(guān)c++新年煙花內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++實(shí)現(xiàn)KFC點(diǎn)餐系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)KFC點(diǎn)餐系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07一文讓你不再害怕指針之C指針詳解(經(jīng)典,非常詳細(xì))
這篇文章主要給大家介紹了C指針的相關(guān)資料,文中介紹的很經(jīng)典,非常詳細(xì),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用C指針具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08文件編譯時(shí)出現(xiàn)multiple definition of ''xxxxxx''的具體解決方法
以下是對(duì)文件編譯時(shí)出現(xiàn)multiple definition of 'xxxxxx'的解決方法進(jìn)行了詳細(xì)的分析介紹,如也遇到此問題的朋友們可以過來參考下2013-07-07C語言內(nèi)存函數(shù)的使用及其模擬實(shí)現(xiàn)
這篇文章主要介紹了C語言內(nèi)存函數(shù)的使用及其模擬實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-10-10OpenCV實(shí)現(xiàn)視頻綠幕背景替換功能的示例代碼
這篇文章主要介紹了如何利用OpenCV實(shí)現(xiàn)視頻綠幕背景替換功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)OpenCV有一定的幫助,感興趣的可以學(xué)習(xí)一下2023-02-02