当前位置:   article > 正文

爱心代码(python)_python爱心代码

python爱心代码

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

运行步骤


一、python代码(love.py)

  1. import time
  2. words = "love"
  3. print('\n')
  4. for item in words.split():
  5. letterlist = []
  6. for y in range(12, -12, -1):
  7. list_X = []
  8. letters = ''
  9. for x in range(-30, 30):
  10. expression = ((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2*(y*0.1)**3
  11. if expression <= 0:
  12. letters += item[(x-y) % len(item)]
  13. else:
  14. letters += ' '
  15. list_X.append(letters)
  16. letterlist += list_X
  17. print('\n'.join(letterlist))
  18. time.sleep(0.5);
  19. print('\n to 茂茂酱!!\n\n')

运行图: 

二、python代码(love.py)

  1. import turtle as t
  2. # 布置画布和设置画笔配置
  3. t.setup(width=800, height=500) #窗口(画布)大小
  4. t.color('pink', 'pink') #画笔填充颜色
  5. t.pensize(3) #画笔粗细
  6. t.speed(1.5) #描绘速度
  7. # 开始画
  8. t.begin_fill() #开始填充颜色
  9. t.up() # 提笔
  10. t.goto(0, -100) # 移动画笔到指定坐标
  11. t.down() # 下笔
  12. t.left(45) #笔向向左45
  13. t.forward(200) #画出长度为200的直线
  14. t.circle(100,180) #绘制直径为100,角度为180度的半圆
  15. t.right(90) #笔向向右90
  16. t.circle(100,180) #绘制直径为100,角度为180度的半圆
  17. t.forward(200) #画出长度为200的直线
  18. t.end_fill() #结束填充颜色
  19. # 写文字
  20. t.up() #提笔
  21. t.goto(-120, -120) #移动画笔到指定坐标
  22. t.down() #下笔
  23. t.write('祝茂茂',align='center',font=('宋体','20')) #文字可以随意调整,slign是对齐方式,font是字体和字号
  24. t.up() # 提笔
  25. t.goto(120, -120) #移动画笔到指定坐标
  26. t.down() #下笔
  27. t.write('天天开心',align='center',font=('宋体','20'))

 运行图: 

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

  1. #include<stdio.h>
  2. #include<Windows.h>
  3. int main()
  4. {
  5. float x,y,a;
  6. for(y=1.5; y>-1.5; y-=0.1)
  7. {
  8. for(x=-1.5; x<1.5; x+=0.05)
  9. {
  10. a=x*x+y*y-1;
  11. putchar(a*a*a-x*x*y*y*y<=0.0?'*':' ');
  12. }
  13. system("color 0c");
  14. putchar('\n');
  15. }
  16. printf("\t\t\t\t\t茂茂天天开心~\n\n");
  17. return 0;
  18. }

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

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>粒子爱心</title>
  6. <style>
  7. canvas {
  8. position: absolute;
  9. left: 0;
  10. top: 0;
  11. width: 100%;
  12. height: 100%;
  13. background-color: rgba(0, 0, 0, .1);
  14. }
  15. #child {
  16. position: fixed;
  17. top: 52%;
  18. left: 50%;
  19. margin-top: -75px;
  20. margin-left: -100px;
  21. z-index: 100;
  22. }
  23. h4 {
  24. font-family: "STKaiti";
  25. font-size: 40px;
  26. color: #f584b7;
  27. position: relative;
  28. top: -70px;
  29. left: -65px;
  30. }
  31. </style>
  32. </head>
  33. <body>
  34. <div id="child">
  35. </div>
  36. <canvas id="heart" width="1920" height="947"></canvas>
  37. <script>
  38. window.requestAnimationFrame =
  39. window.__requestAnimationFrame ||
  40. window.requestAnimationFrame ||
  41. window.webkitRequestAnimationFrame ||
  42. window.mozRequestAnimationFrame ||
  43. window.oRequestAnimationFrame ||
  44. window.msRequestAnimationFrame ||
  45. (function () {
  46. return function (callback, element) {
  47. var lastTime = element.__lastTime;
  48. if (lastTime === undefined) {
  49. lastTime = 0;
  50. }
  51. var currTime = Date.now();
  52. var timeToCall = Math.max(1, 33 - (currTime - lastTime));
  53. window.setTimeout(callback, timeToCall);
  54. element.__lastTime = currTime + timeToCall;
  55. };
  56. })();
  57. window.isDevice = (/android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(((navigator.userAgent || navigator.vendor || window.opera)).toLowerCase()));
  58. var loaded = false;
  59. var init = function () {
  60. if (loaded) return;
  61. loaded = true;
  62. var mobile = window.isDevice;
  63. var koef = mobile ? 0.5 : 1;
  64. var canvas = document.getElementById('heart');
  65. var ctx = canvas.getContext('2d');
  66. var width = canvas.width = koef * innerWidth;
  67. var height = canvas.height = koef * innerHeight;
  68. var rand = Math.random;
  69. ctx.fillStyle = "rgba(0,0,0,1)";
  70. ctx.fillRect(0, 0, width, height);
  71. var heartPosition = function (rad) {
  72. 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))];
  73. };
  74. var scaleAndTranslate = function (pos, sx, sy, dx, dy) {
  75. return [dx + pos[0] * sx, dy + pos[1] * sy];
  76. };
  77. window.addEventListener('resize', function () {
  78. width = canvas.width = koef * innerWidth;
  79. height = canvas.height = koef * innerHeight;
  80. ctx.fillStyle = "rgba(0,0,0,1)";
  81. ctx.fillRect(0, 0, width, height);
  82. });
  83. var traceCount = mobile ? 20 : 50;
  84. var pointsOrigin = [];
  85. var i;
  86. var dr = mobile ? 0.3 : 0.1;
  87. for (i = 0; i < Math.PI * 2; i += dr) pointsOrigin.push(scaleAndTranslate(heartPosition(i), 210, 13, 0, 0));
  88. for (i = 0; i < Math.PI * 2; i += dr) pointsOrigin.push(scaleAndTranslate(heartPosition(i), 150, 9, 0, 0));
  89. for (i = 0; i < Math.PI * 2; i += dr) pointsOrigin.push(scaleAndTranslate(heartPosition(i), 90, 5, 0, 0));
  90. var heartPointsCount = pointsOrigin.length;
  91. var targetPoints = [];
  92. var pulse = function (kx, ky) {
  93. for (i = 0; i < pointsOrigin.length; i++) {
  94. targetPoints[i] = [];
  95. targetPoints[i][0] = kx * pointsOrigin[i][0] + width / 2;
  96. targetPoints[i][1] = ky * pointsOrigin[i][1] + height / 2;
  97. }
  98. };
  99. var e = [];
  100. for (i = 0; i < heartPointsCount; i++) {
  101. var x = rand() * width;
  102. var y = rand() * height;
  103. e[i] = {
  104. vx: 0,
  105. vy: 0,
  106. R: 2,
  107. speed: rand() + 5,
  108. q: ~~(rand() * heartPointsCount),
  109. D: 2 * (i % 2) - 1,
  110. force: 0.2 * rand() + 0.7,
  111. f: "hsla(0," + ~~(40 * rand() + 60) + "%," + ~~(60 * rand() + 20) + "%,.3)",
  112. trace: []
  113. };
  114. for (var k = 0; k < traceCount; k++) e[i].trace[k] = { x: x, y: y };
  115. }
  116. var config = {
  117. traceK: 0.4,
  118. timeDelta: 0.01
  119. };
  120. var time = 0;
  121. var loop = function () {
  122. var n = -Math.cos(time);
  123. pulse((1 + n) * .5, (1 + n) * .5);
  124. time += ((Math.sin(time)) < 0 ? 9 : (n > 0.8) ? .2 : 1) * config.timeDelta;
  125. ctx.fillStyle = "rgba(0,0,0,.1)";
  126. ctx.fillRect(0, 0, width, height);
  127. for (i = e.length; i--;) {
  128. var u = e[i];
  129. var q = targetPoints[u.q];
  130. var dx = u.trace[0].x - q[0];
  131. var dy = u.trace[0].y - q[1];
  132. var length = Math.sqrt(dx * dx + dy * dy);
  133. if (10 > length) {
  134. if (0.95 < rand()) {
  135. u.q = ~~(rand() * heartPointsCount);
  136. } else {
  137. if (0.99 < rand()) {
  138. u.D *= -1;
  139. }
  140. u.q += u.D;
  141. u.q %= heartPointsCount;
  142. if (0 > u.q) {
  143. u.q += heartPointsCount;
  144. }
  145. }
  146. }
  147. u.vx += -dx / length * u.speed;
  148. u.vy += -dy / length * u.speed;
  149. u.trace[0].x += u.vx;
  150. u.trace[0].y += u.vy;
  151. u.vx *= u.force;
  152. u.vy *= u.force;
  153. for (k = 0; k < u.trace.length - 1;) {
  154. var T = u.trace[k];
  155. var N = u.trace[++k];
  156. N.x -= config.traceK * (N.x - T.x);
  157. N.y -= config.traceK * (N.y - T.y);
  158. }
  159. ctx.fillStyle = u.f;
  160. for (k = 0; k < u.trace.length; k++) {
  161. ctx.fillRect(u.trace[k].x, u.trace[k].y, 1, 1);
  162. }
  163. }
  164. ctx.fillStyle = "rgba(255,255,255,1)";
  165. for (i = u.trace.length + 13; i--;) ctx.fillRect(targetPoints[i][0], targetPoints[i][1], 2, 2);
  166. window.requestAnimationFrame(loop, canvas);
  167. };
  168. loop();
  169. };
  170. var s = document.readyState;
  171. if (s === 'complete' || s === 'loaded' || s === 'interactive') init();
  172. else document.addEventListener('DOMContentLoaded', init, false);
  173. </script>
  174. </body>
  175. </html>

 

运行图: 


运行步骤:

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

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

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

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

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

 

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

python love.py

 按enter 运行

 纯纯保姆级别~~~~~

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/171654
推荐阅读
相关标签
  

闽ICP备14008679号