To draw a hexagon in Python Turtle, you repeat the action of moving the turtle forward for a specific distance and then turning it 60 degrees, completing this process six times. This method is straightforward and effectively constructs a regular hexagon.
Understanding Turtle Graphics and Hexagons
Python Turtle Graphics is a built-in module that provides a simple way to create graphics. It simulates a "turtle" on a canvas, which you can command to move, turn, and draw. This makes it an excellent tool for learning programming fundamentals and geometry.
A hexagon is a polygon with six sides and six angles. For a regular hexagon, all sides are of equal length, and all interior angles are equal (120 degrees each). When drawing with Turtle, we typically focus on the exterior angle, which is the angle the turtle needs to turn to draw the next side.
The formula for the exterior angle of any regular polygon is:
Exterior Angle = 360 degrees / Number of Sides
For a hexagon, with 6 sides:
Exterior Angle = 360 / 6 = 60 degrees
This means that after drawing each side, the turtle must turn 60 degrees to be correctly oriented for drawing the next side. Repeating this action six times will complete the hexagon, bringing the turtle back to its starting position and orientation.
Step-by-Step Guide to Drawing a Hexagon
Here’s how you can draw a hexagon using Python Turtle:
- Import the
turtle
module: This gives you access to all the Turtle graphics functions. - Set up the screen: Create a graphics window where your turtle will draw.
- Create a turtle object: Instantiate a turtle that you can control.
- Use a loop: A
for
loop is perfect for repeating the drawing steps for each side of the hexagon. - Move forward: Command the turtle to move a certain distance (this is one side of the hexagon).
- Turn left (or right): Command the turtle to turn 60 degrees.
- Keep the window open: Ensure the drawing window stays visible until you close it manually.
Basic Code Example
import turtle
# 1. Set up the screen
screen = turtle.Screen()
screen.setup(width=600, height=600) # Optional: set window size
screen.title("Python Turtle - Drawing a Hexagon")
# 2. Create a turtle object
pen = turtle.Turtle()
pen.shape("turtle") # Optional: change turtle shape
pen.color("blue") # Optional: set pen color
pen.pensize(3) # Optional: set pen thickness
pen.speed(1) # Set drawing speed (1 slowest, 10 fastest, 0 instant)
# 3. Draw the hexagon
# A hexagon has 6 sides, and the exterior angle is 360/6 = 60 degrees
num_sides = 6
side_length = 100 # Length of each side
for _ in range(num_sides):
pen.forward(side_length)
pen.left(60) # Turn 60 degrees to the left
# 4. Keep the window open until closed manually
screen.mainloop()
In this example, the for
loop iterates 6 times. In each iteration, the turtle moves side_length
pixels forward and then turns left by 60 degrees. After the sixth iteration, the hexagon is complete, and the turtle will be back at its original starting point and facing its initial direction.
Customizing Your Hexagon
Turtle Graphics offers many ways to customize your drawing:
- Side Length: Change the
side_length
variable to make the hexagon larger or smaller. - Pen Color: Use
pen.color("red")
orpen.pencolor("green")
to change the line color. - Fill Color: You can fill the hexagon with a color:
pen.begin_fill() pen.fillcolor("yellow") for _ in range(num_sides): pen.forward(side_length) pen.left(60) pen.end_fill()
- Pen Speed: Adjust
pen.speed()
from 0 (fastest) to 1 (slowest) or higher numbers for more control. - Pen Up/Down: Use
pen.penup()
to lift the pen (move without drawing) andpen.pendown()
to put it back down. - Starting Position: Use
pen.setx(x_coord)
,pen.sety(y_coord)
, orpen.goto(x, y)
to move the turtle to a specific starting point before drawing.
Drawing Other Regular Polygons
The same principle applies to drawing any regular polygon. You just need to adjust the number of sides and the turn angle.
Polygon | Number of Sides | Exterior Angle (360 / Sides) |
---|---|---|
Triangle | 3 | 120 degrees |
Square | 4 | 90 degrees |
Pentagon | 5 | 72 degrees |
Hexagon | 6 | 60 degrees |
Octagon | 8 | 45 degrees |
By simply changing the num_sides
variable and the turn_angle
in the loop, you can draw a variety of regular shapes.
Reusable Function for Drawing Polygons
For more complex drawings or to draw multiple hexagons, encapsulating the drawing logic in a function is a good practice:
import turtle
def draw_polygon(turt, num_sides, side_length, pen_color="black", fill_color=None):
"""
Draws a regular polygon using the given turtle.
Args:
turt (turtle.Turtle): The turtle object to draw with.
num_sides (int): The number of sides for the polygon.
side_length (int): The length of each side.
pen_color (str): The color of the polygon's outline.
fill_color (str, optional): The fill color of the polygon.
"""
turt.pencolor(pen_color)
turt.pensize(2)
angle = 360 / num_sides
if fill_color:
turt.fillcolor(fill_color)
turt.begin_fill()
for _ in range(num_sides):
turt.forward(side_length)
turt.left(angle)
if fill_color:
turt.end_fill()
# Setup screen
screen = turtle.Screen()
screen.setup(width=700, height=500)
screen.title("Python Turtle - Multiple Hexagons")
screen.bgcolor("lightblue") # Set background color
# Create a turtle
my_turtle = turtle.Turtle()
my_turtle.speed(8)
my_turtle.shape("arrow")
# Draw a red hexagon
my_turtle.penup()
my_turtle.goto(-150, 0)
my_turtle.pendown()
draw_polygon(my_turtle, 6, 80, "red", "lightcoral")
# Draw a green hexagon
my_turtle.penup()
my_turtle.goto(50, 0)
my_turtle.pendown()
draw_polygon(my_turtle, 6, 80, "green", "lightgreen")
screen.mainloop()
By leveraging functions and loops, drawing shapes like hexagons becomes a "nice and easy" task in Python Turtle, allowing for creative and complex designs while ensuring the turtle always "finishes off back where our turtle was at the beginning" for perfect closed shapes.