How to draw color filled star in Python-Turtle?

How to draw color filled star in Python-Turtle?

To draw a color-filled star using the turtle module in Python, you can follow these steps:

  1. Import the turtle module.
  2. Set up the turtle settings like speed, shape, and screen color.
  3. Use the begin_fill() method to start filling the shape.
  4. Draw the star using a loop.
  5. Use the end_fill() method to complete the fill process.
  6. Use the done() method to finish the drawing.

Below is a simple example to draw a 5-pointed star filled with the color red:

import turtle # Set up the screen screen = turtle.Screen() screen.bgcolor("white") # Create a turtle object star = turtle.Turtle() star.shape("turtle") star.speed(5) # Set drawing speed # Color settings star.color("black", "red") # (border color, fill color) def draw_star(size): star.begin_fill() for _ in range(5): star.forward(size) star.right(144) star.end_fill() # Draw the star draw_star(100) # Close the window on click screen.exitonclick() 

You can adjust the size of the star by changing the argument to the draw_star() function. The current example draws a star with each line segment of length 100. Adjust the angle inside the loop if you want a star with a different number of points.


More Tags

maatwebsite-excel android-constraintlayout elastic-stack libusb-1.0 android-coordinatorlayout flash-message dependency-management require real-time-clock multiple-select

More Programming Guides

Other Guides

More Programming Examples