在Python中使用Turtle绘制汽车

在Python中使用Turtle绘制汽车

在Python中,有许多描述图形插图的模块,其中之一是turtle,它是Python中的一个内置模块,让用户控制一支笔(turtle)在屏幕(画板)上作画。它主要用于说明数字、形状、设计等。在这篇文章中,我们将学习如何使用turtle模块来画一辆车。

在Python中使用Turtle模块来画一辆汽车:

1.我们将使用Turtle模块创建不同的形状,以说明一辆汽车。
2.轮胎可以用circle()函数来绘制。
3.上半身可以被认为是一个长方形。
4.屋顶和窗户类似于梯形。
5.将上述所有的形状在特定的位置上重叠起来,就可以说明一辆汽车。

让我们在以下程序的帮助下尝试更好地理解它:

#Python program to draw car in turtle programming
 
# Import required library
import turtle
  
   
car = turtle.Turtle()
 
 
# 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()
  
   
# 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)
  
   
# 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.hideturtle()

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程