运行软件:pycharm
画一个爱心的代码:
import turtle
def print_love1():
# 设置画笔颜色和粗细
turtle.pencolor("red")
turtle.pensize(6)
# 绘制爱心
turtle.fillcolor("pink")
turtle.begin_fill()
turtle.left(45)
turtle.forward(100)
turtle.circle(50, 180)
turtle.right(90)
turtle.circle(50, 180)
turtle.forward(100)
turtle.end_fill()
# 隐藏画笔
turtle.hideturtle()
# 显示结果
turtle.done()
if __name__ == '__main__':
print_love1()
循环画一个爱心的代码:
import turtle
import time
def print_love():
# 设置画笔颜色和粗细
turtle.pencolor("red") # 或者使用 (135/255, 206/255, 235/255)
turtle.pensize(6)
# 绘制爱心
turtle.fillcolor("pink")
turtle.begin_fill()
turtle.left(45)
turtle.forward(100)
turtle.circle(50, 180)
turtle.right(90)
turtle.circle(50, 180)
turtle.forward(100)
turtle.end_fill()
if __name__ == '__main__':
turtle.speed(1) # 设置绘制速度
for _ in range(10): # 循环绘制10个爱心
print_love()
time.sleep(1) # 等待1秒以便观察
turtle.clear() # 清空画布
turtle.penup() # 抬起画笔
turtle.goto(0, 0) # 回到原点
turtle.setheading(0) # 重置方向
turtle.pendown() # 放下画笔
turtle.hideturtle() # 隐藏画笔
turtle.done() # 显示结果
效果图: