爱心代码(python)

鉴于好多小伙伴不太会运行,这边补充一下我的运行方式哈~(针对python),不会的小伙伴直接点击 “运行步骤”即可~

运行步骤


一、python代码(love.py)

import time
words = "love"
print('\n')
for item in words.split():
 letterlist = []
 for y in range(12, -12, -1):
  list_X = []
  letters = ''
  for x in range(-30, 30):
   expression = ((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2*(y*0.1)**3
   if expression <= 0:
    letters += item[(x-y) % len(item)]
   else:
    letters += ' '
  list_X.append(letters)
  letterlist += list_X
 print('\n'.join(letterlist))
 time.sleep(0.5);
print('\n                                           to 茂茂酱!!\n\n')
 

运行图: 

二、python代码(love.py)

import turtle as t 

# 布置画布和设置画笔配置
t.setup(width=800, height=500) #窗口(画布)大小
t.color('pink', 'pink') #画笔填充颜色
t.pensize(3) #画笔粗细
t.speed(1.5) #描绘速度

# 开始画
t.begin_fill() #开始填充颜色
t.up() # 提笔
t.goto(0, -100) # 移动画笔到指定坐标
t.down() # 下笔
t.left(45) #笔向向左45度
t.forward(200) #画出长度为200的直线
t.circle(100,180) #绘制直径为100,角度为180度的半圆
t.right(90) #笔向向右90度
t.circle(100,180) #绘制直径为100,角度为180度的半圆
t.forward(200) #画出长度为200的直线
t.end_fill() #结束填充颜色

# 写文字
t.up() #提笔
t.goto(-120, -120) #移动画笔到指定坐标
t.down() #下笔
t.write('祝茂茂',align='center',font=('宋体','20')) #文字可以随意调整,slign是对齐方式,font是字体和字号
t.up() # 提笔
t.goto(120, -120) #移动画笔到指定坐标
t.down() #下笔
t.write('天天开心',align='center',font=('宋体','20'))

 运行图: 

 三、C语言代码(love.c)

#include<stdio.h>
#include<Windows.h>

int main()
{
	float x,y,a;
	for(y=1.5; y>-1.5; y-=0.1)
	{
		for(x=-1.5; x<1.5; x+=0.05)
		{
			a=x*x+y*y-1;
			putchar(a*a*a-x*x*y*y*y<=0.0?'*':' ');
		}
		system("color 0c");
		putchar('\n');
	}
	printf("\t\t\t\t\t茂茂天天开心~\n\n"); 
	return 0;
}

2023.11.30更新~  又看到一个爱心代码,不过是html格式的,复制到记事本上,改下后缀为html后双击即可运行~   例:更改为love.html,运行效果如下,动态的o~

<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <title>粒子爱心</title>
    <style>
        canvas {
            position: absolute;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, .1);
        }

        #child {
            position: fixed;
            top: 52%;
            left: 50%;
            margin-top: -75px;
            margin-left: -100px;
            z-index: 100;

        }

        h4 {
            font-family: "STKaiti";
            font-size: 40px;
            color: #f584b7;
            position: relative;
            top: -70px;
            left: -65px;
        }
    </style>
</head>

