I wrote a turtle code which writes my name - letter by letter I want turtle to write the letters at the same time but I dont know how Can someone help? Would it be possible to define the letters and then store them in a list and then put the list in a print function to draw the letter silmutaneously?
here is the code:
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)
The entire idea of a turtle is that it just draws a line wherever it goes, so by its nature it can't draw more than one thing at exactly the same time. You could define functions for your letters, though, like this:
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)
by its nature it can't draw more than one thing at exactly the same time
You should know better than say can't when it comes to turtles. Crude but functional:
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()
We follow @SamStafford's great advice about making individual letters into functions. But rather than have the functions print the letters, we have them draw the letter invisibly and return the resulting polygon. We walk all the polygons in parallel (simulated with ontimer()
) finally drawing the letters.
To facilitate this, I've modified the drawing code not to lift the pen. This wasn't much of an issue as most of the pen up draw was overdrawing existing lines.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.