Open in App
Not now

# Draw Colourful Star Pattern in Turtle – Python

• Difficulty Level : Medium
• Last Updated : 08 Jun, 2020

In this article we will use Python’s turtle library to draw a spiral of stars, filled with randomly generated colours. We can generate different patterns by varying some parameters. modules required:

turtle:

turtle library enables users to draw picture or shapes using commands, providing them with a virtual canvas.
turtle comes with Python’s Standard Library. It needs a version of Python with Tk support, as it uses tkinter for the graphics.

Explanation:
First we set each of the parameters for the spiral: number of stars, exterior angle of the stars and angle of rotation for the spiral. The colours are chosen randomly by choosing three random integers for rgb values, and so each time we get a different colour combination.
In the implementation below, we will draw a pattern of 30 stars, with exterior angle 144 degrees and angle of rotation 18 degrees.

 `from` `turtle ``import` `*` `import` `random`` ` `speed(speed ``=``'fastest'``)`` ` `def` `draw(n, x, angle):``    ``# loop for number of stars``    ``for` `i ``in` `range``(n):``         ` `        ``colormode(``255``)``         ` `        ``# choosing random integers ``        ``# between 0 and 255``        ``# to generate random rgb values ``        ``a ``=` `random.randint(``0``, ``255``)``        ``b ``=` `random.randint(``0``, ``255``)``        ``c ``=` `random.randint(``0``, ``255``)``         ` `        ``# setting the outline ``        ``# and fill colour``        ``pencolor(a, b, c)``        ``fillcolor(a, b, c)``         ` `        ``# begins filling the star``        ``begin_fill()``         ` `        ``# loop for drawing each star``        ``for` `j ``in` `range``(``5``):``              ` `            ``forward(``5` `*` `n``-``5` `*` `i)``            ``right(x)``            ``forward(``5` `*` `n``-``5` `*` `i)``            ``right(``72` `-` `x)``             ` `        ``# colour filling complete``        ``end_fill()``         ` `        ``# rotating for``        ``# the next star``        ``rt(angle)``         ` ` ` `# setting the parameters``n ``=` `30`    `# number of stars``x ``=` `144`   `# exterior angle of each star``angle ``=` `18`    `# angle of rotation for the spiral`` ` `draw(n, x, angle)`

Output:

By changing the exterior angle to 72, we can get a pattern of pentagons as such:

My Personal Notes arrow_drop_up
{"ft_lang_label":"__label__en","ft_lang_prob":0.66194993,"math_prob":0.8732786,"size":1810,"snap":"2022-40-2023-06","text_gpt3_token_len":472,"char_repetition_ratio":0.11406423,"word_repetition_ratio":0.04819277,"special_character_ratio":0.2718232,"punctuation_ratio":0.12034384,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9946228,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,7,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-02-08T06:40:09Z\",\"WARC-Record-ID\":\"<urn:uuid:0db53719-2aac-4acd-9b8a-888eaf5dffe4>\",\"Content-Length\":\"156815\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:4cde2e1e-a76e-4257-a69f-26337cfcc0b3>\",\"WARC-Concurrent-To\":\"<urn:uuid:3a69d6ac-7d4f-4221-b7c7-d4d41bcba461>\",\"WARC-IP-Address\":\"23.15.9.42\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/draw-colourful-star-pattern-in-turtle-python/\",\"WARC-Payload-Digest\":\"sha1:ZS2VHG4XLGCQLIPI72YX5T5S5XWUXR4M\",\"WARC-Block-Digest\":\"sha1:6PAZXGDQ7TDBMVTET4VBWQXMGU52H2PQ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-06/CC-MAIN-2023-06_segments_1674764500719.31_warc_CC-MAIN-20230208060523-20230208090523-00124.warc.gz\"}"}