最新520表白程序(C++).doc
精品资料520表白程序(C+).菜鸟奉献你一个520表白神器爱情总是那么神圣,烂漫,每个人都会有无限的向往,即便是程序员(自我笑下),忽然我想起周星驰的那些话“曾经有一段爱情摆在我面前。我希望是一万年”,于是我试着做了表白程序。实现原理:原理比较的简单,主要是运用windows API来实现,程序初步实现的功能是显示文字,图像,歌曲(重复),燃放烟花(循环),程序的实现过程大概可以描述为烟花燃放的一个过程(初始化,上升,爆炸,循环),具体实现方法不再复述了,代码比较简单。工具:VS2013 Eaxy(冬至版)使用TC库,不是VC库,用Eaxy主要代码:#include "graphics.h"#include <conio.h>#include <math.h>#include <time.h>#include <stdio.h>#include <Mmsystem.h>#pragma comment ( lib, "Winmm.lib" )/* 宏定义区 */#define NUM13/ 烟花种类数量宏定义/* 结构定义区 */ 烟花结构struct FIREint r;/ 当前爆炸半径int max_r;/ 爆炸中心距离边缘最大半径int x, y;/ 爆炸中心在窗口的坐标int cen_x, cen_y;/ 爆炸中心相对图片左上角的坐标int width, height;/ 图片的宽高int xy240240;/ 储存图片像素点bool show;/ 是否绽放bool draw;/ 开始输出像素点DWORD t1, t2, dt;/ 绽放速度FireNUM;/ 烟花弹结构struct JETint x, y;/ 喷射点坐标int hx, hy;/ 最高点坐标-将赋值给 FIRE 里面的 x, yint height;/ 烟花高度bool shoot;/ 是否可以发射DWORD t1, t2, dt;/ 发射速度IMAGE img2;/ 储存花弹一亮一暗图片byte n : 1;/ 图片下标JetNUM;/* 函数申明区 */void Init(int);/ 初始化烟花void Load();/ 加载烟花图片void Shoot();/ 发射烟花void Chose(DWORD&);/ 筛选烟花void Style(DWORD&);/ 发射样式void Show(DWORD*);/ 绽放烟花/ 主函数void main()initgraph(1200, 800);srand(time(0);/ 播放背景音乐mciSendString("open ./fire/bk.mp3 alias bk", 0, 0, 0);mciSendString("play bk repeat", 0, 0, 0);/setfillstyle(0);settextstyle(50, 0, "楷体");setcolor(YELLOW);outtextxy(480, 100, "My Dear");outtextxy(400, 200, "愿你悲伤有人分担!");outtextxy(400, 280, "愿你孤独有人陪伴!");getchar();cleardevice();settextstyle(25, 0, "楷体");outtextxy(400, 250, "曾经");outtextxy(400, 300, "有一份真挚的爱情摆在我的面前");outtextxy(400, 350, "我没有珍惜");outtextxy(400, 400, "直到失去了才后悔莫及");outtextxy(400, 450, "世间最痛苦的事情莫过于此");outtextxy(400, 500, "如果上天能给我一个在来一次的机会");outtextxy(400, 550, "我会对那个女孩说");outtextxy(400, 600, "我爱你,一万年");outtextxy(700, 660, "谢新明");getchar();DWORD t1 = timeGetTime();/ 筛选烟花计时DWORD st1 = timeGetTime();/ 播放花样计时DWORD* pMem = GetImageBuffer();/ 获取窗口显存指针for (int i = 0; i < NUM; i+)/ 初始化烟花Init(i);Load();/ 将烟花图片信息加载进相应结构中BeginBatchDraw();/ 开始批量绘图while (!kbhit()Sleep(10);/ 随机选择 4000 个像素点擦除for (int clr = 0; clr < 1000; clr+)for (int j = 0; j < 2; j+)int px1 = rand() % 1200;int py1 = rand() % 800;if (py1 < 799)/ 防止越界 pMempy1 * 1200 + px1 = pMempy1 * 1200 + px1 + 1 = BLACK; / 对显存赋值擦出像素点Chose(t1);/ 筛选烟花Shoot();/ 发射烟花Show(pMem);/ 绽放烟花Style(st1);/ 花样发射FlushBatchDraw();/ 显示前面的所有绘图操作/ 初始化烟花参数void Init(int i)/ 分别为:烟花中心到图片边缘的最远距离、烟花中心到图片左上角的距离 (x、y) 两个分量int r13 = 120, 120, 155, 123, 130, 147, 138, 138, 130, 135, 140, 132, 155 ;int x13 = 120, 120, 110, 117, 110, 93, 102, 102, 110, 105, 100, 108, 110 ;int y13 = 120, 120, 85, 118, 120, 103, 105, 110, 110, 120, 120, 104, 85 ;/* 初始化烟花 */Firei.x = 0;/ 烟花中心坐标Firei.y = 0;Firei.width = 240;/ 图片宽Firei.height = 240;/ 图片高Firei.max_r = ri;/ 最大半径Firei.cen_x = xi;/ 中心距左上角距离Firei.cen_y = yi;Firei.show = false;/ 是否绽放Firei.dt = 5;/ 绽放时间间隔Firei.t1 = timeGetTime();Firei.r = 0;/ 从 0 开始绽放/* 初始化烟花弹 */Jeti.x = -240;/ 烟花弹左上角坐标Jeti.y = -240;Jeti.hx = -240;/ 烟花弹发射最高点坐标Jeti.hy = -240;Jeti.height = 0;/ 发射高度Jeti.t1 = timeGetTime();Jeti.dt = rand() % 10;/ 发射速度时间间隔Jeti.n = 0;/ 烟花弹闪烁图片下标Jeti.shoot = false;/ 是否发射/ 加载图片void Load()/* 储存烟花的像素点颜色 */IMAGE fm, gm;loadimage(&fm, "./fire/flower.jpg", 3120, 240);for (int i = 0; i < 13; i+)SetWorkingImage(&fm);getimage(&gm, i * 240, 0, 240, 240);SetWorkingImage(&gm);for (int a = 0; a < 240; a+)for (int b = 0; b < 240; b+)Firei.xyab = getpixel(a, b);/* 加载烟花弹 */IMAGE sm;loadimage(&sm, "./fire/shoot.jpg", 200, 50);for (int i = 0; i < 13; i+)SetWorkingImage(&sm);int n = rand() % 5;getimage(&Jeti.img0, n * 20, 0, 20, 50);/ 暗getimage(&Jeti.img1, (n + 5) * 20, 0, 20, 50);/ 亮SetWorkingImage();/ 设置回绘图窗口/ 在一定范围内筛选可发射的烟花,并初始化发射参数,输出烟花弹到屏幕,播放声音void Chose(DWORD& t1)DWORD t2 = timeGetTime();if (t2 - t1 > 100)int n = rand() % 20;if (n < 13 && Jetn.shoot = false && Firen.show = false)/* 重置烟花弹,预备发射 */Jetn.x = rand() % 1200;Jetn.y = rand() % 100 + 600;Jetn.hx = Jetn.x;Jetn.hy = rand() % 400;Jetn.height = Jetn.y - Jetn.hy;Jetn.shoot = true;putimage(Jetn.x, Jetn.y, &Jetn.imgJetn.n, SRCINVERT);/* 播放每个烟花弹的声音 */char c150, c230, c330;sprintf(c1, "open ./fire/shoot.mp3 alias s%d", n);sprintf(c2, "play s%d", n);sprintf(c3, "close n%d", n);mciSendString(c3, 0, 0, 0);mciSendString(c1, 0, 0, 0);mciSendString(c2, 0, 0, 0);t1 = t2;/ 扫描烟花弹并发射void Shoot()for (int i = 0; i < 13; i+)Jeti.t2 = timeGetTime();if (Jeti.t2 - Jeti.t1 > Jeti.dt && Jeti.shoot = true)/* 烟花弹的上升 */putimage(Jeti.x, Jeti.y, &Jeti.imgJeti.n, SRCINVERT);if (Jeti.y > Jeti.hy)Jeti.n+;Jeti.y -= 5;putimage(Jeti.x, Jeti.y, &Jeti.imgJeti.n, SRCINVERT);/* 上升到高度的 3 / 4,减速 */if (Jeti.y - Jeti.hy) * 4 < Jeti.height)Jeti.dt = rand() % 4 + 10;/* 上升到最大高度 */if (Jeti.y <= Jeti.hy)/ 播放爆炸声char c150, c230, c330;sprintf(c1, "open ./fire/bomb.wav alias n%d", i);sprintf(c2, "play n%d", i);sprintf(c3, "close s%d", i);mciSendString(c3, 0, 0, 0);mciSendString(c1, 0, 0, 0);mciSendString(c2, 0, 0, 0);putimage(Jeti.x, Jeti.y, &Jeti.imgJeti.n, SRCINVERT);/ 擦掉烟花弹Firei.x = Jeti.hx + 10;/ 在烟花弹中间爆炸Firei.y = Jeti.hy;/ 在最高点绽放Firei.show = true;/ 开始绽放Jeti.shoot = false;/ 停止发射Jeti.t1 = Jeti.t2;/ 显示花样void Style(DWORD& st1)DWORD st2 = timeGetTime();if (st2 - st1 > 20000)/ 一首歌的时间/ 心形坐标int x13 = 60, 75, 91, 100, 95, 75, 60, 45, 25, 15, 25, 41, 60 ;int y13 = 65, 53, 40, 22, 5, 4, 20, 4, 5, 22, 40, 53, 65 ;for (int i = 0; i < NUM; i+)/cleardevice();/* 规律分布烟花弹 */Jeti.x = xi * 10;Jeti.y = (yi + 75) * 10;Jeti.hx = Jeti.x;Jeti.hy = yi * 10;Jeti.height = Jeti.y - Jeti.hy;Jeti.shoot = true;Jeti.dt = 7;putimage(Jeti.x, Jeti.y, &Jeti.imgJeti.n, SRCINVERT);/ 显示烟花弹/* 设置烟花参数 */Firei.x = Jeti.x + 10;Firei.y = Jeti.hy;Firei.show = false;Firei.r = 0;/* 播放发射声音 */char c150, c230, c330;sprintf(c1, "open ./fire/shoot.mp3 alias s%d", i);sprintf(c2, "play s%d", i);sprintf(c3, "close n%d", i);mciSendString(c3, 0, 0, 0);mciSendString(c1, 0, 0, 0);mciSendString(c2, 0, 0, 0);st1 = st2;/ 绽放烟花void Show(DWORD* pMem)/ 烟花个阶段绽放时间间隔,制作变速绽放效果int drt16 = 5, 5, 5, 5, 5, 6, 25, 25, 25, 25, 55, 55, 55, 55, 55 ;for (int i = 0; i < NUM; i+)Firei.t2 = timeGetTime();/ 增加爆炸半径,绽放烟花,增加时间间隔做变速效果if (Firei.t2 - Firei.t1 > Firei.dt && Firei.show = true)if (Firei.r < Firei.max_r)Firei.r+;Firei.dt = drtFirei.r / 10;Firei.draw = true;if (Firei.r >= Firei.max_r - 1)Firei.draw = false;Init(i);Firei.t1 = Firei.t2;/ 如果该号炮花可爆炸,根据当前爆炸半径画烟花,颜色值接近黑色的不输出。if (Firei.draw)for (double a = 0; a <= 6.28; a += 0.01)int x1 = (int)(Firei.cen_x + Firei.r * cos(a);/ 相对于图片左上角的坐标int y1 = (int)(Firei.cen_y - Firei.r * sin(a);if (x1 > 0 && x1 < Firei.width && y1 > 0 && y1 < Firei.height)/ 只输出图片内的像素点int b = Firei.xyx1y1 & 0xff;int g = (Firei.xyx1y1 >> 8) & 0xff;int r = (Firei.xyx1y1 >> 16);/ 烟花像素点在窗口上的坐标int xx = (int)(Firei.x + Firei.r * cos(a);int yy = (int)(Firei.y - Firei.r * sin(a);/ 较暗的像素点不输出、防止越界if (r > 0x20 && g > 0x20 && b > 0x20 && xx > 0 && xx < 1200 && yy > 0 && yy < 800)pMemyy * 1200 + xx = BGR(Firei.xyx1y1);/ 显存操作绘制烟花Firei.draw = false;程序效果:(enter 键继续,enter键退出)图一. 文字界面一图二. 文字界面二图三. 烟花效果图(1)图四. 烟花效果图(2)图五. 烟花效果(3)图六. 烟花效果(4)注:如果需要素材(图片,音乐等)和源码,可以加Q1623451686,希望和大家一起交流!结束语: 程序员的爱情,不是简简单单的三个字,而是我用特有的表达告诉你,其实你在我心里有多么重要,愿天下有情人终成眷属!。(感觉好肉麻啊!)