为什么我的乌龟不画我的赛车道?
因此,我正在进行一场乌龟比赛,但是我想通过使用DEF使我的代码短。我以前从未使用过,但是我查找了示例,我不知道我的代码缺少什么,因为它不起作用。我基本上想画很多“ |”但是,在列之间有15行和5列,在列之间有20个“空间”,行之间的35个“空格”。我没有任何错误,窗户打开了,但没有任何绘制。这是我的代码:
import turtle
lijn = turtle.Turtle()
lijn.hideturtle()
lijn.color("black")
lijn.speed(0)
fontLines = ("Arial", 16, "normal")
def line(x,y,width,text):
lijn.penup()
lijn.goto(x,y)
for i in range (15):
lijn.write(text, font=fontLines)
lijn.forward(width)
def raceBaan():
line(-150,60,20,"|")
line(-150,25,20,"|")
line(-150,-10,20,"|")
line(-150,-45,20,"|")
line(-150,-80,20,"|")
turtle.done()
So I'm making a turtle race, but I wanted to make my code shorter by using def. I haven't used it before but I looked up examples and I have no idea what my code is missing because it doesn't work. I basically want to draw a lot of "|" but with 15 rows and 5 columns with 20 'spaces' in between the columns and 35 'spaces' between the rows. I don't get any errors, the windows opens but it doesn't draw anything. This is my code:
import turtle
lijn = turtle.Turtle()
lijn.hideturtle()
lijn.color("black")
lijn.speed(0)
fontLines = ("Arial", 16, "normal")
def line(x,y,width,text):
lijn.penup()
lijn.goto(x,y)
for i in range (15):
lijn.write(text, font=fontLines)
lijn.forward(width)
def raceBaan():
line(-150,60,20,"|")
line(-150,25,20,"|")
line(-150,-10,20,"|")
line(-150,-45,20,"|")
line(-150,-80,20,"|")
turtle.done()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您只需要致电RaceBaan()并将其放在代码末尾,但在turtle.done()之前,因为您总是必须调用函数。同样,括号是用于参数的,以防您流浪:)
yeah you just need to call raceBaan() and put that at the end of the code but before turtle.done() because you always have to call functions. Also the brackets are for parameters in case you're wandering :)