C++超好看动态爱心代码,快来看看吧

140 篇文章 0 订阅
35 篇文章 1 订阅

前言:今天我们给大家介绍一个有趣的代码,那就是爱心代码,前提是这段代码要先下载一个东西,就是有关C++头文件的,这段代码各位看看就好,当个乐子,因为涉及的代码知识很多。如果大家有兴趣研究的,可以把整段代码看一看。

下面直接先展现代码了:

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <graphics.h>//这个头文件需要下载一个软件
#include <time.h>
#include <math.h>
#include <stdlib.h>
#include<conio.h>
struct Point {
    double x, y;
    COLORREF color;
};
 
COLORREF colors[7] = { RGB(255,32,83),RGB(252,222,250), RGB(255,0,0), RGB(255,0,0), RGB(255,2,2), RGB(255,0,8),  RGB(255,5,5) };
const int xScreen = 1200;
const int yScreen = 800;
const double PI = 3.1426535159;
const double e = 2.71828;
const double averag_distance = 0.162;
const int quantity = 506;
const int circles = 210;
const int frames = 20;
Point origin_points[quantity];
Point points[circles * quantity];
IMAGE images[frames];
 
double screen_x(double x)
{
    x += xScreen / 2;
    return x;
}
 
 
double screen_y(double y)
{
    y = -y + yScreen / 2;
    return y;
}
 
 
int creat_random(int x1, int x2)
{
    if (x2 > x1)
        return rand() % (x2 - x1 + 1) + x1;
}
 
void creat_data()
{
    int index = 0;
    double x1 = 0, y1 = 0, x2 = 0, y2 = 0;
    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 > averag_distance)
        {
            x1 = x2, y1 = y2;
            origin_points[index].x = x2;
            origin_points[index++].y = y2;
        }
    }
 
 
    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 < quantity; ++i)
        {
            if (success_p > creat_random(0, 100) / 100.0)
            {
                points[index].color = colors[creat_random(0, 6)];
                points[index].x = size * origin_points[i].x + creat_random(-4, 4);
                points[index++].y = size * origin_points[i].y + creat_random(-4, 4);
            }
        }
    }
 
    int points_size = index;
 
    for (int frame = 0; frame < frames; ++frame)
    {
        images[frame] = IMAGE(xScreen, yScreen);
        SetWorkingImage(&images[frame]);
 
        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 diatance_increase = -0.0009 * distance * distance + 0.35714 * distance + 5;
            double x_increase = diatance_increase * x / distance / frames;
            double y_increase = diatance_increase * y / distance / frames;
            points[index].x += x_increase;
            points[index].y += y_increase;
            setfillcolor(points[index].color);
            solidcircle(screen_x(points[index].x), screen_y(points[index].y), 1);
 
        }
 
        for (double size = 17; size < 23; size += 0.3)
        {
            for (index = 0; index < quantity; ++index)
            {
                if ((creat_random(0, 100) / 100.0 > 0.6 && size >= 20) || (size < 20 && creat_random(0, 100) / 100.0 > 0.95))
                {
                    double x, y;
                    if (size >= 20)
                    {
                        x = origin_points[index].x * size + creat_random(-frame * frame / 5 - 15, frame * frame / 5 + 15);
                        y = origin_points[index].y * size + creat_random(-frame * frame / 5 - 15, frame * frame / 5 + 15);
                    }
                    else
                    {
                        x = origin_points[index].x * size + creat_random(-5, 5);
                        y = origin_points[index].y * size + creat_random(-5, 5);
                    }
 
                    setfillcolor(colors[creat_random(0, 6)]);
                    solidcircle(screen_x(x), screen_y(y), 1);
                }
 
 
                for (double size = 17; size < 23; size += 0.3)
                {
                    for (index = 0; index < quantity; ++index)
                    {
                        if ((creat_random(0, 100) / 100.0 > 0.6 && size >= 20) || (size < 20 && creat_random(0, 100) / 100.0 > 0.95))
                        {
                            double x, y;
                            if (size >= 20)
                            {
                                x = origin_points[index].x * size + creat_random(-frame * frame / 5 - 15, frame * frame / 5 + 15);
                                y = origin_points[index].y * size + creat_random(-frame * frame / 5 - 15, frame * frame / 5 + 15);
                            }
                            else
                            {
                                x = origin_points[index].x * size + creat_random(-5, 5);
                                y = origin_points[index].y * size + creat_random(-5, 5);
                            }
                            setfillcolor(colors[creat_random(0, 6)]);
                            solidcircle(screen_x(x), screen_y(y), 1);
                        }
                    }
                }
            }
        }
    }
}
 
 
int main()
{
    initgraph(xScreen, yScreen);
    BeginBatchDraw();
    srand(time(0));
    creat_data();
    SetWorkingImage();
    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;
 
}

这里是运行的情况

#include <graphics.h>//这个头文件需要下载一个软件:下面讲解这个软件的安装:、

EasyX Graphics Library for C++

然后出来这个网址:

点继续

点右边下载

可以看到这个

然后打开

点击下一步:

