龟的位置总是显示(0,0)

时间:2016-02-28 09:39:21

标签: python turtle-graphics

我是Python编程的初学者(以及一般的编程),并且只是在Python中使用Turtle模块,并且想知道为什么在下面的程序中使用turtle.pos()的龟的坐标总是给出( 0,0)移动龟后,何时显示在图形界面中?当使用Python shell移动具有相同turtle.forward()的龟时,情况并非如此?

import turtle

wn = turtle.Screen()
wn.bgcolor("lightgreen")

def draw_lines(w, height):
    w.begin_fill()
    w.right(90)
    w.forward(height)
    w.write(turtle.pos())

line = turtle.Turtle()
line.color("blue", "red")
line.pensize(3)

height = [50]

for x in height:
    draw_lines(line, x)

wn.mainloop()

谢谢!

2 个答案:

答案 0 :(得分:1)

该行

w.write(turtle.pos())

应该是

w.write(w.pos())

答案 1 :(得分:0)

你的乌龟作为参数(w)传递给draw_lines:

更正draw_lines,如果在参数中使用更好的名称会更容易:

def draw_lines(my_turtle, height):
    my_turtle.begin_fill()
    my_turtle.right(90)
    my_turtle.forward(height)
    my_turtle.write(my_turtle.pos())