c++实现新年烟花效果完整代码
作者:独编
这篇文章主要给大家介绍了关于c++实现新年烟花效果的相关资料,文中给出了详细完整代码,适合初学C语言/C++的小伙伴学习研究,需要的朋友可以参考下
分享一下我的烟花代码,图片可以去百度自己搜索
完整代码:
#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; //当前半径 int maxr = 0; //最大半径 int x = 0, y = 0; //中心点坐标(窗口) int cx = 0, cy = 0; //中心点坐标(图画) int xy[240][240]; //保存像素点 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(); //获取系统时间 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; //中心点的坐标(图片) 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); //传入像素点 } } SetWorkingImage(); } //函数测试 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) //烟花弹移动出界 { 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 来自像素点的628个像素坐标 { int x2 = firen->cx + m * cos(a); int y2 = firen->cy - m * sin(a); //如果数据正常,获取像素点的三原色系 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; //求圈上的点在窗体上的坐标 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() //烟花主函数 { 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; }
这是一个烟花连续放的程序,如果要更多的烟花道理相同,如果不想自己写可以私信我哦。
总结
到此这篇关于c++实现新年烟花效果完整代码的文章就介绍到这了,更多相关c++新年烟花内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!