简体   繁体   English

如何从随机列表中绘制数字以用作龟图形的Y坐标

[英]How to draw numbers from a random list to use as Y coordinates for turtle graphics

I am trying to generate a list of random Y coordinates for a turtle graphics program that needs to draw flowers, but to prevent the flowers from drawing over each other I need to draw them from back to front. 我正在尝试为需要绘制花朵的乌龟图形程序生成一个随机Y坐标列表,但为了防止花朵相互绘制,我需要从后向前绘制它们。 What I need to do is sort the random list from largest to smallest, then have it draw the flowers in that order. 我需要做的是将随机列表从大到小排序,然后让它按顺序绘制花朵。

I have already set up the program to generate 9 random numbers and sort them from largest to smallest, but I don't know how to draw the numbers from that list in order and assign them the Y value of the flower 我已经设置了程序来生成9个随机数并从最大到最小排序,但我不知道如何按顺序从该列表中绘制数字并为它们分配花的Y值

This is my code for generating the random list: 这是我生成随机列表的代码:

def draw_stem(t,StemX,StemY):
    StemYcoordinates=random.sample(range(-200,0),9)
    sorted(StemYcoordinates, key=int)

But I am having trouble connecting it to this part of the code where it goes to the xy position where I want the flower to be drawn 但我无法将它连接到代码的这一部分,它会进入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()

any help would be greatly appreciated 任何帮助将不胜感激

I think for your code, you will need to use the setpos() method. 我认为对于您的代码,您将需要使用setpos()方法。

This method treats the screen in turtle like a coordinate plane with four quadrants. 此方法将乌龟屏幕视为具有四个象限的坐标平面。

在此输入图像描述

Using this, set the x and y coordinates accordingly, or randomly if you want to. 使用此选项,相应地设置x和y坐标,或者根据需要随机设置。

For example, this puts the turtle in a random spot every time you run it: 例如,每次运行时,这会将乌龟放在一个随机位置:

from turtle import *
t = Turtle()
t.penup()
from random import randint
x = randint(-200, 200)
y = randint(-200, 200)
t.setpos(x, y)

Hope this Helps!!! 希望这可以帮助!!!

Based on the description of your problem, you seem to want something like: 基于您的问题的描述,您似乎想要类似的东西:

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()

在此输入图像描述

But if this isn't what you're looking for, take the time to reread and edit your question to clarify what you want to do. 但如果这不是您想要的,请花点时间重新阅读并编辑您的问题,以澄清您想要做的事情。 Add more (if not all) of the code you've written so far, to make it clear what you need help with. 添加更多(如果不是全部)您目前编写的代码,以明确您需要帮助的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM