Starry Night with Python Turtle
Tags: python, tutorial, turtle, logo
Published: 2021-06-04 16:54:47

In the previous posts we did

1. [Filled Stars](https://dev.to/taarimalta/how-to-draw-a-filled-star-with-python-turtle-2k0g)
2. [Spirals](https://dev.to/taarimalta/how-to-draw-a-spiral-with-python-turtle-2n5c)
3. [Rainbow and clouds](https://dev.to/taarimalta/rainbow-and-clouds-with-python-turtle-pgh)

In this article we will draw a night sky with a crescent moon. This will give us an opportunity to play with the `random` library of Python.

# Set up the canvas

Let's set up the screen to a defined size and color it black like the dark of night.

```python
import turtle as t

SCREEN_WIDTH=1000
SCREEN_HEIGHT=1000

t.Screen().screensize(SCREEN_WIDTH,SCREEN_HEIGHT)
t.Screen().setworldcoordinates(-SCREEN_WIDTH,-SCREEN_HEIGHT,SCREEN_WIDTH,SCREEN_HEIGHT)
t.Screen().bgcolor("black")
```

We are setting up a square grid of coordinates

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fxei1h2xl9rviulaxuj8.png)

# Generate stars

We can approximate stars by simply using white dots

```python
import turtle as t

SCREEN_WIDTH=1000
SCREEN_HEIGHT=1000
STAR_COLOR="white"

t.Screen().screensize(SCREEN_WIDTH,SCREEN_HEIGHT)
t.Screen().setworldcoordinates(-SCREEN_WIDTH,-SCREEN_HEIGHT,SCREEN_WIDTH,SCREEN_HEIGHT)
t.Screen().bgcolor("black")


t.penup()
t.color(STAR_COLOR, STAR_COLOR)

t.goto(100,100)
t.dot(1)
t.goto(20,20)
t.dot(2)
t.goto(-100, -10)
t.dot(1)
t.goto(10,-100)
t.dot(1)

t.hideturtle()
```

In the code above all we're doing is moving the turtle into different coordinates on the screen to plot dots. 

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cq4sko2se6anu8ub8h7j.png)
 
Instead of manually defining the coordinates of the stars lets randomly generate them. Here's how we can do that

```python
import random

for _ in range(1000)
    x = random.randint(-1000,1000)
    y = random.randint(-1000,1000)

    print(x,y)

```
This will print a series of coordinates which are pseudo-random

```
779 -633
359 357
622 410
-606 -540
-532 517
-67 -378
...
74 220
-953 204
-240 712
57 994
```

We will use the method above to generate the coordinates of the star. Here is the code

```python
import turtle as t
import random

SCREEN_WIDTH=1000
SCREEN_HEIGHT=1000
STAR_COLOR="white"

t.Screen().tracer(0,0)
t.Screen().screensize(SCREEN_WIDTH,SCREEN_HEIGHT)
t.Screen().setworldcoordinates(-SCREEN_WIDTH,-SCREEN_HEIGHT,SCREEN_WIDTH,SCREEN_HEIGHT)
t.Screen().bgcolor("black")



def random_star():
  t.penup()
  t.color(STAR_COLOR, STAR_COLOR)
  
  x=random.randint(-SCREEN_WIDTH,SCREEN_WIDTH)
  y=random.randint(-SCREEN_HEIGHT,SCREEN_HEIGHT)
  
  dot_size=random.random()*3
  
  t.goto(x,y)
  t.dot(dot_size)
    

for _ in range(1000):
    random_star()
    
t.hideturtle()
t.Screen().update()
```

In the code above we have not only randomized the location of the stars but also the size of the stars (using `dot`). The size of the dot uses `random.random()*3` instead of `randint` function. This gives us continuous random sizes as opposed to discreet.

Here is the result:

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/swungn19wb1w9c7fyeyg.png)

Now all we need is a moon

# Draw a crescent moon

A direct approach to drawing a moon could be to draw a semicircle, then turn the turtle around and draw another arc to complete the loop. We will use an easier way where we don't have to compute the angles.

Here is the function that draws a moon

```python
def moon(size=100):
  t.penup()
  t.left(20)
  
  t.color(MOON_COLOR, MOON_COLOR)
  t.dot(size)
  t.forward(size*.25)
  t.color(NIGHT_COLOR, NIGHT_COLOR)
  t.dot(size)
```

We are simply drawing a large white dot and then overlapping a large black dot on top of it. 

> One disadvantage of this approach is that some stars that are located outside the sphere of the moon are erased, but maybe no one will notice ;)

Here is the final code:

```python
import turtle as t
import random

SCREEN_WIDTH=1000
SCREEN_HEIGHT=1000
STAR_COLOR="#f0ffff"
MOON_COLOR="#FFFFF0"
NIGHT_COLOR="black"

t.Screen().tracer(0,0)
t.Screen().screensize(SCREEN_WIDTH,SCREEN_HEIGHT)
t.Screen().setworldcoordinates(-SCREEN_WIDTH,-SCREEN_HEIGHT,SCREEN_WIDTH,SCREEN_HEIGHT)
t.Screen().bgcolor(NIGHT_COLOR)
    
def moon(size=100):
  t.penup()
  t.left(20)
  
  t.color(MOON_COLOR, MOON_COLOR)
  t.dot(size)
  t.forward(size*.25)
  t.color(NIGHT_COLOR, NIGHT_COLOR)
  t.dot(size)

def random_star():
  t.penup()
  t.color(STAR_COLOR, STAR_COLOR)
  
  x=random.randint(-SCREEN_WIDTH,SCREEN_WIDTH)
  y=random.randint(-SCREEN_HEIGHT,SCREEN_HEIGHT)
  
  dot_size=random.random()*3
  
  t.goto(x,y)
  t.dot(dot_size)
    

for _ in range(1000):
    random_star()
    
moon(150)
    
t.hideturtle()
t.Screen().update()
```

> Note that the location of the moon is going to be random too

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z60wixakhra88xxci6pr.png)

Yay!!~~