- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Draw a car using Turtle in Python
Turtle is a Python module that is used to create graphics, images, and animation using simple commands. Turtle is a simple and easy tool for beginners to learn programming by creating exciting visuals with it. In this article, we will create a car using a turtle in Python.
Step 1: Installing Turtle in Python
Before we dive into the code, we need to make sure that the Turtle module is installed in our Python environment. Use the Python package manager to install the Turtle module. To install the turtle module type the following command in your terminal.
pip install turtle
Step 2: Importing Turtle
After installing the turtle module you can import turtle using the import command in Python.
import turtle
Step 3: Creating a turtle object
After importing Turtle, we need to create a Turtle object to draw with. We can do this by calling the Turtle() function −
t = turtle.Turtle()
Step 4: Drawing a Car
Once we have created a turtle object, now we can use that to create the body of the car. we will move the turtle forward and backward to create the top and bottom of the car, and then turn it to create the sides. Then we can draw the front wheel, back wheel, and window of the car.
# Draw the top of the car t.penup() t.goto(0, 50) t.pendown() t.goto(0, 100) # Draw the bottom of the car t.penup() t.goto(0, 50) t.pendown() t.goto(75, 50) t.goto(100, 75) # Draw the sides of the car t.penup() t.goto(0, 100) t.pendown() t.goto(75, 100) t.goto(100, 75) # Draw the front wheel t.penup() t.goto(25, 25) t.pendown() t.circle(25) # Draw the back wheel t.penup() t.goto(75, 25) t.pendown() t.circle(25) # Draw the windows t.penup() t.goto(15, 100) t.pendown() t.goto(60, 100) t.goto(75, 75) t.goto(60, 50) t.goto(15, 50) t.goto(0, 75) t.goto(15, 100) # Draw the headlights t.penup() t.goto(90, 75) t.dot(10)
Step 5: Adding Color
We can make the car look more realistic by adding color to the car. We can do this by using the fillcolor() and begin_fill() methods to fill the car with a specific color. We can also use the pencolor() method to change the color of the pen used to draw the car.
t.fillcolor("red") t.begin_fill() # Draw the body of the car # ... t.end_fill() t.fillcolor("black") t.begin_fill() # Draw the wheels of the car # ... t.end_fill() t.pencolor("white") # Draw the windows and headlights of the car # ...
Step 6: Completing the car
We can use the hideturtle() method to hide the turtle and the done() method to keep the window open until the user closes it.
t.hideturtle() turtle.done()
Here is the complete code for drawing a car using Turtle in Python
import turtle # Create a Turtle object t = turtle.Turtle() # Draw the body of the car t.penup() t.goto(0, 50) t.pendown() t.goto(0, 100) t.penup() t.goto(0, 50) t.pendown() t.goto(75, 50) t.goto(100, 75) t.penup() t.goto(0, 100) t.pendown() t.goto(75, 100) t.goto(100, 75) # Draw the wheels of the car t.penup() t.goto(25, 25) t.pendown() t.circle(25) t.penup() t.goto(75, 25) t.pendown() t.circle(25) # Draw the windows and headlights of the car t.penup() t.goto(15, 100) t.pendown() t.goto(60, 100) t.goto(75, 75) t.goto(60, 50) t.goto(15, 50) t.goto(0, 75) t.goto(15, 100) t.penup() t.goto(90, 75) t.dot(10) # Add color to the car t.fillcolor("red") t.begin_fill() t.penup() t.goto(0, 50) t.pendown() t.goto(0, 100) t.goto(75, 100) t.goto(100, 75) t.goto(75, 50) t.goto(0, 50) t.end_fill() t.fillcolor("black") t.begin_fill() t.penup() t.goto(25, 25) t.pendown() t.circle(25) t.penup() t.goto(75, 25) t.pendown() t.circle(25) t.end_fill() t.pencolor("white") # draw windows t.penup() t.goto(15, 50) t.pendown() t.goto(60, 50) t.penup() t.goto(15, 100) t.pendown() t.goto(60, 100) #hide the turtle t.hideturtle() turtle.done()
Output
Conclusion
Turtle graphics is a simple and fun way to learn programming, and it is an excellent tool for teaching kids about computer science. In this article, we have shown you how to draw a car using Turtle in Python. We have covered the basic concepts of Turtle graphics, such as creating a Turtle object, moving the turtle, drawing shapes, and adding color. With this knowledge, you can create more complex and interesting designs using Turtle graphics in Python.