如何在Python写一首诗?

发布于 2025-02-13 18:22:14 字数 1522 浏览 0 评论 0原文

import turtle
Elizabeth = turtle.Turtle()
Elizabeth.shape('turtle')
Elizabeth.penup()

def line(words, horiz_pos = -50):
x,y = Elizabeth.pos()
Elizabeth.goto(max(horiz_pos, -190), y)
Elizabeth.write(words)
x,y = Elizabeth.pos()
Elizabeth.goto(x, y - 25)

def by(author):
x,y = Elizabeth.pos()
Elizabeth.goto(x + 70, max( -190, y -30))
Elizabeth.write(author)
x,y = Elizabeth.pos()
Elizabeth.goto(0, y)

Elizabeth.goto(-50, 190)
line("""Land lies in water; it is shadowed green.
Shadows, or are they shallows, at its edges
showing the line of long sea- weeded ledges
where weeds hang to the simple blue from green.
Or does the land lean down to lift the sea from under,
drawing it unperturbed around itself ?
Along the fine tan sandy shelf
is the land tugging at the sea from under?
""", -190)
line("")
line("""The shadow of Newfoundland lies flat at and still.
Labrador’s yellow, where the moony Eskimo
has oiled it. We can stroke these lovely bays,
under a glass as if they were expected to blossom,
or as if to provide a clean cage for invisible fish.
The names of seashore towns run out to sea,
the names of cities cross the neighboring mountains
— the printer here experiencing the same excitement
as when emotion too far exceeds its cause.
These peninsulas take the water between thumb and finger
like women feeling for the smoothness of yard- goods.""", -190)

我正在使用Turtle创作一首由Elizaebeth Bishop的诗,称为地图。但是我正面临一个问题,那就是当我运行代码时,它与乌龟窗口中的所有诗歌都重叠。我无法在没有重叠的情况下正确的4-5行诗歌,或者它开始向屏幕顶部移动,而一半的诗歌看不见:

import turtle
Elizabeth = turtle.Turtle()
Elizabeth.shape('turtle')
Elizabeth.penup()

def line(words, horiz_pos = -50):
x,y = Elizabeth.pos()
Elizabeth.goto(max(horiz_pos, -190), y)
Elizabeth.write(words)
x,y = Elizabeth.pos()
Elizabeth.goto(x, y - 25)

def by(author):
x,y = Elizabeth.pos()
Elizabeth.goto(x + 70, max( -190, y -30))
Elizabeth.write(author)
x,y = Elizabeth.pos()
Elizabeth.goto(0, y)

Elizabeth.goto(-50, 190)
line("""Land lies in water; it is shadowed green.
Shadows, or are they shallows, at its edges
showing the line of long sea- weeded ledges
where weeds hang to the simple blue from green.
Or does the land lean down to lift the sea from under,
drawing it unperturbed around itself ?
Along the fine tan sandy shelf
is the land tugging at the sea from under?
""", -190)
line("")
line("""The shadow of Newfoundland lies flat at and still.
Labrador’s yellow, where the moony Eskimo
has oiled it. We can stroke these lovely bays,
under a glass as if they were expected to blossom,
or as if to provide a clean cage for invisible fish.
The names of seashore towns run out to sea,
the names of cities cross the neighboring mountains
— the printer here experiencing the same excitement
as when emotion too far exceeds its cause.
These peninsulas take the water between thumb and finger
like women feeling for the smoothness of yard- goods.""", -190)

I am using turtle to create a poem called THE MAP BY ELIZAEBETH BISHOP. But I am facing a problem, which is that when I run the code it overlaps all the lines of poetry in the turtle window. I cant right more than 4-5 lines of poetry without it overlapping or it starts moving towards the top of the screen and half the poem cannot be seen:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

别忘他 2025-02-20 18:22:14

重叠的问题在于line函数:

Elizabeth.goto(x, y - 25)

在这里,您将笔向下移动25像素,而与文本中的线路无关。
取而代之的是,计算线的量,然后用线路将它们乘以根据书面文字移动笔。

用以下三行替换此行,以获得不重叠的结果:

number_of_lines = words.count("\n") + 1
line_height = 14  # measured by me
Elizabeth.goto(x, y - line_height * number_of_lines)

The issue of the overlap is in the line function:

Elizabeth.goto(x, y - 25)

Here, you move the pen downwards by 25 pixels, independent of the linebreaks within the text.
Instead, calculate the amount of lines and multiply them with the lineheight to move the pen according to the written text.

Replace this line with the following three lines to get a non-overlapping result:

number_of_lines = words.count("\n") + 1
line_height = 14  # measured by me
Elizabeth.goto(x, y - line_height * number_of_lines)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文