Ora

How to draw a dashed line in Python turtle?

Published in Turtle Graphics 4 mins read

Drawing a dashed line in Python Turtle is straightforward and typically involves repeatedly lifting and lowering the pen while moving the turtle forward. This creates alternating segments of drawn lines and empty spaces.

Essential Setup for Turtle Graphics

Before drawing any shapes, including dashed lines, you must initialize the Turtle environment. This always involves creating a Turtle object and a Screen object. These are standard first steps for any drawing program you'll create with Python Turtle.

import turtle

# 1. Create a Screen object (the drawing window)
screen = turtle.Screen()
screen.setup(width=600, height=400) # Optional: Set window size
screen.title("Python Turtle Dashed Line")

# 2. Create a Turtle object (your drawing pen)
pen = turtle.Turtle()
pen.shape("turtle") # Optional: Change the turtle's shape
pen.speed(0) # Optional: Set the drawing speed (0 is fastest)

Method 1: Iterative Pen Up/Down Movement

The most common and flexible way to draw a dashed line is by using a loop that alternates between pen.pendown() (to draw a segment) and pen.penup() (to create a gap) while moving the turtle forward.

How it Works:

  1. pen.pendown(): Puts the pen down, so the turtle will draw when it moves.
  2. pen.forward(dash_length): Moves the turtle, drawing a segment of the specified dash_length.
  3. pen.penup(): Lifts the pen, so the turtle will move without drawing.
  4. pen.forward(gap_length): Moves the turtle, creating an empty gap of the specified gap_length.
  5. Repeat: This sequence is repeated inside a loop to form the dashed line.

Example Code:

# ... (initial setup code from above) ...

# Define properties for the dashed line
dash_length = 10
gap_length = 5
number_of_dashes = 30 # How many dash-gap pairs to draw

pen.pencolor("blue")
pen.pensize(3)

# Start drawing from a specific position (optional)
pen.penup()
pen.goto(-250, 0)
pen.pendown()

# Loop to draw the dashed line
for _ in range(number_of_dashes):
    pen.pendown()           # Pen down, ready to draw
    pen.forward(dash_length) # Draw a dash
    pen.penup()             # Pen up, ready to create a gap
    pen.forward(gap_length)  # Move forward to create a gap

# Keep the window open until closed manually
screen.mainloop()

Customizing Dash Appearance

You have full control over the look of your dashed line:

  • dash_length: Determines the length of each drawn segment.
  • gap_length: Controls the length of the space between dashes.
  • number_of_dashes: Specifies how many dash-gap pairs will be drawn, effectively controlling the total length of the dashed line.
  • pen.pencolor(): Sets the color of the dashes.
  • pen.pensize(): Sets the thickness of the dashes.

Method 2: Creating a Reusable Dash Function

For more complex drawings or if you plan to draw multiple dashed lines with different properties, it's beneficial to encapsulate the dashing logic into a reusable function.

import turtle

screen = turtle.Screen()
screen.setup(width=600, height=400)
screen.title("Python Turtle Reusable Dashed Line")

pen = turtle.Turtle()
pen.speed(0)
pen.pencolor("red")
pen.pensize(2)

# Define a function to draw a dashed line
def draw_dashed_line(turtle_obj, dash, gap, num_dashes):
    for _ in range(num_dashes):
        turtle_obj.pendown()
        turtle_obj.forward(dash)
        turtle_obj.penup()
        turtle_obj.forward(gap)

# Use the function to draw lines
pen.penup()
pen.goto(-250, 50)
draw_dashed_line(pen, 15, 7, 20) # Draw a long red dashed line

pen.penup()
pen.goto(-250, -50)
pen.pencolor("green")
pen.pensize(5)
draw_dashed_line(pen, 8, 8, 25) # Draw a shorter, thicker green dashed line

screen.mainloop()

Enhancing Your Dashed Lines

  • turtle.pensize(width): Adjusts the thickness of the line.
  • turtle.pencolor(color_name): Changes the color of the line. You can use common color names (e.g., "red", "blue", "green") or hex codes (e.g., "#FF0000").
  • turtle.speed(speed_value): Controls the animation speed. 0 is the fastest, 1 is slowest, 10 is fast.

For more information on the Turtle graphics module, refer to the official Python Turtle documentation.