我正在尝试为一个需要绘制鲜花的海龟图形程序生成一个随机Y坐标列表,但为了防止鲜花相互绘制,我需要从后到前绘制它们。我需要做的是从大到小对随机列表进行排序,然后让它按这个顺序绘制花朵。
我已经设置了程序来生成9个随机数,并从大到小对它们进行排序,但我不知道如何从该列表中按顺序绘制数字,并为它们分配花朵的Y值
这是我生成随机列表的代码:
def draw_stem(t,StemX,StemY):
StemYcoordinates=random.sample(range(-200,0),9)
sorted(StemYcoordinates, key=int)但我在将它连接到代码的这一部分时遇到了问题,在这部分代码中,它转到了我想要绘制花朵的xy位置
for i in range(9):
t.setheading(90)
t.pensize(7-(StemYcoordinate//40))
t.color("#39ff14")
t.penup()
t.goto(StemX,StemYcoordinates)
t.down()任何帮助都将不胜感激
发布于 2019-03-27 11:53:28
我认为对于您的代码,您将需要使用setpos()方法。
此方法将turtle中的屏幕视为具有四个象限的坐标平面。

使用此选项,可以相应地设置x和y坐标,也可以根据需要随机设置。
例如,每次运行时,这会将海龟放在一个随机的位置:
from turtle import *
t = Turtle()
t.penup()
from random import randint
x = randint(-200, 200)
y = randint(-200, 200)
t.setpos(x, y)希望这能有所帮助!
发布于 2019-03-27 23:39:20
根据对您的问题的描述,您似乎想要以下内容:
from turtle import Screen, Turtle
from random import sample, random
RADIUS = 100
PETALS = 10
def draw_petal(t, radius):
heading = t.heading()
t.circle(radius, 60)
t.left(120)
t.circle(radius, 60)
t.setheading(heading)
def draw_flower(t):
for _ in range(PETALS):
draw_petal(t, RADIUS)
t.left(360 / PETALS)
def draw_flowers(t, stemX):
stemYcoordinates = sorted(sample(range(-200, 0), 9))
for stemYcoordinate in stemYcoordinates:
t.setheading(90)
t.pensize(7 + stemYcoordinate // 40)
t.color(random(), random(), random())
t.penup()
t.goto(stemX, stemYcoordinate)
t.pendown()
draw_flower(t)
screen = Screen()
turtle = Turtle(visible=False)
turtle.speed('fastest') # because I have no patience
draw_flowers(turtle, 0)
screen.exitonclick()

但如果这不是你想要的,花时间重读和编辑你的问题,以阐明你想要做什么。添加更多(如果不是全部)您到目前为止已经编写的代码,以明确您需要帮助的内容。
https://stackoverflow.com/questions/55368406
复制相似问题