点击蓝字 关注我们
一、写在前面
在编程的世界里,代码不仅仅是冷冰冰的命令,它也可以成为表达情感、传递浪漫的工具。今天,就让小编带着大家用 C++ 语言打造出 李峋同款跳动的爱心 吧!
视频演示
首先,我们需要知道C++作为一种高级编程语言,拥有强大的功能和灵活的语法,非常适合用来制作各种有趣的动画效果。而跳动的爱心,正是一种既简单又富有创意的动画效果,非常适合用来作为编程初学者的练习项目。 要实现这个效果,我们需要用到C++的图形库和定时器功能。图形库可以帮助我们在控制台或窗口中绘制出爱心的形状,而定时器则可以让爱心以一定的频率跳动起来。
二、跳动的爱心
接下来,让我们一步步来实现这个跳动的爱心。 首先,我们需 要 通过 一系列的数学公式来 定义爱心的形状 ,比如使用参数方程来描述爱心的轮廓。 然后,我们可以使用C++的图形库来绘制出这个形状。 当 绘制好爱心 后, 就要想办法 让它 跳 动起来了, 这里我们可以使用C++的定时器功能,设置一个合适的时间间隔,让爱心在每个时间间隔内改变位置或大小,从而产生跳动的效果。
完整代码
#include <graphics.h>
#include <conio.h>
#include <ctime>
#include <cmath>
#include <cstdlib>
struct Point {
double x, y;
COLORREF color;
};
const int MAX_POINTS = 256;
const COLORREF colors[MAX_POINTS] = {
RGB(255, 192, 203), // 浅粉色 (Light Pink)
RGB(255, 182, 193), // 淡粉红 (LightPink)
RGB(255, 105, 180), // 热粉红 (HotPink)
RGB(255, 20, 147), // 深粉色 (DeepPink)
RGB(219, 112, 147), // 浓粉红 (PaleVioletRed)
RGB(255, 174, 185), // 浅玫瑰红 (LightPink)
RGB(255, 0, 144) // 紫红色 (Crimson)
};
const int xScreen = GetSystemMetrics(SM_CXSCREEN);
const int yScreen = GetSystemMetrics(SM_CYSCREEN) - 100;
const double PI = 3.14159265359;
const double E = 2.71828;
const double AVG_DISTANCE = 0.162;
const int NUM_ORIGIN_POINTS = 506;
const int NUM_CIRCLES = 210;
const int NUM_FRAMES = 20;
const int COLOR_RANGE = 6;
Point origin_points[NUM_ORIGIN_POINTS];
Point points[NUM_CIRCLES * NUM_ORIGIN_POINTS];
IMAGE images[NUM_FRAMES];
int create_random(int min, int max) {
return rand() % (max - min + 1) + min;
}
void create_data() {
int index = 0;
double x1 = 0, y1 = 0, x2 = 0, y2 = 0;
// Generate origin points
for (double radian = 0.1; radian <= 2 * PI; radian += 0.005) {
x2 = 16 * pow(sin(radian), 3);
y2 = 13 * cos(radian) - 5 * cos(2 * radian) - 2 * cos(3 * radian) - cos(4 * radian);
double distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
if (distance > AVG_DISTANCE) {
x1 = x2, y1 = y2;
origin_points[index].x = x2;
origin_points[index++].y = y2;
}
}
// Generate points
index = 0;
for (double size = 0.1, lightness = 1.5; size <= 20; size += 0.1) {
double success_p = 1 / (1 + pow(E, 8 - size / 2));
if (lightness > 1) lightness -= 0.0025;
for (int i = 0; i < NUM_ORIGIN_POINTS; ++i) {
if (success_p > create_random(0, 100) / 100.0) {
COLORREF color = colors[create_random(0, COLOR_RANGE)];
points[index].color = RGB(GetRValue(color) / lightness, GetGValue(color) / lightness, GetBValue(color) / lightness);
points[index].x = size * origin_points[i].x + create_random(-4, 4);
points[index++].y = size * origin_points[i].y + create_random(-4, 4);
}
}
}
int points_size = index;
// Generate images
for (int frame = 0; frame < NUM_FRAMES; ++frame) {
images[frame] = IMAGE(xScreen, yScreen);
SetWorkingImage(&images[frame]);
setorigin(xScreen / 2, yScreen / 2);
setaspectratio(1, -1);
for (index = 0; index < points_size; ++index) {
double x = points[index].x, y = points[index].y;
double distance = sqrt(pow(x, 2) + pow(y, 2));
double distance_increase = -0.0009 * distance * distance + 0.35714 * distance + 5;
double x_increase = distance_increase * x / distance / NUM_FRAMES;
double y_increase = distance_increase * y / distance / NUM_FRAMES;
points[index].x += x_increase;
points[index].y += y_increase;
setfillcolor(points[index].color);
solidcircle(points[index].x, points[index].y, 1);
}
for (double size = 17; size < 23; size += 0.3) {
for (index = 0; index < NUM_ORIGIN_POINTS; ++index) {
if ((create_random(0, 100) / 100.0 > 0.6 && size >= 20) || (size < 20 && create_random(0, 100) / 100.0 > 0.95)) {
double x, y;
if (size >= 20) {
x = origin_points[index].x * size + create_random(-frame * frame / 5 - 15, frame * frame / 5 + 15);
y = origin_points[index].y * size + create_random(-frame * frame / 5 - 15, frame * frame / 5 + 15);
}
else {
x = origin_points[index].x * size + create_random(-5, 5);
y = origin_points[index].y * size + create_random(-5, 5);
}
setfillcolor(colors[create_random(0, COLOR_RANGE)]);
solidcircle(x, y, 1);
}
}
}
setaspectratio(1, 1);
settextstyle(100, 0, _T("宋体"));
settextcolor(RGB(219, 112, 147));
outtextxy(-150, -50, _T("我爱你"));
saveimage(_T("爱心.png"), &images[frame]);
setorigin(0, 0);
setaspectratio(1, 1);
loadimage(&images[frame], _T("爱心.png"));
}
SetWorkingImage();
}
void init_graphics() {
HWND hwnd = initgraph(xScreen, yScreen);
RECT rect;
SystemParametersInfo(SPI_GETWORKAREA, 0, ▭, 0);
ShowWindow(hwnd, SW_MAXIMIZE);
SetWindowPos(hwnd, HWND_TOP, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, SWP_SHOWWINDOW);
BeginBatchDraw();
setorigin(xScreen / 2, yScreen / 2);
setaspectratio(1, -1);
srand(static_cast<unsigned>(time(0)));
}
int main() {
init_graphics();
create_data();
graphdefaults();
bool extend = true, shrink = false;
for (int frame = 0; !_kbhit();) {
putimage(0, 0, &images[frame]);
FlushBatchDraw();
Sleep(20);
cleardevice();
if (extend)
frame == 19 ? (shrink = true, extend = false) : ++frame;
else
frame == 0 ? (shrink = false, extend = true) : --frame;
}
EndBatchDraw();
closegraph();
return 0;
}
完成这些步骤后,我们就成功打造出了一个李峋同款跳动的爱心( 在代码的第112行可以修改文字哦 )。每当你运行这段代码时,这个爱心就会在屏幕上跳动起来,为你的编程之旅增添一份浪漫和乐趣。
代码分析
这段代码使用C语言和Turbo C++图形库来创建一个动态的心形图案动画。
- 定义结构体和常量
-
Point 结构体包含点的坐标(x 和 y)以及颜色值(COLORREF)。
-
预定义的颜色数组 colors[],包括多种粉红色调。
-
屏幕尺寸 xScreen 和 yScreen 通过系统函数获取。
-
数学常数 PI 和 E。
-
其他常量如 MAX_POINTS,AVG_DISTANCE 等,用于控制生成点的数量和分布。
- 随机数生成函数 create_random()
-
返回指定范围内的随机整数。
- 数据生成函数 create_data()
-
生成 origin_points[]:这些点用于描绘心形的基本轮廓,通过参数方程计算得到。
-
生成 points[]:这些点基于 origin_points[] 生成,通过缩放和随机偏移来形成心形的纹理。
-
生成 images[]:这是一系列帧,每一帧都是在前一帧的基础上对点的位置进行微调,并添加新的点,最终保存为图像文件。
- 图形初始化函数 init_graphics()
-
初始化图形窗口,设置屏幕尺寸,并准备绘图环境。
- 主函数 main()
-
调用 init_graphics() 准备图形环境。
-
调用 create_data() 生成动画数据。
-
在循环中按顺序显示每一帧,实现动画效果,直到检测到键盘输入为止。
- 动画的具体实现
-
心形图案通过调整点的位置和大小随时间变化来产生脉动效果。
-
随机添加新点以增强视觉效果。
-
每一帧被保存为图像文件 ("爱心.png"),然后加载并显示,形成连续动画
由于代码中涉及到大量图形库特定的函数和概念,理解它们需要一定的图形编程基础,尤其是Turbo C++图形库。如果您对此不熟悉,我建议可以先从基本的图形编程概念入手,逐步学习和理解。
通过这个项目,我们不仅学会了如何使用C++语言制作动画效果,还学会了如何将情感融入到代码中。这让我们更加深刻地认识到,编程不仅仅是一种技能,更是一种表达自我、传递情感的方式。所以,无论你是编程初学者还是资深开发者,都不妨尝试一下用C++打造一款跳动的爱心吧!相信在这个过程中,你会收获到很多的乐趣。
运行效果
三、写在后面
感谢小伙伴们的支持,更多精彩内容等你发现喔~
精彩内容
序号 | 目录 |
---|---|
1 | C++《黑客帝国》同款炫酷代码雨 |
2 | C语言呈现一场精彩的新春烟花秀 |
3 | 我用C语言写了一个贪吃蛇小游戏! |
4 | 今年会遇到心软的神吗(C语言) |
5 | C语言满屏飘字表白代码(可修改文案) |
6 | C语言实现跳动的爱心完整代码 |
7 | 一起用c语言模拟大雪纷飞的效果吧! |
8 | 一起用C语言画个爱心吧! |
9 | 我用C语言实现的文字跑马灯,简直是程序员的表白神器! |
10 | C语言圣诞树代码 |
11 | 假期没事干?整个小游戏玩玩 |
如果你也喜欢这篇文章,可以留下小心心吗~
个人主页