资讯详情

如何用python绘制表白爱心?含参数逻辑

一直觉得turtle这是一个非常可爱的库,突然想试试传说中的土味表白:用python画一点爱。

实现效果如下:

虽然作为一个老式的土味招数,现成的代码确实很多。但是左右看,感受背后的逻辑,比如forward( )的参数、right( )参数怎么来的不是很直白,copy代码很无聊,所以用最简单的想法再推一次:

第一步是从心尖的位置出发,先在左上角画一条直线,与y轴的夹角设置为45度:

第二步,心形上方有一个弧形,可以设计成从原来的斜率开始,每一个单位到一个单位的角度,画一个半圆:

第三步,右侧基本重复,但调整旋转角度,各参数之间的相应关系也很容易推:

OK沿着这个想法,非常简单的代码如下:

import turtle #set a window wn=turtle.Screen() wn.bgcolor("black") wn.title("shape@kaimifami") wn.screensize(800,600)  #set a pen pen=turtle.Turtle() pen.color("pink") pen.shape("turtle")#画笔形状设置了非常可爱的海龟hh pen.fillcolor("pink") turtle.speed(10)  #definefunctions defcurve(): foriinrange(180): pen.right(1) pen.forward(2)  defheart(): pen.left(135) pen.forward(720/3.14) curve() pen.left(90) curve() pen.forward(720/3.14)  deftext(): pen.penup() pen.setpos(-50,50) pen.pendown() pen.color("white") pen.write(input("enterthewords:"),font=("Verdana",30,"bold"))  #painting! pen.begin_fill() pen.penup() pen.setpos(0,-150) pen.pendown() heart() pen.end_fill() text() turtle.done() 

okk!完成!

[1] Draw Heart Using Turtle Graphics in Python - GeeksforGeeks


可直接从Github下载本文code:

Let's draw a heart shape with turtle!??Turtle is a cute and amzing library in python. Let's draw a heart shape with turtle. This will be my very first project here. - GitHub - Liagogo/Draw-a-heart-shape: Turtle is a cute and amzing library in python. Let's draw a heart shape with turtle. This will be my very first project here.https://github.com/Liagogo/Draw-a-heart-shape微信公众号同步内容:

用python画一个爱心情人节快乐!https://mp.weixin.qq.com/s/LvtHLaN31_LEnBhbL5tA1g

标签: 撕裂传感器限位开关zwn

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台