【发布时间】:2021-01-08 05:36:53
【问题描述】:
在这个程序中,我的代码中有十二只海龟(左侧六只,右侧六只)。我创建了两个函数,可以使用户移动左乌龟和右乌龟之一,并为左乌龟和右乌龟指定按键。当我尝试将我的第一只海龟移动到左侧时,当我尝试将第一只海龟移动到左侧时,我收到一条错误消息,提示“TypeError:leftMove() 缺少 1 个必需的位置参数:'lts'”。我在尝试移动右侧的第一只海龟时也收到此错误消息,当我还尝试移动右侧的第一只海龟时说“TypeError:rightMove() 缺少 1 个必需的位置参数:'rts'”。如何解决此错误消息,以便用户可以控制第一左和右海龟的移动?
这是我的代码:
import turtle as trtl
leftTurtles = [] #Stores the turtle from leftTurtleShapes
rightTurtles = [] #Stores the turtle from rightTurtleShapes
leftTurtleShapes = ["triangle", "circle", "arrow", "square", "turtle", "classic"]
rightTurtleShapes = ["classic", "square", "circle", "triangle", "arrow", "turtle"]
leftTurtleColors = ["gold", "silver", "dodgerblue", "greenyellow", "peru", "crimson"] #Color aligns with leftTurtleShapes
rightTurtleColors = ["dodgerblue", "peru", "gold", "crimson", "greenyellow", "silver"] #Color aligns with rightTurtleShapes
for d in leftTurtleShapes: #Makes the turtle of leftTurtleShapes and the color of leftTurtleColors to be aligned with leftTurtleShapes and be stored in leftTurtles
lts = trtl.Turtle(shape=d)
leftTurtles.append(lts)
lts.penup()
leftColor = leftTurtleColors.pop()
lts.fillcolor(leftColor)
lts.goto(-350, 0) #Moves the leftTurtle to the left side of the window
lts.setheading(0)
for s in rightTurtleShapes: #Makes the turtle of rightTurtleShapes and the color of rightTurtleColors to be aligned with rightTurtleShapes and be stored in rightTurtles
rts = trtl.Turtle(shape=s)
rightTurtles.append(rts)
rts.penup()
rightColor = rightTurtleColors.pop()
rts.fillcolor(rightColor)
rts.goto(350, 0) #Moves the rightTurtle to the right side of the window
rts.setheading(180)
def rightMove(rts): #User can control the movement of one of the right turtles
rts.setheading(180)
rts.forward(1)
def leftMove(lts): #User can control the movement of one of the left turtles
lts.setheading(0)
lts.forward(1)
rightMove(rightTurtles[0]) #User controls the first right turtle
leftMove(leftTurtles[0]) #User controls the first left turtle
wn = trtl.Screen()
wn.onkeypress(leftMove, "d") #User uses the letter "d" to control the movement of the left turtle
wn.onkeypress(rightMove, "e") #User uses the letter "e" to control the movement of the right turtle
wn.listen()
wn.mainloop()
【问题讨论】:
标签: python