Creating a Starry Sky with 100 Random Stars using Python

In this blog post, we’ll delve into the process of creating a visually appealing starry sky with 100 random stars using Python. We’ll utilize the turtle graphics module to accomplish this task and discuss the necessary steps in detail.

Setting Up the Canvas

To begin, we’ll need to set up a canvas where we can draw our stars. The turtle module provides a convenient way to do this. We’ll create a new screen, set its background color to black to mimic the night sky, and create a turtle object to handle the drawing.

pythonimport turtle

# Set up the canvas
screen = turtle.Screen()
screen.bgcolor("black") # Set the background color to black
star_turtle = turtle.Turtle()
star_turtle.speed(0) # Set the drawing speed to the fastest

Drawing a Star

Next, we’ll define a function to draw a star. A star can be approximated by drawing a pentagon with each side extended slightly. We’ll use the turtle’s forward() and right() methods to achieve this.

pythondef draw_star(turtle, size):
for _ in range(5):
turtle.forward(size)
turtle.right(144) # Star points are separated by 144 degrees

Generating and Drawing Random Stars

Now, we’ll generate 100 random stars and draw them on the canvas. For each star, we’ll randomly choose a position within the canvas boundaries and a size. We’ll also randomly select a color for each star to create a more vibrant sky.

python# Generate and draw random stars
num_stars = 100
for _ in range(num_stars):
x = random.randint(-300, 300) # Random x position
y = random.randint(-200, 200) # Random y position
size = random.randint(2, 5) # Random star size
star_turtle.penup()
star_turtle.goto(x, y) # Move to the star's position
star_turtle.color(random.choice(["white", "yellow", "orange"])) # Random star color
star_turtle.pendown()
draw_star(star_turtle, size) # Draw the star

# Remember to import random module
import random

Finishing Up

After drawing all the stars, we’ll close the turtle screen to end the program.

python# Close the canvas
turtle.done()

Combining the Code

Here’s the complete code that combines all the steps discussed above:

pythonimport turtle
import random

# Set up the canvas
screen = turtle.Screen()
screen.bgcolor("black")
star_turtle = turtle.Turtle()
star_turtle.speed(0)

# Function to draw a star
def draw_star(turtle, size):
for _ in range(5):
turtle.forward(size)
turtle.right(144)

# Generate and draw random stars
num_stars = 100
for _ in range(num_stars):
x = random.randint(-300, 300)
y = random.randint(-200, 200)
size = random.randint(2, 5)
star_turtle.penup()
star_turtle.goto(x, y)
star_turtle.color(random.choice(["white", "yellow", "orange"]))
star_turtle.pendown()
draw_star(star_turtle, size)

# Close the canvas
turtle.done()

Conclusion

By utilizing the turtle graphics module in Python, we’ve successfully created a starry sky with 100 random stars. You can experiment with different parameters, such as the number of stars, their sizes, colors, and positions, to create a variety of starry sky scenes. This project not only showcases the beauty of generative art but also demonstrates the power of Python’s graphics capabilities.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *