Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Draw Starry Sky with Moon using Turtle in Python

  • Difficulty Level : Expert
  • Last Updated : 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.

METHODPARAMETER DESCRIPTION
Turtle()None It creates and returns a new turtle object.
bgcolorcolor namefills the color in the background of the window
circle()radius, extent, stepsDraw a circle with given radius.
title()nameGives name to the turtle window
forward() / fd()amountIt moves the turtle forward by the specified amount.
Screen()NoneAfter passing this method turtle screen will be active
speedvalueDecides the speed of the pen
up()NonePicks up the turtle’s Pen.
down()NonePicks down the turtle’s Pen.
left()angleIt turns the turtle counter clockwise.
right()angleIt turns the turtle clockwise.
goto() x, yIt moves the turtle to position x, y.
begin_fill()NoneCall just before drawing a shape to be filled. Equivalent to fill(True).
end_fill()NoneFill the shape drawn after the last call to begin_fill(). Equivalent to fill(False).
hideturtle()NoneHides the turtle after finishing the drawing.
exitonclick()NoneOn clicking exit the turtle screen.
randintstart, end valueGenerates random values with in the given range.

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 library
import turtle
  
  
# creating turtle object
t = turtle.Turtle()
  
# to activate turtle graphics screen
w = turtle.Screen()
  
# speed of turtle's pen
t.speed(0)
  
# creating star
for i in range(0, 5):
    t.fd(200)
    t.right(144)
  
# after clicking turtle graphics screen
# will be terminated
w.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 library
import turtle
  
  
# creating turtle object
t = turtle.Turtle()
  
# to activate turtle graphics Screen
w = turtle.Screen()
  
# speed of turtle's pen
t.speed()
  
# Creating circle
t.circle(100)
  
# after clicking turtle graphics screen 
# will be terminated
w.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 libraries
import turtle
import random
  
  
# creating turtle object
t = turtle.Turtle()
  
# to activate turtle graphics Screen
w = turtle.Screen()
  
# setting speed of turtle
t.speed(0)
  
# giving the background color of turtle
# graphics screen
w.bgcolor("black")
  
# giving the color of pen to our turtle
# for drawing
t.color("white")
  
# giving title to our turtle graphics window
w.title("Starry Sky")
  
# making function to draw the stars
def stars():
    for i in range(5):
        t.fd(10)
        t.right(144)
  
  
# loop for making number of stars
for 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 pen
t.up()
  
# going at the specific coordinated
t.goto(0, 170)
  
# took down the pen to start drawing
t.down()
  
# giving color to turtle's pen
t.color("white")
  
# start filling the color
t.begin_fill()
  
# making our moon
t.circle(80)
  
# stop filling the color
t.end_fill()
  
# after drawing hidding the turtle from
# the window
t.hideturtle()
  
# terminated the window after clicking
w.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.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!