我的代码都是为了能够一次输入一个字母但由于某种原因无法弄清楚如何制作它以便用户可以输入最多三个字母来绘制乌龟。到目前为止这是我的代码。任何帮助将不胜感激,谢谢你提前
import turtle
velcro = turtle.Turtle()
wn = turtle.Screen()
wn.bgcolor('pink')
velcro.color("purple", "blue")
velcro.color()
('purple', 'blue')
velcro.pensize("12")
def color_purple():
velcro.color ('purple')
def color_blue():
velcro.color('blue')
def color_black():
velcro.color ('black')
velcroColor = turtle.textinput("pick a color", "please chose from the colors purple, blue or black, to draw in")
if (velcroColor == 'purple'):
color_purple()
elif (velcroColor == 'blue'):
color_blue()
elif (velcroColor == 'black'):
color_black()
def letter_A():
velcro.left(90)
velcro.forward(150)
velcro.right(90)
velcro.forward(150)
velcro.right(90)
velcro.forward(150)
velcro.right(180)
velcro.forward(75)
velcro.left(90)
velcro.forward(150)
def letter_B():
velcro.left(90)
velcro.forward(150)
velcro.right(90)
velcro.forward(80)
velcro.right(90)
velcro.forward(80)
velcro.right(90)
velcro.forward(80)
velcro.right(180)
velcro.forward(95)
velcro.right(90)
velcro.forward(80)
velcro.right(90)
velcro.forward(93)
def letter_C():
velcro.right(90)
velcro.forward(150)
velcro.left(90)
velcro.forward(150)
velcro.left(180)
velcro.forward(150)
velcro.right(90)
velcro.forward(200)
velcro.right(90)
velcro.forward(150)
def letter_D():
velcro.left(90)
velcro.forward(200)
velcro.right(98)
velcro.forward(180)
velcro.right(85)
velcro.forward(200)
velcro.right(96)
velcro.forward(172)
def letter_E():
velcro.right(90)
velcro.forward(200)
velcro.left(90)
velcro.forward(155)
velcro.left(180)
velcro.forward(155)
velcro.right(90)
velcro.forward(250)
velcro.right(90)
velcro.forward(155)
velcro.left(180)
velcro.forward(155)
velcro.left(90)
velcro.forward(125)
velcro.left(90)
velcro.forward(150)
def letter_F():
velcro.right(90)
velcro.forward(200)
velcro.right(180)
velcro.forward(250)
velcro.right(90)
velcro.forward(150)
velcro.left(180)
velcro.forward(150)
velcro.left(90)
velcro.forward(95)
velcro.left(90)
velcro.forward(125)
def letter_G():
velcro.left(90)
velcro.forward(200)
velcro.right(90)
velcro.forward(155)
velcro.right(180)
velcro.forward(155)
velcro.left(90)
velcro.forward(225)
velcro.left(90)
velcro.forward(175)
velcro.left(90)
velcro.forward(80)
velcro.left(90)
velcro.forward(80)
def letter_H():
velcro.forward(150)
velcro.right(90)
velcro.forward(175)
velcro.right(180)
velcro.forward(300)
velcro.left(180)
velcro.forward(125)
velcro.right(90)
velcro.forward(150)
velcro.right(90)
velcro.forward(125)
velcro.right(180)
velcro.forward(300)
def letter_I():
velcro.left(90)
velcro.forward(150)
velcro.left(90)
velcro.forward(100)
velcro.right(180)
velcro.forward(200)
velcro.right(180)
velcro.forward(100)
velcro.left(90)
velcro.forward(250)
velcro.right(90)
velcro.forward(100)
velcro.right(180)
velcro.forward(200)
def letter_J():
velcro.left(90)
velcro.forward(150)
velcro.left(90)
velcro.forward(100)
velcro.left(180)
velcro.forward(200)
velcro.left(180)
velcro.forward(100)
velcro.left(90)
velcro.forward(250)
velcro.right(90)
velcro.forward(100)
velcro.right(90)
velcro.forward(95)
def letter_K():
velcro.left(90)
velcro.forward(200)
velcro.left(180)
velcro.forward(100)
velcro.left(145)
velcro.forward(100)
velcro.left(180)
velcro.forward(100)
velcro.left(85)
velcro.forward(150)
def letter_L():
velcro.left(90)
velcro.forward(250)
velcro.left(180)
velcro.forward(300)
velcro.left(90)
velcro.forward(200)
def letter_M():
velcro.left(90)
velcro.forward(250)
velcro.right(140)
velcro.forward(200)
velcro.left(100)
velcro.forward(200)
velcro.right(140)
velcro.forward(250)
def letter_N():
velcro.left(90)
velcro.forward(200)
velcro.right(140)
velcro.forward(250)
velcro.left(140)
velcro.forward(200)
def letter_O():
velcro.left(180)
velcro.forward(100)
velcro.right(90)
velcro.forward(250)
velcro.right(90)
velcro.forward(100)
velcro.right(90)
velcro.forward(250)
def letter_P():
velcro.left(90)
velcro.forward(250)
velcro.right(90)
velcro.forward(95)
velcro.right(90)
velcro.forward(95)
velcro.right(90)
velcro.forward(95)
def letter_Q():
velcro.left(90)
velcro.forward(250)
velcro.left(90)
velcro.forward(150)
velcro.left(90)
velcro.forward(250)
velcro.left(90)
velcro.forward(150)
velcro.left(140)
velcro.forward(50)
velcro.left(180)
velcro.forward(100)
def letter_R():
velcro.left(90)
velcro.forward(250)
velcro.right(90)
velcro.forward(100)
velcro.right(90)
velcro.forward(115)
velcro.right(90)
velcro.forward(100)
velcro.left(135)
velcro.forward(180)
def letter_S():
velcro.right(90)
velcro.forward(150)
velcro.right(90)
velcro.forward(150)
velcro.left(180)
velcro.forward(150)
velcro.left(90)
velcro.forward(150)
velcro.left(90)
velcro.forward(150)
velcro.right(90)
velcro.forward(150)
velcro.right(90)
velcro.forward(150)
def letter_T():
velcro.left(90)
velcro.forward(150)
velcro.left(90)
velcro.forward(50)
velcro.right(180)
velcro.forward(100)
def letter_U():
velcro.left(90)
velcro.forward(200)
velcro.left(180)
velcro.forward(200)
velcro.right(90)
velcro.forward(150)
velcro.right(90)
velcro.forward(200)
def letter_V():
velcro.left(115)
velcro.forward(250)
velcro.left(180)
velcro.forward(250)
velcro.left(120)
velcro.forward(250)
def letter_W():
velcro.left(270)
velcro.forward(275)
velcro.right(140)
velcro.forward(200)
velcro.left(100)
velcro.forward(200)
velcro.right(140)
velcro.forward(275)
def letter_X():
velcro.left(140)
velcro.forward(300)
velcro.left(180)
velcro.forward(150)
velcro.right(270)
velcro.forward(150)
velcro.right(180)
velcro.forward(300)
def letter_Y():
velcro.left(140)
velcro.forward(200)
velcro.left(180)
velcro.forward(200)
velcro.right(275)
velcro.forward(200)
velcro.left(180)
velcro.forward(200)
velcro.left(45)
velcro.forward(250)
def letter_Z():
velcro.forward(225)
velcro.left(140)
velcro.forward(300)
velcro.right(140)
velcro.forward(225)
velcroLetter = turtle.textinput("Turtle alphabet", "please enter a capital letter from A-Z to draw:")
if (velcroLetter == 'a' or velcroLetter == 'A'):
letter_A()
elif (velcroLetter == 'b' or velcroLetter == 'B'):
letter_B()
elif (velcroLetter == 'c' or velcroLetter == 'C'):
letter_C()
elif (velcroLetter == 'd' or velcroLetter == 'D'):
letter_D()
elif (velcroLetter == 'e' or velcroLetter == 'E'):
letter_E()
elif (velcroLetter == 'f' or velcroLetter == 'F'):
letter_F()
elif (velcroLetter == 'g' or velcroLetter == 'G'):
letter_G()
elif (velcroLetter == 'h' or velcroLetter == 'H'):
letter_H()
elif (velcroLetter == 'i' or velcroLetter == 'I'):
letter_I()
elif (velcroLetter == 'j' or velcroLetter == 'J'):
letter_J()
elif (velcroLetter == 'k' or velcroLetter == 'K'):
letter_K()
elif (velcroLetter == 'l' or velcroLetter == 'L'):
letter_L()
elif (velcroLetter == 'm' or velcroLetter == 'M'):
letter_M()
elif (velcroLetter == 'n' or velcroLetter == 'N'):
letter_N()
elif (velcroLetter == 'o' or velcroLetter == 'O'):
letter_O()
elif (velcroLetter == 'p' or velcroLetter == 'P'):
letter_P()
elif (velcroLetter == 'q' or velcroLetter == 'Q'):
letter_Q()
elif (velcroLetter == 'r' or velcroLetter == 'R'):
letter_R()
elif (velcroLetter == 's' or velcroLetter == 'S'):
letter_S()
elif (velcroLetter == 't' or velcroLetter == 'T'):
letter_T()
elif (velcroLetter == 'u' or velcroLetter == 'U'):
letter_U()
elif (velcroLetter == 'v' or velcroLetter == 'V'):
letter_V()
elif (velcroLetter == 'w' or velcroLetter == 'W'):
letter_W()
elif (velcroLetter == 'x' or velcroLetter == 'X'):
letter_X()
elif (velcroLetter == 'y' or velcroLetter == 'Y'):
letter_Y()
elif (velcroLetter == 'z' or velcroLetter == 'Z'):
letter_Z()
turtle.mainloop()
答案 0 :(得分:0)
由于您的字母相对于原点(0,0)打印,诀窍是将屏幕分成若干部分(比如三分之一),并且对于每个字母,在绘制字母之前移动到相应部分的中心。
我已经重新编写了下面的代码来执行上述操作(最多三个字母)并简化了一些逻辑。字母函数本身没有变化,所以我在下面遗漏了大部分内容,只需将原始代码拼接在那里:
from turtle import Turtle, Screen
SCREEN_WIDTH, SCREEN_HEIGHT = 1200, 600
SCREEN_MARGIN = 0.1 * SCREEN_WIDTH
MAXIMUM_CHARACTERS = 3
CHARACTER_WIDTH = (SCREEN_WIDTH - (2 * SCREEN_MARGIN)) / MAXIMUM_CHARACTERS
COLORS = ['purple', 'blue', 'black']
velcro = Turtle()
screen = Screen()
def letter_A():
velcro.left(90)
velcro.forward(150)
velcro.right(90)
velcro.forward(150)
velcro.right(90)
velcro.forward(150)
velcro.right(180)
velcro.forward(75)
velcro.left(90)
velcro.forward(150)
# ...
def letter_Z():
velcro.forward(225)
velcro.left(140)
velcro.forward(300)
velcro.right(140)
velcro.forward(225)
letter_lookup = {
'A': letter_A,
'B': letter_B,
'C': letter_C,
'D': letter_D,
'E': letter_E,
'F': letter_F,
'G': letter_G,
'H': letter_H,
'I': letter_I,
'J': letter_J,
'K': letter_K,
'L': letter_L,
'M': letter_M,
'N': letter_N,
'O': letter_O,
'P': letter_P,
'Q': letter_Q,
'R': letter_R,
'S': letter_S,
'T': letter_T,
'U': letter_U,
'V': letter_V,
'W': letter_W,
'X': letter_X,
'Y': letter_Y,
'Z': letter_Z,
}
screen.setup(width=SCREEN_WIDTH, height=SCREEN_HEIGHT)
screen.bgcolor('pink')
velcro.color("purple", "blue")
velcro.pensize("12")
color_string = ", ".join(COLORS[:-1]) + " or " + COLORS[-1]
velcroColor = screen.textinput("Pick a Color", "Please chose from the colors " + color_string + ", to draw with")
if (velcroColor in COLORS):
velcro.color(velcroColor)
velcroLetters = screen.textinput("Turtle alphabet", "Please enter up to 3 letters from A-Z to draw:")[:MAXIMUM_CHARACTERS]
center_x = CHARACTER_WIDTH * (len(velcroLetters) - 1) / -2
for letter in velcroLetters:
velcro.penup()
velcro.home()
velcro.goto(center_x, 0)
velcro.pendown()
letter_lookup[letter.upper()]()
center_x += CHARACTER_WIDTH
screen.mainloop()
答案 1 :(得分:-1)
也许是因为你一次输入多个字母。然后你的程序检查是否e。 G。 “ABC”匹配“A”。尝试输入单个字母。