怎么使用python的turtle画编程狮logo?

steven 2021-10-26 17:32:56 浏览数 (3040)
反馈

在python中有一个很有趣的库,叫做turtle,翻译成中文就是海龟。这个库可以用来画一些有趣的图案。今天小编就带来了一个使用海龟绘图法绘制编程狮logo的代码,并在代码后介绍一些简单的使用心得,感兴趣的小伙伴赶紧copy代码跑起来吧!

import turtle as t

def hexagon(): #画狮子的六边形的脸,并上色
    t.begin_fill()
    t.right(60)
    t.forward(210)
    t.right(60)
    t.forward(210)
    t.right(60)
    t.forward(210)
    t.right(60)
    t.forward(210)  
    t.right(60)
    t.forward(210)
    t.right(60)
    t.forward(210)
    t.end_fill()


def face(): #画狮子的脸部细节(把脸部细节当成一个多边形画)
    t.pensize(2)
    t.color("black","black")
    t.begin_fill()
    t.right(60)
    t.forward(103)
    t.right(120)
    t.forward(105)
    t.left(90)
    t.forward(180)
    t.right(90)
    t.forward(100)
    t.right(90)
    t.forward(180)
    t.left(90)
    t.forward(105)
    t.left(150)
    t.forward(105)
    t.right(60)
    t.forward(140)
    t.left(60)
    t.forward(50)
    t.right(60)
    t.forward(55)
    t.left(90)
    t.forward(45)
    t.left(90)
    t.forward(55)
    t.right(60)
    t.forward(50)
    t.left(60)
    t.forward(140)
    t.right(60)
    t.forward(105)
    t.end_fill()
    

def zongmao(): #画狮子的鬃毛
    t.begin_fill()
    t.forward(210)
    t.left(90)
    t.circle(210,120)
    t.left(30)
    t.forward(210)
    t.left(120)
    t.forward(210)
    t.end_fill()
    t.backward(210)
    t.left(60)


t.penup()
t.goto(60,180)
t.pendown()
t.color("black","#f9e052")
t.pensize(10)
hexagon()
face()
t.penup()
t.goto(60,180)
t.pendown()
t.right(30)
t.color("black","#fe6a00")
t.pensize(10)
for i in range(6):
    zongmao()

心得体会:

    海龟绘图法之所以称为海龟绘图法,就是因为它的笔像一个海龟一样,通过爬行画出内容,只要把自己想象为那只海龟,就知道什么时候左转,什么时候右转,需要转动多少度,需要走多少步了。

    随时注意笔的状态(放下还是提起),这决定了你在移动海龟的时候会不会在画布上留下痕迹。

    有些重复的动作,可以考虑封装成函数来减少代码量,当画的图案比较复杂的时候,不封装的代码量会很恐怖。

    对于初学者而言,很多函数的作用不清楚,可以对照教程进行使用。

1 人点赞