简体   繁体   中英

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. 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

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

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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