Turtle Graphics:如何向上移动标签?

Turtle Graphics: How to shift label upwards?

我正在学习 Python 并且正在尝试编辑有以下错误的代码:

Each height label is partly covered by the top segment of its bar. Can you modify the drawBar code, moving the label up slightly but not changing the bar? Hint: The label cannot be drawn during the polygon fill sequence.

我已经尝试 def 一个新功能,但是没有用。你能找到 error/edit 吗?

def drawBar(t, height):
    """ Get turtle t to draw one bar, of height. """
    t.begin_fill()               # start filling this shape
    t.left(90)
    t.forward(height)
    t.write(str(height))
    t.right(90)
    t.forward(40)
    t.right(90)
    t.forward(height)
    t.left(90)
    t.end_fill()                 # stop filling this shape

xs = [48, 117, 200, 240, 160, 260, 220]  # here is the data
maxheight = max(xs)
numbars = len(xs)
border = 10

wn = turtle.Screen()             # Set up the window and its attributes
wn.setworldcoordinates(0-border, 0-border, 40*numbars+border, maxheight+border)
wn.bgcolor("lightgreen")

tess = turtle.Turtle()           # create tess and set some attributes
tess.color("blue")
tess.fillcolor("red")
tess.pensize(3)

for a in xs:
    drawBar(tess, a)

wn.exitonclick()

如果你想向上移动,那么举起笔,向前移动,写文字,向后移动,放下宠物。

t.penup()
t.forward(2)
t.write(str(height))
t.forward(-2)
t.pendown() 

最小工作代码

import turtle

def drawBar(t, height):
    """ Get turtle t to draw one bar, of height. """
    t.begin_fill()               # start filling this shape
    t.left(90)
    t.forward(height)
    
    t.penup()
    t.forward(2)
    t.write(str(height))
    t.forward(-2)
    t.pendown()

    t.right(90)
    t.forward(40)
    t.right(90)
    t.forward(height)
    t.left(90)
    t.end_fill()              # stop filling this shape

xs = [48, 117, 200, 240, 160, 260, 220]  # here is the data
maxheight = max(xs)
numbars = len(xs)
border = 10

wn = turtle.Screen()             # Set up the window and its attributes
wn.setworldcoordinates(0-border, 0-border, 40*numbars+border, maxheight+border)
wn.bgcolor("lightgreen")

tess = turtle.Turtle()           # create tess and set some attributes
tess.color("blue")
tess.fillcolor("red")
tess.pensize(3)

for a in xs:
    drawBar(tess, a)

wn.exitonclick()

编辑:

如果你想向上和向右移动

t.penup()

t.forward(2)
t.right(90)
t.forward(3)

t.write(str(height))

t.forward(-3)
t.right(-90)
t.forward(-2)

t.pendown()

您可以通过保存海龟的状态(它的位置和航向),在绘制标签之前将它稍微向右移动一点,然后再恢复它的状态来实现。

我的意思是:

def drawBar(t, height):
    """ Get turtle t to draw one bar, of height. """
    t.begin_fill()               # start filling this shape
    t.left(90)
    t.forward(height)

    posn = t.position()          # Save position.
    heading = t.heading()        # Save heading.
    t.right(90)                  # Point to the right.
    t.forward(1)                 # Move over a little.
    t.write(height)              # Display the label.
    t.goto(posn)                 # Restore position.
    t.setheading(heading)        # Restore heading.

    t.right(90)
    t.forward(40)
    t.right(90)
    t.forward(height)
    t.left(90)
    t.end_fill()                 # stop filling this shape