简体   繁体   中英

How to generate a random star using turtle graphics in Python?

I'm new to turtle graphics in Python, and I'm running into some issues with a particular problem. I'm trying to generate a star that uses a while loop to draw random jagged lines from the center of a circle.

Each line should have a distance of 250. I'm using the penup pendown and setpos commands within the loop to draw these random lines, and each line should be a random color.

Here's an idea of what I'm hoping to generate: random star

Here's the code I have so far:

# tg_random_star.py

from random import randrange
from turtle import *

MAX_ANGLE = 30

def jaggedLine(turtle, pieces, pieceLength):

    for i in range(pieces):
        turtle.forward(pieceLength)
        r = randrange(-MAX_ANGLE, MAX_ANGLE + 1)
        turtle.right(r)

def jumpToCenter(turtle):
    turtle.penup()
    turtle.setpos(0, 0)
    turtle.pendown()

def randomColor(turtle):
    turtle.colormode(255)
    r = randrange(255)    # red component of color
    g = randrange(255)    # green component
    b = randrange(255)    # blue component

    turtle.pencolor(r, g, b)

def main():
    colormode(255)
    t = Turtle()
    jaggedLine(t, 10, 30)
    jumpToCenter(t)
    jaggedLine(t, 10, 30)

if __name__ == "__main__":
    main()

It currently generates 2 lines, but the turtle.pencolor(r, g, b) and the colormode(255) don't seem to be working, as both lines are black. Any idea why these lines aren't in color?

Rather than using for i in range(pieces) to draw lines that are based on the number of segments, how can I use a while loop to draw jagged lines that each have a distance of 250? In other words, I want each line to have a distance of 250 before drawing a new line from the center.

(Maybe I could use the xcor and ycor methods to find the turtle's position, then calculate the distance using the distance formula?)

def distance(p0, p1):
return math.sqrt((p0[0] - p1[0])**2 + (p0[1] - p1[1])**2)`

Any help or explanation would be appreciated, thank you.

Any idea why these lines aren't in color?

That one's easy, it's because you never actually call randomColor()

Rather than using for i in range(pieces) to draw lines that are based on the number of segments, how can I use a while loop to draw jagged lines that each have a distance of 250?

Here we can take advantage of the under utilized .distance() method of turtle to tell us how far we are from center. This is straight line distance, not path travelled distance, which seems to match your target illustration:

from turtle import Turtle, Screen
from random import randrange

MAX_ANGLE = 30
MAX_DISTANCE = 250

def jaggedLine(t, pieceLength):
    randomColor(t)

    while t.distance(0, 0) < MAX_DISTANCE:
        angle = randrange(-MAX_ANGLE, MAX_ANGLE + 1)
        t.right(angle)
        t.forward(pieceLength)

def jumpToCenter(t):
    t.penup()
    t.home()  # return turtle to original position
    t.pendown()

def randomColor(t):
    # red, green & blue components of turtle color
    r, g, b = randrange(255), randrange(255), randrange(255)

    t.pencolor(r, g, b)

def main():
    screen = Screen()
    screen.tracer(False)  # because I have no patience
    screen.colormode(255)

    turtle = Turtle()
    turtle.hideturtle()
    turtle.pensize(2)

    for angle in range(0, 360, 2):
        jumpToCenter(turtle)
        turtle.setheading(angle)
        jaggedLine(turtle, 30)

    screen.tracer(True)  # forces an update
    screen.mainloop()

if __name__ == "__main__":
    main()

OUTPUT

在此处输入图片说明

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