C++實現(xiàn)動態(tài)煙花代碼
寫在前面
首先,祝大家新年快樂!
即將迎來新的一年——癸卯兔年,祝大家成績、事業(yè)“兔”飛猛進,必定紅紅火火!
在這篇文章中,將用煙花致以大家最好的祝福!
煙花代碼將會用到 Easyx 圖形庫,可以去官網(wǎng)下載:easyx.cn
代碼思路
1 煙花結構體
2 初始化煙花
3 煙花上升
4 煙花爆炸
5 繪制煙花
不需要任何圖片、音效(本來想加的,你要加自己加吧)
開始編寫
提前聲明:代碼純原創(chuàng)!
需要用到的頭文件以及宏:
#include <graphics.h> #include <math.h> #include <time.h> #include <stdio.h> #define MAXNUM 15 #define WIDTH 640 #define HEIGHT 480 #define PI 3.1415926
graphics.h Easyx圖形庫
math.h 計算煙花位置
time.h 選取隨機種子
stdio.h 標準輸入輸出頭文件
MAXNUM 煙花數(shù)量
WIDTH , HEIGHT 窗口的寬、高
PI 圓周率常量,結合數(shù)學庫計算煙花位置
1 煙花結構體
定義的參數(shù)如下:
nowx , nowy 現(xiàn)在坐標
endy 爆炸高度
radio 爆炸半徑
explode 爆炸進度
rgb[3] 煙花顏色rgb值
color 煙花顏色
struct Fire
{
int nowx;
int nowy;
int endy;
int radio;
int explode;
int rgb[3];
COLORREF color;
}fire[MAXNUM];2 初始化煙花
nowx , nowy 窗口外的隨機位置
endy 隨即高度
radio 隨即半徑
explode 一開始設為0
rgb[3] 一些特定的顏色隨機取
color 按照rgb值
void Init()
{
for (int i = 0; i < MAXNUM; i++)
{
fire[i].nowx = rand() % WIDTH;
fire[i].nowy = HEIGHT + rand() % 250 + 50;
fire[i].endy = rand() % 100 + 10;
fire[i].radio = rand() % 50 + 120;
fire[i].explode = 0;
int c[][3] = { {255, 0, 0}, {210, 190, 255}, {255, 120, 0}, {255, 0, 150}, {255, 240, 100}, {10, 255, 255}, {160, 10, 255}, {255, 200, 60} };
int n = rand() % 8;
fire[i].color = RGB(c[n][0], c[n][1], c[n][2]);
fire[i].rgb[0] = c[n][0];
fire[i].rgb[1] = c[n][1];
fire[i].rgb[2] = c[n][2];
}
}3 煙花上升
y坐標 還沒到達爆炸高度時一直上升
for (int i = 0; i < MAXNUM; i++)
{
if (fire[i].nowy > fire[i].endy)
{
fire[i].nowy -= 3;
}
}4 煙花爆炸
爆炸進度 explode 不斷增加,如果到達爆炸半徑,就重新初始化。(接上面的if語句)
else
{
if (fire[i].explode >= fire[i].radio)
{
fire[i].nowx = rand() % WIDTH;
fire[i].nowy = HEIGHT + rand() % 250 + 50;
fire[i].endy = rand() % 100 + 10;
fire[i].radio = rand() % 50 + 120;
fire[i].explode = 0;
int c[][3] = { {255, 0, 0}, {210, 190, 255}, {255, 120, 0}, {255, 0, 150}, {255, 240, 100}, {10, 255, 255}, {160, 10, 255}, {255, 200, 60} };
int n = rand() % 8;
fire[i].color = RGB(c[n][0], c[n][1], c[n][2]);
fire[i].rgb[0] = c[n][0];
fire[i].rgb[1] = c[n][1];
fire[i].rgb[2] = c[n][2];
}
else fire[i].explode++;
}5 繪制煙花
上升
繪制5個圓,大小從大到小,顏色從深到淺(增加rgb值)
for (int i = 0; i < MAXNUM; i++)
{
if (fire[i].nowy > fire[i].endy)
{
for (int size = 5; size > 0; size--)
{
int temp[] = { fire[i].rgb[0], fire[i].rgb[1], fire[i].rgb[2] };
for (int k = 0; k < 3; k++)
{
temp[k] += 50 * (5 - size);
if (temp[k] > 255) temp[k] = 255;
}
setfillcolor(RGB(temp[0], temp[1], temp[2]));
solidcircle(fire[i].nowx, fire[i].nowy + 15*(10 - size), size);
}
}
}爆炸
重頭戲!重頭戲!重頭戲!需要一些簡(fù)單(zá)的數(shù)(gāo)學(děng)常(shù)識(xúe)!
顏色漸變的實現(xiàn)方式跟前面一樣,只不過這里要用三角函數(shù)計算坐標,再用萬有引力、拋物線等改變 y坐標,讓它看得更真實?。ㄅnD:算你小子識相)
具體如何計算坐標,看我的文章,里面有說如何計算
然后是萬有引力,大概需要高度乘以 0.98 ,這里就相當于減去 0.1
由于 easyx 坐標上方為 y = 0,所以,應該是:
y + (HEIGHT - y) * 0.1
再根據(jù)拋物線:y = x^2 ,修改一下就行了
else
{
for (int a = 0; a < 360; a += 30)
{
for (int size = 5; size > 0; size--)
{
int x = cos(a * PI / 180.0) * (fire[i].explode + size * 10) + fire[i].nowx;
int y = sin(a * PI / 180.0) * (fire[i].explode + size * 10) + fire[i].nowy + fire[i].radio / 2;
int temp[] = { fire[i].rgb[0], fire[i].rgb[1], fire[i].rgb[2] };
for (int k = 0; k < 3; k++)
{
temp[k] += 50 * (5 - size);
if (temp[k] > 255) temp[k] = 255;
}
setfillcolor(RGB(temp[0], temp[1], temp[2]));
solidcircle(x, y + (HEIGHT - y) * 0.1 + size * (size - 2), size);
}
}
}效果展示



