Drawing and Filling a Star with Multiple Colors in Python

Drawing geometric shapes, especially stars, and filling them with multiple colors can be an engaging task for both beginners and experienced programmers. Python, with its vast array of libraries, provides a simple yet powerful way to accomplish this task. In this article, we will explore how to draw a five-pointed star and fill it with different colors using Python.

To achieve this, we will primarily use the turtle module, which is part of Python’s standard library and is designed for introductory programming exercises. It provides a simple way to create graphics and explore programming concepts through visual outputs.

Step 1: Importing the Turtle Module

First, we need to import the turtle module. This module allows us to create a canvas on which we can draw shapes and apply colors.

pythonCopy Code
import turtle

Step 2: Setting Up the Turtle

Before we start drawing, we need to set up the turtle by defining its speed, the background color of the canvas, and lifting the pen to avoid unwanted lines.

pythonCopy Code
turtle.speed(1) # Setting the drawing speed turtle.bgcolor("black") # Setting the background color turtle.penup() # Lifting the pen to move without drawing

Step 3: Drawing the Star

To draw a star, we can use a simple approach where we draw lines and turn the turtle at specific angles. A five-pointed star can be drawn by making the turtle turn 144 degrees after drawing each line segment.

pythonCopy Code
turtle.goto(0, -100) # Moving the turtle to start position turtle.pendown() # Putting the pen down to start drawing turtle.begin_fill() # Start filling the shape for _ in range(5): turtle.forward(200) # Drawing a line segment turtle.right(144) # Turning the turtle turtle.end_fill() # End filling the shape

Step 4: Filling the Star with Multiple Colors

To fill the star with multiple colors, we can utilize the turtle’s ability to fill shapes. However, since the begin_fill() and end_fill() methods only allow for a single fill color, we need to approach this differently. We can draw multiple overlapping stars, each with a different color.

pythonCopy Code
colors = ["red", "blue", "green", "yellow", "purple"] # List of colors for i in range(5): turtle.color(colors[i]) # Changing the color turtle.begin_fill() # Start filling for _ in range(5): turtle.forward(200) turtle.right(144) turtle.end_fill() # End filling turtle.right(72) # Rotating the star for the next iteration

Step 5: Completing the Drawing

After drawing and filling the star with multiple colors, we can hide the turtle cursor and keep the drawing window open for viewing.

pythonCopy Code
turtle.hideturtle() # Hiding the turtle cursor turtle.done() # Keeping the window open

By following these steps, you can create a visually appealing five-pointed star filled with multiple colors using Python’s turtle module. This exercise not only enhances your programming skills but also allows you to explore the creative side of coding.

[tags]
Python, Turtle Graphics, Drawing Shapes, Filling Colors, Programming Exercise

Python official website: https://www.python.org/