1.前言

最近一部电视剧《点燃我,温暖你》可火啦,大家纷纷在搞男主阿瑟的同款爱心代码,但是虽然网上有它的源码,但都不是 C 语言的,看不懂。我看了一下这个视频,大致分析了一下它的运行过程。用语言描述一下,就是好多爱心,然后从内到外,从小到大的显示。

2.实现

对于这个程序的编写,我的思路是这样的。就如同将一头大象放到冰箱里。就是打开冰箱,放入大象,关上冰箱。编写程序也是如此。

首先就是计算坐标,更新坐标,绘制爱心,这样三步。重复调用,就可以实现。里面的具体细节,可以查看代码。

3.效果展示

4.编译环境:VS2019+easyx图形库插件

5.代码展示:

#include#include#include#include#include struct MyLove{int NUMS;//编号double m;double n;double size;bool Is_show;int x;int y;}; MyLove mylove[400];int CenterX = 320;int CenterY = 180;double Size = 60;void initdata();// 初始化数据void updata();// 更新void movedata();// 平移void showdata();// 显示int* GetRand(int* buf, int count, int range);// 随机数的生成void heart(int x0, int y0, int size, COLORREF C);void HpSleep(int ms); int main(){initgraph(640, 480);initdata();BeginBatchDraw();while (true){updata();showdata();HpSleep(30);// 改为精确延时FlushBatchDraw();cleardevice();}EndBatchDraw();_getch();return 0;} void updata(){int* buf = (int*)malloc(sizeof(int) * 20);buf = GetRand(buf, 20, (int)(2 * Size / 0.01));movedata();for (int i = 0; i < 20; i++){mylove[i].m = buf[i] * 0.01;mylove[i].n = (((sin(buf[(int)i] * 0.01) * sqrt(fabs(cos(buf[(int)i] * 0.01)))) / (sin(buf[(int)i] * 0.01) + 1.4142)) - 2 * sin(buf[(int)i] * 0.01) + 2);mylove[i].size = Size;mylove[i].NUMS = i / 20;mylove[i].Is_show = true;mylove[i].x = (int)(-Size * mylove[i].n * cos(mylove[i].m) + CenterX);mylove[i].y = (int)(-Size * mylove[i].n * sin(mylove[i].m) + CenterY - mylove[i].size);}for (int i = 20; i < 400; i++){mylove[i].size = mylove[i].size + 1;if (mylove[i].size > 80){mylove[i].size = 80;}mylove[i].NUMS = i / 20;mylove[i].x = (int)(-mylove[i].size * mylove[i].n * cos(mylove[i].m) + CenterX);mylove[i].y = (int)(-mylove[i].size * mylove[i].n * sin(mylove[i].m) + CenterY - mylove[i].size);}} void movedata(){for (int i = 399; i > 19; i--){mylove[i] = mylove[i - 20];}} void showdata(){settextcolor(RED);wchar_t c = 0x59;// 0x28 是电话机在 Wingdings 字体中的对应编码for (int i = 0; i < 400; i++){settextstyle(mylove[i].NUMS + 10, 0, "Webdings");setbkmode(TRANSPARENT);outtextxy(mylove[i].x + 20, mylove[i].y + 20, c);}} int* GetRand(int* buf, int count, int range){struct timeb timeSeed;ftime(&timeSeed);srand(timeSeed.time * 1000 + timeSeed.millitm);// milli timefor (int i = 0; i < count; i++){int randTmp = rand() % range;for (int j = 0; j < i; j++){if (buf[j] == randTmp){break;//检查重复。}}buf[i] = randTmp;}return buf;} void initdata(){for (int i = 0; i < 400; i++){mylove[i].NUMS = 0;mylove[i].m = 0;mylove[i].n = 0;mylove[i].size = 0;mylove[i].Is_show = false;mylove[i].x = 0;mylove[i].y = 0;}} // 精确延时函数(可以精确到 1ms,精度 ±1ms)// by yangw80, 2011-5-4void HpSleep(int ms){static clock_t oldclock = clock();// 静态变量,记录上一次 tickoldclock += ms * CLOCKS_PER_SEC / 1000;// 更新 tickif (clock() > oldclock)// 如果已经超时,无需延时oldclock = clock();elsewhile (clock() < oldclock)// 延时Sleep(1);// 释放 CPU 控制权,降低 CPU 占用率,精度 10~16ms//Sleep(0);// 更高精度、更高 CPU 占用率,精度 1ms}