然后上面看你C++编译器的类型和版本,这里我下载了VS2022和VS2019,所以直接安装第二个和三个。

然后重启编译器,就可以看到爱心代码了。

  • 7
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: C语言是一种高级程序设计语言,它可以用来编写各种类型的软件和游戏等。 如果您希望编写一个动态爱心代码,您可以使用C语言中的图形库来实现它。其中,最常用的图形库是OpenGL和SDL(Simple DirectMedia Layer)。 使用OpenGL,您可以创建一个基于2D和3D的图形,你可以创建一个圆和一个矩形然后用代码将它们组合在一起来形成一个心的形状。然后将心的两个半圆形分别分为若干个小圆,在不同的时间和速度下改变这些小圆的颜色和位置,就可以实现动态爱心效果。 而使用SDL,您可以创建一个游戏窗口并给窗口添加一个动态的背景,让爱心(图案)不断地在窗口中移动、旋转和变色。通过鼠标或键盘的交互,您还可以实现一些用户交互的操作。例如,当鼠标悬停在爱心上时,它会变成另一种颜色,或是当点击一个按钮时心会变化,等等。 总之,无论您选择OpenGL还是SDL,只要掌握好C语言图形库的知识,就很容易创建出一个漂亮而充满动感的动态爱心代码。 ### 回答2: 动态爱心代码非常适合在网页、博客和微信等平台上展示,可以为你的页面增添一份温馨和浪漫。下面,我将为大家介绍一下使用c语言编程实现动态爱心代码的方式。 1、创建一个窗口用于展示动态爱心效果,可以使用c语言中的Graphics.h库创建一个窗口; 2、在窗口中绘制出一颗心形图案,可以使用c语言的绘图函数实现,例如lineTo等; 3、使用for循环不停地更新心形位置,使其向上运动,可以使用Sleep函数来控制循环执行的时间间隔; 4、在窗口中添加背景音乐,可以使用c语言中的winmm.h库实现,例如PlaySound()函数。 5、当点击窗口后,心形图案会缓慢渐变消失,并播放一段祝福语音。 下面是一个示例代码,可供参考: ``` #include<graphics.h> #include<mmsystem.h> #pragma comment(lib,"winmm.lib") void Heart() { for (int y = -20; y <= 70; y++) {//Heart loop for (int x = -30; x <= 30; x++) { double distance = sqrt(x * x + y * y); if (distance >= 18 && distance <= 20) putpixel(x + 100, y + 150, RED);//put pixel on graph if (distance >= 10 && distance <= 20) putpixel(x + 120, y + 150, RED);//put pixel on graph } } } void sound() { PlaySound(TEXT("love.wav"), NULL, SND_FILENAME | SND_ASYNC);/*snd_async not block*/ } int main() { initgraph(640, 480); setbkcolor(YELLOW);//set background color Heart();//show heart SOUND sound();//play music for (int t = 0; t < 250; t += 5) {//change color loop setcolor(BLUE);//set color gradually, special loop settextstyle(40, 0, _T("宋体")); outtextxy(200, 200, _T("Happy Valentine's Day"));//print text setcolor(RED);//set color gradually, special loop settextstyle(40, 0, _T("宋体")); outtextxy(200, 200, _T("Happy Valentine's Day"));//specificate heart Sleep(50);//sleep some time } closegraph();// to colse return 0; } ``` 以上就是使用c语言编写动态爱心代码的基本思路和示例代码。希望能对大家有所帮助,祝大家情人节乐! ### 回答3: 在编程中,动态爱心代码是一种非常有趣的代码实现方式,它涉及到多种技术和算法。想要实现一个简单的爱心动态代码,我们可以参照以下步骤: 1. 准备工作。首先,我们需要选择一个开发环境,比如Visual Studio Code或Sublime Text。然后,我们需要先在代码中定义一个HTML元素,用于容纳我们的爱心动画效果。 2. 定义样式。在爱心动画中,我们需要定义一些基本的样式,比如颜色、大小、位置等。为了实现动态,我们需要使用CSS中的animation属性定义动画效果。具体而言,我们可以使用translateX()和translateY()函数控制位置,使用scale()函数控制大小,使用rotate()函数实现旋转等。 3. 实现动态。在爱心动画中,我们需要使用JavaScript代码实现动态效果。我们可以使用setInterval()函数控制动画的频率,使用requestAnimationFrame()函数控制动画的流畅度。具体而言,我们可以通过改变CSS样式来实现动态效果,比如改变translateX()和translateY()的值,实现左右摇晃和上下跳动的效果。 4. 添加交互。如果需要添加交互效果,我们可以使用JavaScript代码监听用户的鼠标点击事件,从而触发动画效果。具体而言,我们可以在鼠标点击事件中改变CSS样式,从而实现不同的动态效果。 总之,通过以上的步骤,我们可以实现一个简单的爱心动态代码。当然,如果我们想要实现更加复杂的效果,还需要积累更多的编程技术和算法知识,不断地尝试和实践。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只贴代码君(yaosicheng)

帅帅的你,留下你的支持吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值