Python 用乌龟画字母

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43689387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:17:06  来源:igfitidea点击:

Draw Letters In Turtle

pythonpython-2.7turtle-graphics

提问by user7904186

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

我刚开始在 python 中学习乌龟。我试图写我的名字“Idan”(大写字母),但我被困在“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"

有没有办法让它做一个半圆来创建一个“D”

回答by Blckknght

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 forloop and you'll be closer to what you want (you'll get something like a P).

您遇到的主要问题是当乌龟面朝上而不是右侧时,您的半圆就开始了。t.right(90)for循环之前添加一个调用,你会更接近你想要的(你会得到类似 a 的东西P)。

After that, you'll need to adjust the forwardcalls inside the loop to make the circle the size you want (or as martineausuggested in a comment, use t.circleinstead 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.pito get a circumference, then divide by the number of segments you're using to construct it).

之后,您需要调整forward循环内的调用以使圆圈达到您想要的大小(或者按照martineau在评论中的建议,使用t.circle而不是您自己的循环)。您可能需要进行一些计算来找出正确的距离(例如,将所需的直径乘以math.pi得到周长,然后除以用于构造它的段数)。

回答by Claudio

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.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()

回答by cdlane

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.

  • 如果需要,请提前在纸上仔细规划您的块字体。你的设计越简单,编码就越容易。

  • 在你的代码中逻辑地分隔你的字母——如果此时没有分隔成函数,至少分隔成单独的注释块。

  • 让每个字母从乌龟指向相同的方向开始,并根据需要进行调整,不要指望前一个字母的结果状态。

  • 使用虚拟坐标系来简化您的绘图逻辑,并允许字母出现在不同大小的窗口中。如果没有类似的函数,setworldcoordinates()则只需通过高度和宽度的乘数即可。

  • 假设在您的设计中您以后要添加更多字母。

  • 考虑一下如果您想拼写“DIAN”会发生什么——您需要更改多少代码以及如何将其保持在最低限度。

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

输出

enter image description here

在此处输入图片说明

回答by ShaoZhe

You can use this code instead:`

您可以改用此代码:`

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

回答by ent3r_

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)

回答by pranksterbunny

Example:

例子:

import turtle

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

# this draws a 180 degree, 50 radius circle