還挺好看的,不是嗎?
完整代碼
最后,把函數(shù)封裝一下,main 函數(shù)寫出來(有手就行!):
/*******************************
* 項目名稱:新年煙花
* 開發(fā)環(huán)境:vs2022 + Easyx
* 作者:軒
* 代碼長度:137 行
* 完成時間:2023.1.20
* 用時:2.2 小時
*******************************/
#include <graphics.h>
#include <math.h>
#include <time.h>
#include <stdio.h>
#define MAXNUM 15
#define WIDTH 640
#define HEIGHT 480
#define PI 3.1415926
struct Fire
{
int nowx;
int nowy;
int endy;
int radio;
int explode;
int rgb[3];
COLORREF color;
}fire[MAXNUM];
void Init()
{
for (int i = 0; i < MAXNUM; i++)
{
fire[i].nowx = rand() % WIDTH;
fire[i].nowy = HEIGHT + rand() % 250 + 50;
fire[i].endy = rand() % 100 + 10;
fire[i].radio = rand() % 50 + 120;
fire[i].explode = 0;
int c[][3] = { {255, 0, 0}, {210, 190, 255}, {255, 120, 0}, {255, 0, 150}, {255, 240, 100}, {10, 255, 255}, {160, 10, 255}, {255, 200, 60} };
int n = rand() % 8;
fire[i].color = RGB(c[n][0], c[n][1], c[n][2]);
fire[i].rgb[0] = c[n][0];
fire[i].rgb[1] = c[n][1];
fire[i].rgb[2] = c[n][2];
}
}
void Draw()
{
for (int i = 0; i < MAXNUM; i++)
{
if (fire[i].nowy > fire[i].endy)
{
for (int size = 5; size > 0; size--)
{
int temp[] = { fire[i].rgb[0], fire[i].rgb[1], fire[i].rgb[2] };
for (int k = 0; k < 3; k++)
{
temp[k] += 50 * (5 - size);
if (temp[k] > 255) temp[k] = 255;
}
setfillcolor(RGB(temp[0], temp[1], temp[2]));
solidcircle(fire[i].nowx, fire[i].nowy + 15*(10 - size), size);
}
}
else
{
for (int a = 0; a < 360; a += 30)
{
for (int size = 5; size > 0; size--)
{
int x = cos(a * PI / 180.0) * (fire[i].explode + size * 10) + fire[i].nowx;
int y = sin(a * PI / 180.0) * (fire[i].explode + size * 10) + fire[i].nowy + fire[i].radio / 2;
int temp[] = { fire[i].rgb[0], fire[i].rgb[1], fire[i].rgb[2] };
for (int k = 0; k < 3; k++)
{
temp[k] += 50 * (5 - size);
if (temp[k] > 255) temp[k] = 255;
}
setfillcolor(RGB(temp[0], temp[1], temp[2]));
solidcircle(x, y + (HEIGHT - y) * 0.1 + size * (size - 2), size);
}
}
}
}
}
void Move()
{
for (int i = 0; i < MAXNUM; i++)
{
if (fire[i].nowy > fire[i].endy)
{
fire[i].nowy -= 3;
}
else
{
if (fire[i].explode >= fire[i].radio)
{
fire[i].nowx = rand() % WIDTH;
fire[i].nowy = HEIGHT + rand() % 250 + 50;
fire[i].endy = rand() % 100 + 10;
fire[i].radio = rand() % 50 + 120;
fire[i].explode = 0;
int c[][3] = { {255, 0, 0}, {210, 190, 255}, {255, 120, 0}, {255, 0, 150}, {255, 240, 100}, {10, 255, 255}, {160, 10, 255}, {255, 200, 60} };
int n = rand() % 8;
fire[i].color = RGB(c[n][0], c[n][1], c[n][2]);
fire[i].rgb[0] = c[n][0];
fire[i].rgb[1] = c[n][1];
fire[i].rgb[2] = c[n][2];
}
else fire[i].explode++;
}
}
}
int main()
{
srand(time(NULL));
initgraph(640, 480);
Init();
BeginBatchDraw();
while (true)
{
cleardevice();
Draw();
Move();
FlushBatchDraw();
Sleep(2);
}
EndBatchDraw();
closegraph();
return 0;
}到此這篇關于C++實現(xiàn)動態(tài)煙花代碼的文章就介紹到這了,更多相關C++煙花內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