<body>
    <div id="child">

    </div>
    <canvas id="heart" width="1920" height="947"></canvas>
    <script>
        window.requestAnimationFrame =
            window.__requestAnimationFrame ||
            window.requestAnimationFrame ||
            window.webkitRequestAnimationFrame ||
            window.mozRequestAnimationFrame ||
            window.oRequestAnimationFrame ||
            window.msRequestAnimationFrame ||
            (function () {
                return function (callback, element) {
                    var lastTime = element.__lastTime;
                    if (lastTime === undefined) {
                        lastTime = 0;
                    }
                    var currTime = Date.now();
                    var timeToCall = Math.max(1, 33 - (currTime - lastTime));
                    window.setTimeout(callback, timeToCall);
                    element.__lastTime = currTime + timeToCall;
                };
            })();
        window.isDevice = (/android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(((navigator.userAgent || navigator.vendor || window.opera)).toLowerCase()));
        var loaded = false;
        var init = function () {
            if (loaded) return;
            loaded = true;
            var mobile = window.isDevice;
            var koef = mobile ? 0.5 : 1;
            var canvas = document.getElementById('heart');
            var ctx = canvas.getContext('2d');
            var width = canvas.width = koef * innerWidth;
            var height = canvas.height = koef * innerHeight;
            var rand = Math.random;
            ctx.fillStyle = "rgba(0,0,0,1)";
            ctx.fillRect(0, 0, width, height);

            var heartPosition = function (rad) {
                return [Math.pow(Math.sin(rad), 3), -(15 * Math.cos(rad) - 5 * Math.cos(2 * rad) - 2 * Math.cos(3 * rad) - Math.cos(4 * rad))];
            };
            var scaleAndTranslate = function (pos, sx, sy, dx, dy) {
                return [dx + pos[0] * sx, dy + pos[1] * sy];
            };

            window.addEventListener('resize', function () {
                width = canvas.width = koef * innerWidth;
                height = canvas.height = koef * innerHeight;
                ctx.fillStyle = "rgba(0,0,0,1)";
                ctx.fillRect(0, 0, width, height);
            });

            var traceCount = mobile ? 20 : 50;
            var pointsOrigin = [];
            var i;
            var dr = mobile ? 0.3 : 0.1;
            for (i = 0; i < Math.PI * 2; i += dr) pointsOrigin.push(scaleAndTranslate(heartPosition(i), 210, 13, 0, 0));
            for (i = 0; i < Math.PI * 2; i += dr) pointsOrigin.push(scaleAndTranslate(heartPosition(i), 150, 9, 0, 0));
            for (i = 0; i < Math.PI * 2; i += dr) pointsOrigin.push(scaleAndTranslate(heartPosition(i), 90, 5, 0, 0));
            var heartPointsCount = pointsOrigin.length;

            var targetPoints = [];
            var pulse = function (kx, ky) {
                for (i = 0; i < pointsOrigin.length; i++) {
                    targetPoints[i] = [];
                    targetPoints[i][0] = kx * pointsOrigin[i][0] + width / 2;
                    targetPoints[i][1] = ky * pointsOrigin[i][1] + height / 2;
                }
            };

            var e = [];
            for (i = 0; i < heartPointsCount; i++) {
                var x = rand() * width;
                var y = rand() * height;
                e[i] = {
                    vx: 0,
                    vy: 0,
                    R: 2,
                    speed: rand() + 5,
                    q: ~~(rand() * heartPointsCount),
                    D: 2 * (i % 2) - 1,
                    force: 0.2 * rand() + 0.7,
                    f: "hsla(0," + ~~(40 * rand() + 60) + "%," + ~~(60 * rand() + 20) + "%,.3)",
                    trace: []
                };
                for (var k = 0; k < traceCount; k++) e[i].trace[k] = { x: x, y: y };
            }

            var config = {
                traceK: 0.4,
                timeDelta: 0.01
            };

            var time = 0;
            var loop = function () {
                var n = -Math.cos(time);
                pulse((1 + n) * .5, (1 + n) * .5);
                time += ((Math.sin(time)) < 0 ? 9 : (n > 0.8) ? .2 : 1) * config.timeDelta;
                ctx.fillStyle = "rgba(0,0,0,.1)";
                ctx.fillRect(0, 0, width, height);
                for (i = e.length; i--;) {
                    var u = e[i];
                    var q = targetPoints[u.q];
                    var dx = u.trace[0].x - q[0];
                    var dy = u.trace[0].y - q[1];
                    var length = Math.sqrt(dx * dx + dy * dy);
                    if (10 > length) {
                        if (0.95 < rand()) {
                            u.q = ~~(rand() * heartPointsCount);
                        } else {
                            if (0.99 < rand()) {
                                u.D *= -1;
                            }
                            u.q += u.D;
                            u.q %= heartPointsCount;
                            if (0 > u.q) {
                                u.q += heartPointsCount;
                            }
                        }
                    }
                    u.vx += -dx / length * u.speed;
                    u.vy += -dy / length * u.speed;
                    u.trace[0].x += u.vx;
                    u.trace[0].y += u.vy;
                    u.vx *= u.force;
                    u.vy *= u.force;
                    for (k = 0; k < u.trace.length - 1;) {
                        var T = u.trace[k];
                        var N = u.trace[++k];
                        N.x -= config.traceK * (N.x - T.x);
                        N.y -= config.traceK * (N.y - T.y);
                    }
                    ctx.fillStyle = u.f;
                    for (k = 0; k < u.trace.length; k++) {
                        ctx.fillRect(u.trace[k].x, u.trace[k].y, 1, 1);
                    }
                }
                ctx.fillStyle = "rgba(255,255,255,1)";
                for (i = u.trace.length + 13; i--;) ctx.fillRect(targetPoints[i][0], targetPoints[i][1], 2, 2);

                window.requestAnimationFrame(loop, canvas);
            };
            loop();
        };

        var s = document.readyState;
        if (s === 'complete' || s === 'loaded' || s === 'interactive') init();
        else document.addEventListener('DOMContentLoaded', init, false);
    </script>

</body>

</html>

 

运行图: 


运行步骤:

鉴于好多小伙伴不太会运行,这边补充一下我的运行方式哈~(针对python)

第一步:随便在一个地方创建文件夹(最好是桌面,待会删删也方便~)

 第二步:打开这个Test文件夹,进入,创建一个后缀为 .py 的文件,名字随便取

//不会吧不会吧,想学编程的如果电脑不能直接改后缀,那就不够称职哦~(叉走,百度去)

 第三步:在导航那边输入 cmd 并按enter(快捷终端方式)

 

 第四步:输入 python+空格+你创建的python文件全名,示例如下:

python love.py

 按enter 运行

 纯纯保姆级别~~~~~

评论 27
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值