我想使用 Turtle 模块创建 Python LOGO

I want to create Python LOGO using Turtle Module

我想制作Python LOGO。所以我将 Turtle 模块导入到我的代码中。我的问题是它只创建了一半 Python LOGO 然后抛出错误。我该如何解决?

Python 徽标使用 Python 乌龟 |酷 Python 龟图形 | Python龟编码|编码

我正在尝试使用 turtle 模块创建一个 PYTHON 徽标。但是,我一直卡在这个上面,不知道如何进行。

代码开始

from turtle import *


speed(100)
#blue part
pencolor('#4584b6')
fillcolor('#4584b6')
begin_fill()
penup()
goto(-70,20)
left(180)
pendown()
forward(10)

def curve():
    for i in range(50):
        forward(0.5)
        right(1)
    for i in range(80):
        forward(2)
        right(1)
    for i in range(50):
        forward(0.5)
        right(1)

curve()
def line():
    forward(130)
    left(90)
    forward(10)
    left(90)
    forward(90)
    right(90)
    forward(30)
line()
curve()

forward(80)
for i in range(90):
    forward(0.5)
    right(1)
forward(120)
for i in range(90):
    forward(0.5)
    left(1)

forward(72.7)
right(90)
right(1)
forward(19)
end_fill()

penup()
goto(160,186)
right(180)
pendown()

#yellow part
pencolor('ffde57')
fillcolor('ffde57')
begin_fill()
forward(10)

curve()
line()
curve()

forward(80)
for i in range(90):
    forward(0.5)
    right(1)
forward(120)
for i in range(90):
    forward(0.5)
    left(1)

forward(72.7)
right(90)
right(1)
forward(19)
end_fill()
penup()
goto(-20,210)
pendown()

#circledots
pencolor('white')
fillcolor('white')
begin_fill()
circle(10)
end_fill()
pencolor('blue')
penup()
goto(110,-30)
pendown()
pencolor('white')
fillcolor('white')
begin_fill()
circle(10)
end_fill()
hideturtle()
done()

关于学习turtle模块的教程有很多。但是如果你只是想让代码绘制python logo。在这里。

import turtle

t = turtle.Turtle()
s = turtle.Screen()
s.bgcolor("black")
t.speed(10)
t.pensize(2)
t.pencolor("white")



def s_curve():
    for i in range(90):
        t.left(1)
        t.forward(1)

def r_curve():
    for i in range(90):
        t.right(1)
        t.forward(1)

def l_curve():
    s_curve()
    t.forward(80)
    s_curve()

def l_curve1():
    s_curve()
    t.forward(90)
    s_curve()

def half():
    t.forward(50)
    s_curve()
    t.forward(90)
    l_curve()
    t.forward(40)
    t.left(90)
    t.forward(80)
    t.right(90)
    t.forward(10)
    t.right(90)
    t.forward(120) #on test
    l_curve1()
    t.forward(30)
    t.left(90)
    t.forward(50)
    r_curve()
    t.forward(40)
    t.end_fill()

def get_pos():
    t.penup()
    t.forward(20)
    t.right(90)
    t.forward(10)
    t.right(90)
    t.pendown()

def eye():
    t.penup()
    t.right(90)
    t.forward(160)
    t.left(90)
    t.forward(70)
    t.pencolor("black")
    t.dot(35)

def sec_dot():
    t.left(90)
    t.penup()
    t.forward(310)
    t.left(90)
    t.forward(120)
    t.pendown()

    t.dot(35)




t.fillcolor("#306998")
t.begin_fill()
half()
t.end_fill()
get_pos()
t.fillcolor("#FFD43B")
t.begin_fill()
half()
t.end_fill()

eye()
sec_dot()



def pause():
    t.speed(2)
    for i in range(100):
        t.left(90)
pause()

输出

利用 turtle 的方法,我们可以用更少的代码得出 Python 徽标的近似值:

from turtle import Screen, Turtle

def curved_box(t, sides):
    for _ in range(sides):
        t.circle(90, extent=90)
        t.forward(120)

    t.circle(90, extent=90)

def snake(t, color):
    t.backward(16)
    t.left(90)
    t.forward(16)
    t.right(90)

    t.fillcolor(color)

    t.begin_fill()
    t.forward(64)
    curved_box(t, 2)
    t.forward(44)
    t.left(90)
    t.forward(152)
    t.right(90)
    t.forward(16)
    t.right(90)
    t.forward(204)
    curved_box(t, 1)
    t.forward(44)
    t.left(90)
    t.forward(60)
    t.circle(-90, extent=90)
    t.forward(64)
    t.end_fill()

    t.backward(86)
    t.left(90)
    t.forward(224)
    t.dot(48, 'white')
    t.backward(224)
    t.right(90)
    t.forward(86)

screen = Screen()

turtle = Turtle()
turtle.hideturtle()
turtle.speed('fastest')
turtle.penup()

snake(turtle, '#3774A8')
turtle.left(180)
snake(turtle, '#F6D646')

screen.exitonclick()

但这仍然只是一个近似值: