为什么我的乌龟不画我的赛车道?

发布于 2025-01-29 04:45:59 字数 638 浏览 2 评论 0原文

因此,我正在进行一场乌龟比赛,但是我想通过使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云朵有点甜 2025-02-05 04:45:59

是的,您只需要致电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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文