Draw Starry Sky with Moon using Turtle in Python

25 Oct, 2020
Prerequisites: Turtle Programming in Python
In Python, Turtle is an in-built library that provides us to draw different patterns, shapes or anything we want but for that, we have to be aware of the uses of different functions provided by Turtle library.
In this article we are going to learn how to draw the Starry Sky. Maybe this article went long but after reading the whole article you get to understand each and every step easily.
For that, we are using the turtle library and random module for generating random positions to draw to the stars.
Methods we are using to draw Starry Sky are listed below.
Our Starry Sky is made up of many Stars and with one moon which is in the shape of a Circle. So, we have to learn first how to draw the Stars and Circle.
Let’s first learn how to draw the star through our code.
Python
# importing turtle libraryimport turtle    # creating turtle objectt = turtle.Turtle()  # to activate turtle graphics screenw = turtle.Screen()  # speed of turtle's pent.speed(0)  # creating starfor i in range(0, 5):    t.fd(200)    t.right(144)  # after clicking turtle graphics screen# will be terminatedw.exitonclick()
Output:
In the above code, we run the for loop from range 0 to 5 because as we all knew star has 5 edges and the angle between two edges will be 144 degrees to make the perfect star that’s what we are doing in the line t.right(144) we are moving the turtles head at the angle 144 degrees and the line t.fd(200) decides the size of our star (smaller the value = smaller the star)
Now, we are going to learn how to draw the Circle through our code.
Python
# importing turtle libraryimport turtle    # creating turtle objectt = turtle.Turtle()  # to activate turtle graphics Screenw = turtle.Screen()  # speed of turtle's pent.speed()  # Creating circlet.circle(100)  # after clicking turtle graphics screen # will be terminatedw.exitonclick()
Output:
In the above code, for making a circle we are using the circle function provided by the turtle library, only we had passed the parameter 100 which defines the radius of the circle. After running our code we get our circle.
So, finally, we had learned how to draw the star and the circle.
Now we are going to draw our Starry Sky.
Python
# importing librariesimport turtleimport random    # creating turtle objectt = turtle.Turtle()  # to activate turtle graphics Screenw = turtle.Screen()  # setting speed of turtlet.speed(0)  # giving the background color of turtle# graphics screenw.bgcolor("black")  # giving the color of pen to our turtle# for drawingt.color("white")  # giving title to our turtle graphics windoww.title("Starry Sky")  # making function to draw the starsdef stars():    for i in range(5):        t.fd(10)        t.right(144)    # loop for making number of starsfor i in range(100):        # generating random integer values for x and y    x = random.randint(-640, 640)    y = random.randint(-330, 330)          # calling the function stars to draw the     # stars at random x,y value    stars()          # took up the turtle's pen    t.up()          # go at the x,y coordinate generated above    t.goto(x, y)          # took down the pen to draw    t.down()  # for making our moon tooking up the pent.up()  # going at the specific coordinatedt.goto(0, 170)  # took down the pen to start drawingt.down()  # giving color to turtle's pent.color("white")  # start filling the colort.begin_fill()  # making our moont.circle(80)  # stop filling the colort.end_fill()  # after drawing hidding the turtle from# the windowt.hideturtle()  # terminated the window after clickingw.exitonclick()
Output:
Starry Sky
In the above code you will be understand after reading the comments before every line of code you get the crystal clear idea that how our code works and drawing the Starry stars.
Python-turtle
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.