如何同时在乌龟中画字母?

时间:2019-10-31 14:43:28

标签: python turtle-graphics

我写了一个乌龟代码,上面写着我的名字-一个字母一个字母 我想让乌龟同时写字母,但是我不知道怎么写 有人可以帮忙吗?是否可以定义字母,然后将其存储在列表中,然后将列表放入打印功能中,以交替方式绘制字母?
这是代码:

import turtle
beni=turtle.Screen()
beni=turtle.Turtle()
beni.speed(4)
beni.pensize(20)
beni.pencolor('dark turquoise')
beni.penup()
beni.setx(-250)
beni.pendown()

beni.left(90)
beni.forward(200)
beni.right(90)
beni.forward(50)
for i in range (36):
    beni.right(5)
    beni.forward(5)
beni.forward(45)
beni.right(90)
beni.right(90)
beni.forward(50)
for i in range (36):
    beni.right(5)
    beni.forward(5)
beni.forward(45)
beni.right(90)
beni.forward(200)
beni.right(180)
beni.forward(200)

beni.penup()
beni.left(90)
beni.forward(150)
beni.pendown()
beni.left(90)

beni.pencolor('light blue')
beni.forward(220)
beni.right(90)
beni.forward(100)
beni.penup()
beni.left(180)
beni.forward(100)
beni.left(90)
beni.forward(110)
beni.left(90)
beni.pendown()
beni.forward(100)
beni.penup()
beni.left(180)
beni.forward(100)
beni.left(90)
beni.forward(110)
beni.left(90)
beni.pendown()
beni.forward(100)

beni.penup()
beni.forward(50)
beni.pendown()
beni.left(90)

beni.pencolor('plum')
beni.forward(220)
beni.right(90)
beni.right(70)
beni.forward(235)
beni.left(70)
beni.left(90)
beni.forward(220)

beni.penup()
beni.right(180)
beni.forward(220)
beni.left(90)
beni.forward(50)
beni.left(90)
beni.pendown()

beni.pencolor('Pale Green')
beni.forward(220)

2 个答案:

答案 0 :(得分:0)

乌龟的整个想法是,无论它走到哪里,它都会画一条线,因此从本质上讲,它不能在同一时间画出多个东西。不过,您可以为字母定义函数,如下所示:

import turtle

def draw_b(t: Turtle):
    t.left(90)
    t.forward(200)
    t.right(90)
    t.forward(50)
    for i in range (36):
        t.right(5)
        t.forward(5)
    t.forward(45)
    t.right(90)
    t.right(90)
    t.forward(50)
    for i in range (36):
        t.right(5)
        t.forward(5)
    t.forward(45)
    t.right(90)
    t.forward(200)
    t.right(180)
    t.forward(200)

    t.penup()
    t.left(90)
    t.forward(150)
    t.pendown()
    t.left(90)

def draw_e(t: Turtle):
    t.forward(220)
    t.right(90)
    t.forward(100)
    t.penup()
    t.left(180)
    t.forward(100)
    t.left(90)
    t.forward(110)
    t.left(90)
    t.pendown()
    t.forward(100)
    t.penup()
    t.left(180)
    t.forward(100)
    t.left(90)
    t.forward(110)
    t.left(90)
    t.pendown()
    t.forward(100)

    t.penup()
    t.forward(50)
    t.pendown()
    t.left(90)

def draw_n(t:Turtle):
    t.forward(220)
    t.right(90)
    t.right(70)
    t.forward(235)
    t.left(70)
    t.left(90)
    t.forward(220)

    t.penup()
    t.right(180)
    t.forward(220)
    t.left(90)
    t.forward(50)
    t.left(90)
    t.pendown()

def draw_i(t: Turtle):
    t.forward(220)
    t.backward(220)
    t.penup()
    t.right(90)
    t.forward(50)
    t.left(90)
    t.pendown()

turtle.Screen()
beni=turtle.Turtle()
beni.speed(4)
beni.pensize(20)
beni.penup()
beni.setx(-250)
beni.pendown()

beni.pencolor('dark turquoise')
draw_b(beni)
beni.pencolor('light blue')
draw_e(beni)
beni.pencolor('plum')
draw_n(beni)
beni.pencolor('Pale Green')
draw_i(beni)

答案 1 :(得分:0)

  

就其本质而言,它不能以完全相同的方式绘制多于一件的东西   时间

关于乌龟,您应该比说 不能 更了解。粗暴但实用:

from turtle import Screen, Turtle

def draw_b(t):
    t.begin_poly()
    t.forward(220)
    t.right(90)
    t.forward(50)

    for _ in range(36):
        t.right(5)
        t.forward(5)

    t.forward(45)
    t.right(180)
    t.forward(50)

    for _ in range(36):
        t.right(5)
        t.forward(5)

    t.forward(45)
    t.end_poly()

    return t.get_poly()

def draw_e(t):
    t.begin_poly()
    t.forward(220)
    t.right(90)
    t.forward(100)
    t.backward(100)
    t.right(90)
    t.forward(110)
    t.left(90)
    t.forward(100)
    t.backward(100)
    t.right(90)
    t.forward(110)
    t.left(90)
    t.forward(100)
    t.end_poly()

    return t.get_poly()

def draw_n(t):
    t.begin_poly()
    t.forward(220)
    t.right(160)
    t.forward(235)
    t.left(160)
    t.forward(220)
    t.end_poly()

    return t.get_poly()

def draw_i(t):
    t.begin_poly()
    t.forward(220)
    t.backward(220)
    t.end_poly()

    return t.get_poly()

def draw_polygon(turtle, polygon):
    head, *tail = polygon

    turtle.goto(head)
    turtle.pendown()

    if tail:
        screen.ontimer(lambda p=tail: draw_polygon(turtle, p), 75)
    else:
        turtle.hideturtle()

LETTERS = {
    'B': ('dark turquoise', draw_b, 'fastest'),
    'E': ('light blue', draw_e, 'slowest'),
    'I': ('pale green', draw_i, 'fast'),
    'N': ('plum', draw_n, 'slow'),
}

screen = Screen()

for i, letter in enumerate("BENI"):
    color, function, speed = LETTERS[letter]

    turtle = Turtle()
    turtle.hideturtle()
    turtle.speed('fastest')
    turtle.penup()
    turtle.setheading(90)
    turtle.setx(-250 + 150 * i)

    polygon = function(turtle)

    turtle.color(color)
    turtle.pensize(20)
    turtle.speed(speed)
    turtle.showturtle()

    draw_polygon(turtle, polygon)

screen.mainloop()

我们遵循@SamStafford关于将单个字母转换为函数的出色建议。但是,与其让函数打印字母,不如让它们不可见地绘制字母并返回生成的多边形。我们平行走动所有多边形(用ontimer()模拟),最后绘制字母。

为方便起见,我修改了绘图代码,以免抬起笔。这并不是什么大问题,因为大多数笔画都在绘制现有线条。