Draw Letters In Turtle

别来无恙 提交于 2019-12-11 06:19:35

问题


I just started learning turtle in python. I am tried to write my name "Idan" (capital letters), but I am stuck on "D".

This is my code so far:

import turtle

t = turtle.Turtle()

t.up()
t.backward(400)
t.left(90)
t.down()
t.forward(250)
t.right(90)
t.forward(62.5)
t.backward(62.5 * 2)
t.forward(62.5)
t.right(90)
t.forward(250)
t.right(90)
t.forward(62.5)
t.backward(62.5 * 2)
t.up()
t.backward(100)
t.right(90)
t.down()
t.forward(250)
for x in range(180):
    t.forward(1)
    t.right(1)

But when I run it it gives me a small half circle.

Is there a way to make it do a half circle that will create a "D"


回答1:


The main issue you have is that your half-circle starts when the turtle is facing up, rather than to the right. Add a t.right(90) call just before the for loop and you'll be closer to what you want (you'll get something like a P).

After that, you'll need to adjust the forward calls inside the loop to make the circle the size you want (or as martineau suggested in a comment, use t.circle instead of your own loop). You might need to do some calculations to figure out the right distances (e.g. multiply your desired diameter by math.pi to get a circumference, then divide by the number of segments you're using to construct it).




回答2:


Not perfect, made by try and error, but it writes the name:

import turtle
turtle.setup(900,400)
wn = turtle.Screen()
wn.title("Turtle writing my name: IDAN")
wn.bgcolor("lightgreen")
turtle.width(30)
# turtle.pensize(30)

t = turtle.Turtle()
t.up()
t.backward(360)
t.left(90)
t.backward(80)
t.down()
t.forward(250)
t.right(90)
t.forward(62.5)
t.backward(62.5 * 2)
t.forward(62.5)
t.right(90)
t.forward(250)
t.right(90)
t.forward(62.5)
t.backward(62.5 * 2)
t.up()
t.backward(100)
t.right(90)
t.down()
t.forward(250)
t.right(90)
for x in range(180):

. t.forward(2.2) # adjust HERE to get different size of circle

    t.right(1)
t.up()
t.right(90)
t.forward(230)
t.right(90)
t.forward(4*62.5)
t.right(65)
t.down()
t.forward(250)
t.right(180)
t.forward(275)
t.left(135)
t.forward(250)
t.right(180)
t.forward(120)
t.right(70)
t.forward(110)
t.right(90)
t.up()
t.forward(115)
t.left(90)
t.forward(1.5*62.5)
t.left(90)
t.down()
t.forward(250)
t.right(145)
t.forward(320)
t.left(145)
t.forward(250)

wn.mainloop()



回答3:


I would suggest the following in solving a problem like this:

  • Plan out your block font carefully ahead, on paper if needed. The simpler your design, the easier it will be to code.

  • Separate your letters logically in your code -- if not into functions at this point, at least into separate commented blocks.

  • Have each letter start with the turtle pointing the same direction and adjust as necessary, don't count on the resulting state of the previous letter.

  • Use a virtual coordinate system to simplify your drawing logic and allow the letters to appear in different size windows. If not with a function like setworldcoordinates() then simply via multipliers for the height and width.

  • Assume in your design that you'll want to add more letters later.

  • Consider what would happen if you wanted to spell "DIAN" instead -- how much code would you have to change and how can you keep this to a minimum.

My example below uses features you've probably not learned yet and is not meant as a solution for you to use -- it's meant to illustrate the above ideas. I.e. try changing the window size, the border size, the width and height, rearrange the leters, etc. and see how the results change but still work:

from turtle import Turtle, Screen

NAME = "IDAN"

BORDER = 1
BOX_WIDTH, BOX_HEIGHT = 6, 10  # letter bounding box
WIDTH, HEIGHT = BOX_WIDTH - BORDER * 2, BOX_HEIGHT - BORDER * 2  # letter size

def letter_A(turtle):
    turtle.forward(HEIGHT / 2)
    for _ in range(3):
        turtle.forward(HEIGHT / 2)
        turtle.right(90)
        turtle.forward(WIDTH)
        turtle.right(90)
    turtle.forward(HEIGHT)

def letter_D(turtle):
    turtle.forward(HEIGHT)
    turtle.right(90)
    turtle.circle(-HEIGHT / 2, 180, 30)

def letter_I(turtle):
    turtle.right(90)
    turtle.forward(WIDTH)
    turtle.backward(WIDTH / 2)
    turtle.left(90)
    turtle.forward(HEIGHT)
    turtle.right(90)
    turtle.backward(WIDTH / 2)
    turtle.forward(WIDTH)

def letter_N(turtle):
    turtle.forward(HEIGHT)
    turtle.goto(turtle.xcor() + WIDTH, BORDER)
    turtle.forward(HEIGHT)

LETTERS = {'A': letter_A, 'D': letter_D, 'I': letter_I, 'N': letter_N}

wn = Screen()
wn.setup(800, 400)  # arbitrary
wn.title("Turtle writing my name: {}".format(NAME))
wn.setworldcoordinates(0, 0, BOX_WIDTH * len(NAME), BOX_HEIGHT)

marker = Turtle()

for counter, letter in enumerate(NAME):
    marker.penup()
    marker.goto(counter * BOX_WIDTH + BORDER, BORDER)
    marker.setheading(90)

    if letter in LETTERS:
        marker.pendown()
        LETTERS[letter](marker)

marker.hideturtle()

wn.mainloop()

OUTPUT




回答4:


You can use this code instead:`

import turtle
turtle = turtle. Turtle()
turtle.write("IDAN" ,font=("Arial",32,"normal")



回答5:


This might work: It works for me

import turtle as t    

def letterD():
    t.pendown()
    t.setheading(90)
    t.forward(108)
    t.setheading(0)
    for i in range(4):
        t.forward(20)
        t.right(30)
    t.setheading(-90)
    t.forward(13)
    for i in range(4):
        t.forward(20)
        t.right(30)
    t.setheading(0)
    t.penup()
    t.forward(65)



回答6:


Example:

import turtle

t = turtle.Pen()
t.circle(50, 180)

# this draws a 180 degree, 50 radius circle


来源:https://stackoverflow.com/questions/43689387/draw-letters-in-turtle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!