Step-by-Step Python Car Program with Turtle

Here’s a Python program that demonstrates how to draw a car using the Turtle module. We’ll go through each part of the code to understand the drawing process.

import turtle

 Initialize turtle object
car = turtle.Turtle()

 Draw the rectangular body
car.color('#008000')  # Green color
car.fillcolor('#008000') # Fill with green
car.penup()
car.goto(0,0) # Starting position
car.pendown()
car.begin_fill()
car.forward(370) # Length of the body
car.left(90)
car.forward(50)  # Height of the body
car.left(90)
car.forward(370)
car.left(90)
car.forward(50)
car.end_fill()

 Draw window and roof (trapezoid shape)
car.penup()
car.goto(100, 50) # Position for roof
car.pendown()
car.setheading(45) # Angle for trapezoid side
car.forward(70)
car.setheading(0)  # Straight line for roof top
car.forward(100)
car.setheading(-45) # Angle for other trapezoid side
car.forward(70)
car.setheading(90)
car.penup()
car.goto(200, 50) # Small vertical line for window separation
car.pendown()
car.forward(49.50)

 Draw the tyres (circles)
car.penup()
car.goto(100, -10) # Position for first tyre
car.pendown()
car.color('#000000') # Black color for tyres
car.fillcolor('#000000') # Fill with black
car.begin_fill()
car.circle(20)       # Tyre radius
car.end_fill()

car.penup()
car.goto(300, -10) # Position for second tyre
car.pendown()
car.color('#000000')
car.fillcolor('#000000')
car.begin_fill()
car.circle(20)       # Tyre radius
car.end_fill()

car.hideturtle()     # Hide turtle after drawing
turtle.done() # Keep window open until closed manually

This Python code will guide the turtle to draw a car step-by-step. Let’s break down the code into sections for better understanding.

Drawing the Car Body

The initial part of the code focuses on creating the main body of the car, which is essentially a green rectangle.

 Below code for drawing rectangular upper body
car.color('#008000')
car.fillcolor('#008000')
car.penup()
car.goto(0,0)
car.pendown()
car.begin_fill()
car.forward(370)
car.left(90)
car.forward(50)
car.left(90)
car.forward(370)
car.left(90)
car.forward(50)
car.end_fill()
  • car.color('#008000') and car.fillcolor('#008000'): Sets the drawing color and fill color to green.
  • car.penup() and car.goto(0,0) and car.pendown(): Lifts the pen up, moves the turtle to the starting coordinates (0,0), and then puts the pen down to start drawing.
  • car.begin_fill() and car.end_fill(): Commands to fill the shape with the selected fillcolor.
  • car.forward(370), car.left(90), car.forward(50), car.left(90), car.forward(370), car.left(90), car.forward(50): These lines instruct the turtle to move forward and turn left by 90 degrees, effectively drawing a rectangle.

Creating the Roof and Windows

Next, we add the roof and windows, shaped like a trapezoid, on top of the rectangular body.

 Below code for drawing window and roof
car.penup()
car.goto(100, 50)
car.pendown()
car.setheading(45)
car.forward(70)
car.setheading(0)
car.forward(100)
car.setheading(-45)
car.forward(70)
car.setheading(90)
car.penup()
car.goto(200, 50)
car.pendown()
car.forward(49.50)
  • car.penup() and car.goto(100, 50) and car.pendown(): Repositions the turtle to start drawing the roof shape.
  • car.setheading(45), car.forward(70), etc.: These commands draw the trapezoid shape by setting angles and moving forward, creating the roof and window section of the car. The small vertical line adds a subtle division for the window.

Adding the Tyres

Finally, we draw the tyres using circles to complete our car illustration.

 Below code for drawing two tyres
car.penup()
car.goto(100, -10)
car.pendown()
car.color('#000000')
car.fillcolor('#000000')
car.begin_fill()
car.circle(20)
car.end_fill()

car.penup()
car.goto(300, -10)
car.pendown()
car.color('#000000')
car.fillcolor('#000000')
car.begin_fill()
car.circle(20)
car.end_fill()
  • car.penup() and car.goto(100, -10) and car.pendown(): Moves the turtle to the position for the first tyre.
  • car.color('#000000') and car.fillcolor('#000000'): Sets the color to black for the tyres.
  • car.begin_fill(), car.circle(20), car.end_fill(): Draws a filled circle with a radius of 20, representing a tyre. This is repeated for the second tyre at a different x-coordinate (300).

Final Output of the Python Car Program

When you run the complete Python code, you will see a window pop up with a green car drawn on it, complete with a body, roof, windows, and tyres.

 Output of the Python Car Program will be a graphical window
 showing a green car with black tyres drawn by the turtle.

This “python car program” is a simple yet effective way to understand the basics of the Turtle module and graphical programming in Python. By modifying the code, you can experiment with different colors, shapes, and sizes to create your own unique vehicle designs. The Turtle module is a fantastic resource for learning programming concepts in a visually engaging way.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *