In this post, I will share a source code to draw iron man helmet using python turtle module.

You can also check our previous article 👉 Top 5 Awesome Python Turtle Graphics

I found this awesome code that draws an Iron Man helmet using Turtle graphics. Let’s first see the visual output, and then I’ll share the source code and explain it to you.

This is the output that we get. Now I know you’re excited to try it by yourself and see the output. So, here is the source code for it.

Source Code

import turtle

# Define the coordinates for the parts of the Iron Man helmet
part_1 = [
    [(-40, 120), (-70, 260), (-130, 230), (-170, 200), (-170, 100), (-160, 40), (-170, 10), (-150, -10), (-140, 10), (-40, -20), (0, -20)],
    [(0, -20), (40, -20), (140, 10), (150, -10), (170, 10), (160, 40), (170, 100), (170, 200), (130, 230), (70, 260), (40, 120), (0, 120)]
]

part_2 = [
    [(-40, -30), (-50, -40), (-100, -46), (-130, -40), (-176, 0), (-186, -30), (-186, -40), (-120, -170), (-110, -210), (-80, -230), (-64, -210), (0, -210)],
    [(0, -210), (64, -210), (80, -230), (110, -210), (120, -170), (186, -40), (186, -30), (176, 0), (130, -40), (100, -46), (50, -40), (40, -30), (0, -30)]
]

Part_3 = [
    [(-60, -220), (-80, -240), (-110, -220), (-120, -250), (-90, -280), (-60, -260), (-30, -260), (-20, -250), (0, -250)],
    [(0, -250), (20, -250), (30, -260), (60, -260), (90, -280), (120, -250), (110, -220), (80, -240), (60, -220), (0, -220)]
]

# Set up the Turtle environment
turtle.hideturtle()
turtle.bgcolor('black')
turtle.setup(500, 600)
turtle.title("Iron Man")

# Define the initial positions for each part of the drawing
part_1Goto = (0, 120)
part_2Goto = (0, -30)
Part_3Goto = (0, -220)

turtle.speed(2)

# Function to draw parts of the Iron Man helmet
def draw_parts(parts, partsGoto):
    turtle.penup()
    turtle.goto(partsGoto)
    turtle.pendown()
    turtle.color('lightblue')
    turtle.begin_fill()
    
    for i in range(len(parts[0])):
        x, y = parts[0][i]
        turtle.goto(x, y)
    
    for i in range(len(parts[1])):
        x, y = parts[1][i]
        turtle.goto(x, y)
    
    turtle.end_fill()

# Draw each part of the Iron Man helmet
draw_parts(part_1, part_1Goto)
draw_parts(part_2, part_2Goto)
draw_parts(Part_3, Part_3Goto)

# Finalize the drawing
turtle.hideturtle()
turtle.done()

Explanation of the code

Here’s a step-by-step explanation of the code:

Importing the Turtle Library

First, we need to import the turtle library, which provides us with the necessary functions to create our drawing.

import turtle

Defining the Parts of the Drawing

Next, we define the coordinates for the different parts of our drawing. Each part is represented by a list of coordinate tuples.

part_1 = [
    [(-40, 120), (-70, 260), (-130, 230), (-170, 200), (-170, 100), (-160, 40), (-170, 10), (-150, -10), (-140, 10), (-40, -20), (0, -20)],
    [(0, -20), (40, -20), (140, 10), (150, -10), (170, 10), (160, 40), (170, 100), (170, 200), (130, 230), (70, 260), (40, 120), (0, 120)]
]

part_2 = [
    [(-40, -30), (-50, -40), (-100, -46), (-130, -40), (-176, 0), (-186, -30), (-186, -40), (-120, -170), (-110, -210), (-80, -230), (-64, -210), (0, -210)],
    [(0, -210), (64, -210), (80, -230), (110, -210), (120, -170), (186, -40), (186, -30), (176, 0), (130, -40), (100, -46), (50, -40), (40, -30), (0, -30)]
]

Part_3 = [
    [(-60, -220), (-80, -240), (-110, -220), (-120, -250), (-90, -280), (-60, -260), (-30, -260), (-20, -250), (0, -250)],
    [(0, -250), (20, -250), (30, -260), (60, -260), (90, -280), (120, -250), (110, -220), (80, -240), (60, -220), (0, -220)]
]

Setting up the Turtle Environment

We set up the turtle environment by configuring the window size, background color, and title.

turtle.hideturtle()
turtle.bgcolor('black')
turtle.setup(500, 600)
turtle.title("Iron Man")

turtle.hideturtle(): This function hides the turtle cursor, making the drawing process cleaner by not displaying the turtle icon as it moves.

Initial Positions for Parts

We define the starting positions for each part of the drawing.

part_1Goto = (0, 120)
part_2Goto = (0, -30)
Part_3Goto = (0, -220)

Drawing Function

We create a function draw_parts to draw each part of the image based on the coordinates provided.

turtle.speed(2)

def draw_parts(parts, partsGoto):
    turtle.penup()
    turtle.goto(partsGoto)
    turtle.pendown()
    turtle.color('lightblue')
    turtle.begin_fill()
    
    for i in range(len(parts[0])):
        x, y = parts[0][i]
        turtle.goto(x, y)
    
    for i in range(len(parts[1])):
        x, y = parts[1][i]
        turtle.goto(x, y)
    
    turtle.end_fill()

turtle.speed(2): Sets the drawing speed.

draw_parts(parts, partsGoto): This function takes the coordinates (parts) and starting position (partsGoto) to draw the parts.

Drawing the Parts

We use the draw_parts function to draw each part of the Iron Man image.

draw_parts(part_1, part_1Goto)
draw_parts(part_2, part_2Goto)
draw_parts(Part_3, Part_3Goto)

Finalizing the Drawing

Finally, we hide the turtle cursor and signal the end of the turtle graphics program.

turtle.hideturtle()
turtle.done()

turtle.hideturtle(): This ensures the turtle cursor is hidden again after the drawing is complete.

turtle.done(): Signals the end of the turtle graphics program, keeping the window open to view the final drawing.

Author

Hi, I'm Yagyavendra Tiwari, a computer engineer with a strong passion for programming. I'm excited to share my programming knowledge with everyone here and help educate others in this field.

Write A Comment