Categories
Python Turtle

How to Draw a Car using Python Turtle

Drawing with code in the Python programming language involves using the Python Turtle module. Python Turtle comes by default with the Python interpreter.

This tutorial covers how to draw a car using the Python Turtle module. Drawing the car involves the following steps:

  1. Get an image of the car you want to draw
  2. Get a grid for the picture of the car
  3. Combine the grid and picture of the car into a single image
  4. Draw center lines on the image of the car
  5. Draw the car using the Python Turtle module

Get an Image of the Car

You cannot replicate a photo using Python Turtle so we need to find a suitable icon.

A great source for icons is The Noun Project. Once you visit the site, you simply search for the kind of image you are looking for.

Get a Grid

Drawing the car with Python Turtle would be impossible without a grid. The grid gives you the coordinates of the points of the image that you want to draw.

I couldn’t find a suitable grid maker for my purposes so I used the grid from the Grid Drawing Tool. You customise the grid you want to your requirements and it gives you the image you want.

Combine the Grid

The grid from the Grid Drawing Tool is then taken and made transparent. The grid and image to be drawn are then combined in such a way that the image is on top of the grid.

A simple software for this is Paint.NET. It is a free software for image editing.

Draw Center Lines

The Python Turtle module has it origins at the points (0, 0). So to draw a image, you need its center lines.

This is easily done using the Paint software available on Windows. The image will now look like this:

Car image with center lines

Draw the Car

The car will be drawn in stages. We shall start from the leftmost position and draw the composite shapes that make up the car until we are done.

To start we shall import the Python Turtle module to our code, make use of the delay function and increase the pen size of our turtle.

Our code will look as shown below:

import turtle
turtle.delay(10)
turtle.pensize(20)

The resulting image is show below:

The turtle is displayed at the center of the screen

Next we move the turtle to the extreme left position. On the image with the center lines, this is (-8, -3). We want to draw with a scale factor of 25 so any coordinate we get, we will multiply by 25. The code to do this is shown below:

# Move the turtle to the extreme left position
turtle.penup()
turtle.setposition(-200,-75)

The resulting image is shown below:

The turtle has moved to the leftmost position

The goal now is to draw a rectangle of length 400 and width 150. The code to do this is show below:

# Draw the rectangle
turtle.pendown()
turtle.forward(400)
turtle.setheading(90)
turtle.forward(150)
turtle.setheading(180)
turtle.forward(400)
turtle.setheading(-90)
turtle.forward(150)

The resulting image is shown below:

The rectangle has been drawn

Next the left tyre should be drawn. To do this, we move the turtle to the leftmost position of the tyre which is at (-7, -3) scaled up to (-175, -75) and we draw the tyre.

The code to do this is shown below:

# Draw the left tire
turtle.penup()
turtle.setposition(-175,-75)
turtle.pendown()
turtle.forward(50)
turtle.setheading(0)
turtle.forward(75)
turtle.setheading(90)
turtle.forward(50)

The resulting image is shown below:

Left tyre drawn

Next the right tyre is drawn. To do so, we move the turtle to the position (100, -75) and we draw the right tyre.

The code to do this is shown below:

# Draw the right tire
turtle.penup()
turtle.setposition(100,-75)
turtle.pendown()
turtle.setheading(270)
turtle.forward(50)
turtle.setheading(0)
turtle.forward(75)
turtle.setheading(90)
turtle.forward(50)

The generated image is shown below:

Right tyre drawn

Now we intend to draw the uppermost line. The left coordinate of the line is at (-5.5, 4). Scaled up, this is (-137.5, 200). The goal would be to draw a line from that point to the point (137.5, 200).

The code to do this is shown below:

# Draw the uppermost line
turtle.penup()
turtle.setposition(-137.5,200)
turtle.pendown()
turtle.setheading(0)
turtle.forward(275)

The generated image is shown below:

Topmost upper line drawn

Next we draw the second uppermost line. To do so, we move the turtle to the position (-150, 150) and draw a line of length 300 pixels.

The code to do this is shown below:

# Draw the second uppermost line
turtle.penup()
turtle.setposition(-150,150)
turtle.pendown()
turtle.setheading(0)
turtle.forward(300)

The resulting image is shown below:

Second uppermost line drawn

To complete the outline of the car, we need to draw the left and right slanting lines.

To draw the left slanting line, we simply draw a line from the tip of the uppermost line to where the left slanting line should touch the rectangle.

The code to do this is shown below:

# Draw the left slanted line
turtle.penup()
turtle.setposition(-137.5,200)
turtle.pendown()
turtle.setposition(-175,75)

The generated image is shown below:

Left slanting line drawn

To draw the right slanting line, we draw a line from the tip of the topmost line to the point where the right slanting line touches the rectangle.

The code to do this is shown below:

# Draw the right slanted line
turtle.penup()
turtle.setposition(137.5,200)
turtle.pendown()
turtle.setposition(175,75)

The generated image is shown below:

Right slanting line drawn

Now we can draw the car proper. The headlamps will be drawn first. The left headlamp is to be drawn first as a circle.

The code to do this is shown below:

# Draw the left circle
turtle.penup()
turtle.setposition(-125,-25)
turtle.pendown()
turtle.circle(25)

The generated image is shown below:

Left headlamp drawn

Next we draw the right headlamp. The code to do this is shown below:

# Draw the right circle
turtle.penup()
turtle.setposition(125,-25)
turtle.pendown()
turtle.circle(25)

The generated image is shown below:

Right headlamp drawn

Now the wipers are to be drawn. The left wiper will be drawn first. THe code to do this is shown below:

# Draw the left wiper
turtle.penup()
turtle.setposition(-87.5, 75)
turtle.pendown()
turtle.setposition(-50, 112.5)

The generated image is shown below:

Left wiper drawn

Next we draw the right wiper. The code to do this is shown below:

# Draw the right wiper
turtle.penup()
turtle.setposition(62.5, 75)
turtle.pendown()
turtle.setposition(100, 112.5)

The generated image is shown below:

Right wiper drawn

Finally the 4 horizontal lines are drawn. The code to do this is shown below:

# Draw the horizontal lines
turtle.penup()
turtle.setposition(-50, -37.5)
turtle.pendown()
turtle.setposition(-50, 0)
turtle.penup()
turtle.setposition(-15, -37.5)
turtle.pendown()
turtle.setposition(-15, 0)
turtle.penup()
turtle.setposition(15, -37.5)
turtle.pendown()
turtle.setposition(15, 0)
turtle.penup()
turtle.setposition(50, -37.5)
turtle.pendown()
turtle.setposition(50, 0)

The generated image is shown below:

Car completed

At the end of this tutorial, we have successfully drawn a car using the Python Turtle module in the Python programming language.

The code in its entirety is available as a GitHub Gist. You can find it here.

Learn Python in One Week

If you want a quick and easy introduction to the Python programming language, you should check out my fifth book: Learn Python in One Week on the Amazon store.