我想有一个海龟,绘制用户在控制台中输入的内容。例如:如果用户输入"CLOCK"
,我希望海龟在所有大写字母中都画"CLOCK
”。你知道吗
我制作了几乎所有的字母+空格和句点,并用名称"letterA"
、"letterB"
等来定义它们
我已经找了很长时间的方法来做这件事,但我想不出来。你知道吗
我的海龟的名字是"t"
,变量是turtleEnd/Start/MidSpot
。如果用户输入英语字母表+ Æ,Ø,Å
中没有的任何字符,则控制台输出如下内容:
"Invalid character in sentence, please try again"
代码:忽略print
语句,它们只是用于我的日志记录
import turtle as t
turtleEndSpot = t.position()
turtleStartSpot = t.position()
turtleMidSpot = t.position()
#------------------------ALLE BOKSTAVENE OG TEGNENE--------------------
def letterA():
print('\x1b[1;30;41m' + 'STARTER letterA' + '\x1b[0m')
t.pendown()
t.left(78)
t.forward(110)
t.right(156)
t.forward(110)
turtleEndSpot = t.position()
t.back(25)
t.right(102)
t.forward(37)
t.penup()
t.setpos(turtleEndSpot)
t.setheading(0)
t.forward(20)
print('\x1b[0;30;42m' + 'letterA FERDIG!' + '\x1b[0m')
def letterB():
print('\x1b[1;30;41m' + 'STARTER letterB' + '\x1b[0m')
t.pendown()
t.left(90)
t.forward(107.59)
t.right(90)
for i in range(7):
t.forward(14)
t.right(30)
t.setheading(0)
for i in range(7):
t.forward(15)
t.right(30)
t.setheading(0)
t.penup()
t.forward(45)
print('\x1b[0;30;42m' + 'letterB FERDIG!' + '\x1b[0m')
def letterC():
print('\x1b[1;30;41m' + 'STARTER letterC' + '\x1b[0m')
t.penup()
turtleStartSpot = t.position()
t.forward(5)
t.setheading(90)
for i in range(7):
t.forward(14)
t.right(30)
t.setheading(-90)
t.backward(75)
t.pendown()
for i in range(7):
t.backward(14)
t.left(30)
t.setheading(-90)
t.forward(41)
for i in range(7):
t.forward(14)
t.left(30)
t.penup()
t.setpos(turtleStartSpot)
t.setheading(0)
t.forward(75)
print('\x1b[0;30;42m' + 'letterC FERDIG!' + '\x1b[0m')
def letterD():
print('\x1b[1;30;41m' + 'STARTER letterD' + '\x1b[0m')
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)
print('\x1b[0;30;42m' + 'letterD FERDIG!' + '\x1b[0m')
def letterE():
print('\x1b[1;30;41m' + 'STARTER letterE' + '\x1b[0m')
t.pendown()
turtleStartSpot=t.position()
t.setheading(90)
t.forward(54)
turtleMidSpot = t.position()
t.forward(54)
t.setheading(0)
t.forward(40)
t.penup()
t.setpos(turtleMidSpot)
t.pendown()
t.forward(35)
t.penup()
t.setpos(turtleStartSpot)
t.pendown()
t.forward(40)
t.penup()
t.forward(15)
print('\x1b[0;30;42m' + 'letterE FERDIG!' + '\x1b[0m')
def letterF():
print('\x1b[1;30;41m' + 'STARTER letterF' + '\x1b[0m')
t.pendown()
turtleStartSpot=t.position()
t.setheading(90)
t.forward(54)
turtleMidSpot = t.position()
t.forward(54)
t.setheading(0)
t.forward(40)
t.penup()
t.setpos(turtleMidSpot)
t.pendown()
t.forward(35)
t.penup()
t.setpos(turtleStartSpot)
t.forward(40)
t.penup()
t.forward(15)
print('\x1b[0;30;42m' + 'letterF FERDIG!' + '\x1b[0m')
def letterG():
print('\x1b[1;30;41m' + 'STARTER letterG' + '\x1b[0m')
t.penup()
turtleStartSpot = t.position()
t.forward(5)
t.setheading(90)
for i in range(7):
t.forward(14)
t.right(30)
t.setheading(-90)
t.backward(75)
t.pendown()
for i in range(7):
t.backward(14)
t.left(30)
t.setheading(-90)
t.forward(41)
for i in range(7):
t.forward(14)
t.left(30)
t.setheading(180)
t.forward(20)
t.penup()
t.setpos(turtleStartSpot)
t.setheading(0)
t.forward(75)
print('\x1b[0;30;42m' + 'letterG FERDIG!' + '\x1b[0m')
def letterH():
print('\x1b[1;30;41m' + 'STARTER letterH' + '\x1b[0m')
t.pendown()
t.setheading(90)
t.forward(107.59)
t.backward(54)
t.setheading(0)
t.forward(35)
t.setheading(90)
t.forward(54)
t.backward(107.59)
t.setheading(0)
t.penup()
t.forward(20)
print('\x1b[0;30;42m' + 'letterH FERDIG!' + '\x1b[0m')
def letterI():
print('\x1b[1;30;41m' + 'STARTER letterI' + '\x1b[0m')
t.pendown()
t.setheading(90)
t.forward(107.59)
t.backward(107.59)
t.setheading(0)
t.penup()
t.forward(20)
print('\x1b[0;30;42m' + 'letterI FERDIG!' + '\x1b[0m')
def letterJ():
print('\x1b[1;30;41m' + 'STARTER letterJ' + '\x1b[0m')
t.forward(50)
t.setheading(90)
t.forward(107.59)
t.pendown()
t.setheading(-90)
t.forward(74)
turtleMidSpot = t.position()
for i in range(7):
t.forward(14)
t.right(30)
t.penup()
t.setpos(turtleMidSpot)
t.setheading(-90)
t.forward(30)
t.setheading(0)
t.forward(20)
print('\x1b[0;30;42m' + 'letterJ FERDIG!' + '\x1b[0m')
def letterK():
print('\x1b[1;30;41m' + 'STARTER letterK' + '\x1b[0m')
t.setheading(90)
t.pendown()
t.forward(54)
turtleMidSpot=t.position()
t.forward(53)
t.setpos(turtleMidSpot)
t.setheading(57)
t.forward(15)
turtleMidSpot=t.position()
t.forward(45)
t.setpos(turtleMidSpot)
t.setheading(-60)
t.forward(79)
t.setheading(0)
t.penup()
t.forward(15)
print('\x1b[0;30;42m' + 'letterK FERDIG!' + '\x1b[0m')
def letterL():
print('\x1b[1;30;41m' + 'STARTER letterL' + '\x1b[0m')
t.pendown()
turtleStartSpot = t.position()
t.setheading(90)
t.forward(107.59)
t.setpos(turtleStartSpot)
t.setheading(0)
t.forward(30)
t.penup()
t.forward(20)
print('\x1b[0;30;42m' + 'letterL FERDIG!' + '\x1b[0m')
def letterM():
print('\x1b[1;30;41m' + 'STARTER letterM' + '\x1b[0m')
t.pendown()
t.setheading(90)
t.forward(107.59)
t.setheading(-60)
t.forward(50)
t.left(120)
t.forward(50)
t.setheading(-90)
t.forward(107.59)
t.setheading(0)
t.penup()
t.forward(20)
print('\x1b[0;30;42m' + 'letterM FERDIG!' + '\x1b[0m')
def letterN():
print('\x1b[1;30;41m' + 'STARTER letterN' + '\x1b[0m')
t.setheading(90)
t.pendown()
t.forward(107.59)
t.setheading(-70)
t.forward(115)
t.setheading(90)
turtleEndSpot = t.position()
t.forward(107.59)
t.penup()
t.setpos(turtleEndSpot)
t.setheading(0)
t.forward(20)
print('\x1b[0;30;42m' + 'letterL FERDIG!' + '\x1b[0m')
def letterO():
print('\x1b[1;30;41m' + 'STARTER letterO' + '\x1b[0m')
t.penup()
turtleStartSpot = t.position()
t.forward(5)
t.setheading(90)
for i in range(7):
t.forward(14)
t.right(30)
t.setheading(-90)
t.backward(75)
t.pendown()
for i in range(7):
t.backward(14)
t.left(30)
t.setheading(-90)
t.forward(41)
for i in range(7):
t.forward(14)
t.left(30)
t.setheading(90)
t.forward(50)
t.penup()
t.setpos(turtleStartSpot)
t.setheading(0)
t.forward(75)
print('\x1b[0;30;42m' + 'letterO FERDIG!' + '\x1b[0m')
def letterP():
print('\x1b[1;30;41m' + 'STARTER letterP' + '\x1b[0m')
t.pendown()
turtleStartSpot = t.position()
t.left(90)
t.forward(107.59)
t.right(90)
for i in range(7):
t.forward(15)
t.right(30)
t.penup()
t.setpos(turtleStartSpot)
t.setheading(0)
t.forward(40)
print('\x1b[0;30;42m' + 'letterP FERDIG!' + '\x1b[0m')
def letterQ():
print('\x1b[1;30;41m' + 'STARTER letterQ' + '\x1b[0m')
t.penup()
turtleStartSpot = t.position()
t.forward(5)
t.setheading(90)
for i in range(7):
t.forward(14)
t.right(30)
t.setheading(-90)
t.backward(75)
t.pendown()
for i in range(7):
t.backward(14)
t.left(30)
t.setheading(-90)
t.forward(41)
for i in range(7):
t.forward(14)
t.left(30)
t.setheading(90)
t.forward(50)
t.penup()
t.setpos(turtleStartSpot)
t.setheading(0)
t.forward(50)
t.setheading(120)
t.turtleEndSpot = t.position()
t.pendown()
t.forward(30)
t.penup()
t.setpos(turtleEndSpot)
t.setheading(0)
t.forward(20)
print('\x1b[0;30;42m' + 'letterO FERDIG!' + '\x1b[0m')
def letterR():
print('\x1b[1;30;41m' + 'STARTER letterR' + '\x1b[0m')
t.pendown()
t.left(90)
t.forward(107.59)
t.right(90)
for i in range(7):
t.forward(14)
t.right(30)
t.setheading(-58)
t.forward(68)
t.setheading(0)
t.penup()
t.forward(45)
print('\x1b[0;30;42m' + 'letterR FERDIG!' + '\x1b[0m')
def letterS():
print('\x1b[1;30;41m' + 'STARTER letterS' + '\x1b[0m')
t.penup()
turtleStartSpot = t.position()
t.backward(20)
t.setheading(90)
for i in range(7):
t.forward(14)
t.right(30)
t.setheading(-90)
t.backward(75)
t.pendown()
for i in range(8):
t.backward(14)
t.left(30)
t.setheading(-30)
t.forward(40)
t.setheading(-60)
for i in range(8):
t.forward(13)
t.right(30)
t.penup()
t.setpos(turtleStartSpot)
t.setheading(0)
t.forward(60)
print('\x1b[0;30;42m' + 'letterS FERDIG!' + '\x1b[0m')
def letterT():
print('\x1b[1;30;41m' + 'STARTER letterT' + '\x1b[0m')
t.penup()
t.forward(30)
turtleEndSpot = t.position()
t.setheading(90)
t.pendown()
t.forward(107.59)
t.setheading(180)
t.forward(30)
t.backward(60)
t.penup()
t.setpos(turtleEndSpot)
t.setheading(0)
t.forward(60)
print('\x1b[0;30;42m' + 'letterT FERDIG!' + '\x1b[0m')
def letterU():
def specialSpace():
t.penup()
t.setheading(0)
t.forward(40)
def specialPeriod():
t.pendown()
t.dot(5)
t.penup()
specialSpace()
#-----------------------------------------------------------------------
def lettersInTheAlphabet():
letterA()
print(" ")
letterB()
print(" ")
letterC()
print(" ")
letterD()
print(" ")
letterE()
print(" ")
letterF()
print(" ")
letterG()
print(" ")
letterH()
print(" ")
letterI()
print(" ")
letterJ()
print(" ")
letterK()
print(" ")
letterL()
print(" ")
letterM()
print(" ")
letterN()
print(" ")
letterO()
print(" ")
letterP()
print(" ")
letterQ()
print(" ")
letterR()
print(" ")
letterS()
print(" ")
letterT()
print(" ")
print("Alfabet ferdig\n---------------")
def readyCanvas():
t.pensize(5)
t.shape("turtle")
t.speed(1000000000)
t.setpos(-1000,0)
t.color("magenta")
t.forward(3000)
t.setpos(-1000,0)
t.setheading(90)
t.forward(107.59)
t.setheading(0)
t.forward(3000)
t.pu()
t.setpos(-1000,0)
t.color("black")
t.pensize(2)
readyCanvas()
lettersInTheAlphabet()
print(" ")
print('\x1b[5;31;41m' + 'PROGRAM FERDIG!' + '\x1b[0m')
如果已经定义了绘制字母的函数,请使用以下方法:
然后循环用户输入并尝试绘制:
这显然不完整,但它展示了将函数存储在字典中以绘制字母,并在循环输入时检索(并调用)它们的思想。您可能需要考虑如何在字符之间放置空格。你知道吗
相关问题 更多 >
编程相关推荐