[英]How to draw numbers from a random list to use as Y coordinates for turtle graphics
我正在嘗試為需要繪制花朵的烏龜圖形程序生成一個隨機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()
任何幫助將不勝感激
基於您的問題的描述,您似乎想要類似的東西:
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()
但如果這不是您想要的,請花點時間重新閱讀並編輯您的問題,以澄清您想要做的事情。 添加更多(如果不是全部)您目前編寫的代碼,以明確您需要幫助的內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.