代码如下
- #include#include#include#include#include#include#include#pragma comment(lib,"winmm.lib")const auto PI = 3.14159;const auto NUM = 100;//烟花个数void clss(int a);void delay(int a);void detabase(int a);//烟花弹struct jet{ int x, y; //烟花弹坐标 int hx, hy; //烟花弹最高点坐标 bool shoot; //烟花弹是否处于发射状态 DWORD t1, t2, dt; //发射时间 引爆时间 隔断时间 IMAGE img[2]; //2张图片 一明一暗 01下标 byte n : 1; //C结构体 位段 //n 变量 1个位 0,1 n++ 0,1,0,1}jet[NUM]; //烟花弹个数//烟花struct Fire{ int x, y; //烟花的坐标 int r; //烟花的半径 int max_r; //烟花的最泰半径 int cen_x, cen_y; //中心距左上角的间隔 int width, height; //长宽 int xy[240][240]; //重要,像素,矩阵 bool draw; //画出 bool show; //显示 DWORD t1, t2, dt; //发射时间 引爆时间 隔断时间}fire[NUM];void FireInit(int i){ //初始化烟花弹 jet[i].t1 = GetTickCount64(); //GetTickCount64()返回从使用系统启动到当前所颠末的毫秒数。使用前包罗windows.h。 jet[i].shoot = false; //未发射 jet[i].dt = 10; //上升时间 jet[i].n = 0; //初始化烟花 fire[i].show = false; //未引爆 fire[i].r = 0; fire[i].dt = 5; //上升时间 fire[i].t1 = GetTickCount64(); fire[i].max_r = rand() % 50 + 100; //100-149 fire[i].cen_x = rand() % 30 + 80; //中心距左上角的间隔 fire[i].cen_y = rand() % 30 + 80; // fire[i].width = 240; //宽 fire[i].height = 240; //长}//加载资源void Load(){ //加载烟花弹 IMAGE jetimg; loadimage(&jetimg, L"./fire/shoot.jpg", 200, 50); SetWorkingImage(&jetimg); for (int i = 0; i < NUM; i++) { int n = rand() % 5; //01234 getimage(&jet[i].img[0], n * 20, 0, 20, 50); getimage(&jet[i].img[1], (n + 5) * 20, 0, 20, 50); } SetWorkingImage(NULL); //加载烟花 IMAGE fireimage, Fireimage; loadimage(&Fireimage, L"./fire/flower.jpg", 3120, 240); for (int i = 0; i < NUM; i++) { SetWorkingImage(&Fireimage); getimage(&fireimage, i * 240, 0, 240, 240); SetWorkingImage(&fireimage); for (int a = 0; a < 240; a++) { for (int b = 0; b < 240; b++) { fire[i].xy[a][b] = getpixel(a, b); } } } SetWorkingImage(NULL);}//选择烟花弹void ChoiceJet(DWORD& t1){ DWORD t2 = GetTickCount64(); if (t2 - t1 > 100) //烟花弹出现的时间隔断100ms { //烟花弹个数 int i = rand() % 10; //不处于发射状态 if (jet[i].shoot == false && fire[i].show == false) { //烟花弹 jet[i].x = rand() % 1000; jet[i].y = rand() % 100 + 450; //450-549 jet[i].hx = jet[i].x; jet[i].hy = rand() % 300; //0-299 jet[i].shoot = true; //发射状态 putimage(jet[i].x, jet[i].y, &jet[i].img[jet[i].n], SRCINVERT); } t1 = t2; }}//判断发射void Shoot(){ for (int i = 0; i < NUM; i++) { jet[i].t2 = GetTickCount64(); if (jet[i].t2 - jet[i].t1 >= jet[i].dt && jet[i].shoot == true) { 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); if (jet[i].y = 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; FireInit(i); } fire[i].t1 = fire[i].t2; // 如果该号炮花可爆炸,根据当前爆炸半径画烟花,颜色值靠近玄色的不输出。 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 = (fire[i].xy[x1][y1] >> 16); // 烟花像素点在窗口上的坐标 int xx = (int)(fire[i].x + fire[i].r * cos(a)); int yy = (int)(fire[i].y - fire[i].r * sin(a)); //较暗的像素点不输出、防止越界 if (r > 0x20 && g > 0x20 && b > 0x20 && xx > 0 && xx < 1000 && yy >0 && yy < 600) { pMem[yy * 1000 + xx] = BGR(fire[i].xy[x1][y1]); } fire[i].draw = false; } } } } }}//菜单界面void welcome(){ setcolor(YELLOW); for (int i = 0; i < 50; i++) { int x = 600 + int(180 * sin(PI * 2 * i / 60)); int y = 200 + int(180 * cos(PI * 2 * i / 60)); cleardevice(); settextstyle(i, 0, L"楷体"); outtextxy(x - 80, y, L"一个小步伐送上"); outtextxy(x - 10, y + 100, L"献给我们自己"); Sleep(65); } Sleep(130); cleardevice(); settextstyle(25, 0, L"楷体"); outtextxy(400, 200, L"2020实属不易"); outtextxy(400, 250, L"2021即将到来"); outtextxy(400, 300, L"----刘仁宇"); Sleep(2000); cleardevice();}//主函数int main(int agrc, char* agrv[]){ mciSendString(L"open 1.mp3",NULL,NULL,NULL); mciSendString(L"play 1.mp3",NULL,NULL,NULL); int a; for (a = 0; a < 17; a++) { detabase(a); delay(100000); clss(1); } detabase(17); delay(500000); //初始界面(1000,600) initgraph(1000, 600); //初始化种子 srand((unsigned int)time(NULL)); welcome(); DWORD t1 = GetTickCount64(); DWORD* pMem = GetImageBuffer(); for (int i = 0; i < NUM; i++) { FireInit(i); } Load(); BeginBatchDraw(); while (1) { // 随机选择像素点擦除 for (int clr = 0; clr < 200; clr++) { int px1 = rand() % 1000; int py1 = rand() % 600; // 防止越界 if (py1 < 599) { //对显存赋值擦除像素点 pMem[py1 * 1000 + px1] = pMem[py1 * 1000 + px1 + 1] = BLACK; } } ChoiceJet(t1); Shoot(); ShowFire(pMem); FlushBatchDraw(); } system("pause"); return 0;}void clss(int a){ system("cls");}void delay(int a){ while (a) { a--; }}void detabase(int a){ switch (a) { case 1: { printf("新"); } break; case 2: { printf("年"); } break; case 3: { printf("快"); } break; case 4: { printf("乐"); } break; case 5: { printf("H"); } break; case 6: { printf("a"); } break; case 7: { printf("p"); } break; case 8: { printf("p"); } break; case 9: { printf("y"); } break; case 10: { printf("N"); } break; case 11: { printf("e"); } break; case 12: { printf("w"); } break; case 13: { printf("Y"); } break; case 14: { printf("e"); } break; case 15: { printf("a"); } break; case 16: { printf("r"); } break; case 17: { printf("新年快乐\nHappy New Year"); } break; }}
复制代码 有没有什么办理方法?
来源:https://blog.csdn.net/liourenyu/article/details/112060993
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |