在 Python 中使用 Turtle 绘制汽车 – 码微 | 您所在的位置:网站首页 › 五菱汽车简笔画怎么画 › 在 Python 中使用 Turtle 绘制汽车 – 码微 |
嘿,编码员!在本教程中,我将教您如何在 python 海龟库的帮助下绘制自己的汽车。如果您不知道该 模块,请查看此处的turtle 教程 。 使用 Python Turtle 绘制汽车的步骤您只需导入Python自带的turtle库即可,无需进行任何额外安装。 1 import turtle下一步涉及创建一个应用程序屏幕来绘制汽车。我们可以为窗口命名任何我们想要的名称。在本教程中,我们将屏幕的名称保留为 car_scr。 下面的代码执行屏幕的创建和自定义,包括屏幕和笔的颜色。 1 2 3 4 import turtle car_scr = turtle car_scr.color('black') car_scr.Screen().bgcolor("#ABEBC6")现在让我们创建一个为我们绘制汽车的函数。汽车有许多不同的部件,例如上半身、车轮和车窗。我们将逐一分别绘制它们。 在函数内部 Draw_Car,我们需要将起始坐标作为参数提供给函数。然后该 goto 函数将指针指向我们传递给该函数的位置。 您可能熟悉 Draw_Car 函数中使用的函数。我将在下面提到每个方法的工作原理: penup & pendown – 控制何时绘制和何时不绘制。 fillcolor、 begin_fill 和 end_fill – 控制特定图形的颜色 forward & backward 和 left & right – 有助于以特定方向或角度在屏幕上绘图。 使用 Python Turtle 创建汽车底座让我们首先使用下面的代码绘制汽车的上半身。看看输出结果有多么出色。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 def Draw_Car(i,j): # Drawing the Upper Body car_scr.fillcolor("#2980B9") car_scr.penup() car_scr.goto(i,j) car_scr.pendown() car_scr.begin_fill() car_scr.forward(370) car_scr.left(90) car_scr.forward(50) car_scr.left(90) car_scr.forward(370) car_scr.left(90) car_scr.forward(50) car_scr.end_fill() 画车上半身 画车窗我们将调用该函数并传递起始 x 和 y 值。我们将该函数称为 Draw_Car(-200,0)。现在我们将使用与上面所示非常相似的代码来绘制窗口。看看下面的代码。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 号 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 def Draw_Car(i,j): # Drawing the Upper Body car_scr.fillcolor("#2980B9") car_scr.penup() car_scr.goto(i,j) car_scr.pendown() car_scr.begin_fill() car_scr.forward(370) car_scr.left(90) car_scr.forward(50) car_scr.left(90) car_scr.forward(370) car_scr.left(90) car_scr.forward(50) car_scr.end_fill() #Draw the Windows car_scr.fillcolor("#D5DBDB") car_scr.penup() car_scr.goto(i+100, 50) car_scr.pendown() car_scr.begin_fill() car_scr.setheading(45) car_scr.forward(70) car_scr.setheading(0) car_scr.forward(100) car_scr.setheading(-45) car_scr.forward(70) car_scr.setheading(90) car_scr.end_fill() car_scr.penup() car_scr.goto(i+200, 50) car_scr.pendown() car_scr.forward(49.50)当我们执行此代码时,我们会得到以下屏幕作为输出。相当惊人吧?! 绘制汽车上车身窗口 使用 Turtle 添加车轮最后,我们需要以类似的方式为汽车添加轮子。看看下面的完整功能就知道了。最后,我们将隐藏乌龟指针以查看干净的汽车图像! 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 号 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 def Draw_Car(i,j): # Drawing the Upper Body car_scr.fillcolor("#2980B9") car_scr.penup() car_scr.goto(i,j) car_scr.pendown() car_scr.begin_fill() car_scr.forward(370) car_scr.left(90) car_scr.forward(50) car_scr.left(90) car_scr.forward(370) car_scr.left(90) car_scr.forward(50) car_scr.end_fill() #Draw the Windows car_scr.fillcolor("#D5DBDB") car_scr.penup() car_scr.goto(i+100, 50) car_scr.pendown() car_scr.begin_fill() car_scr.setheading(45) car_scr.forward(70) car_scr.setheading(0) car_scr.forward(100) car_scr.setheading(-45) car_scr.forward(70) car_scr.setheading(90) car_scr.end_fill() car_scr.penup() car_scr.goto(i+200, 50) car_scr.pendown() car_scr.forward(49.50) # Draw the two wheels car_scr.penup() car_scr.goto(i+100, -10-j) car_scr.pendown() car_scr.color('black') car_scr.fillcolor('black') car_scr.begin_fill() car_scr.circle(20) car_scr.end_fill() car_scr.penup() car_scr.goto(i+300, -10-j) car_scr.pendown() car_scr.color('black') car_scr.fillcolor('black') car_scr.begin_fill() car_scr.circle(20) car_scr.end_fill() car_scr.hideturtle() 运行 Python 海龟让我们使用下面的代码在屏幕上绘制汽车。完成汽车绘制后,我们将借助该 done 功能关闭应用程序屏幕。 Draw_Car(-200,0) car_scr.done() 画车整车 绘制汽车的完整 Python Turtle 代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 号 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 import turtle car_scr = turtle car_scr.color('black') car_scr.Screen().bgcolor("#ABEBC6") def Draw_Car(i,j): # Drawing the Upper Body car_scr.fillcolor("#2980B9") car_scr.penup() car_scr.goto(i,j) car_scr.pendown() car_scr.begin_fill() car_scr.forward(370) car_scr.left(90) car_scr.forward(50) car_scr.left(90) car_scr.forward(370) car_scr.left(90) car_scr.forward(50) car_scr.end_fill() #Draw the Windows car_scr.fillcolor("#D5DBDB") car_scr.penup() car_scr.goto(i+100, 50) car_scr.pendown() car_scr.begin_fill() car_scr.setheading(45) car_scr.forward(70) car_scr.setheading(0) car_scr.forward(100) car_scr.setheading(-45) car_scr.forward(70) car_scr.setheading(90) car_scr.end_fill() car_scr.penup() car_scr.goto(i+200, 50) car_scr.pendown() car_scr.forward(49.50) # Draw the two wheels car_scr.penup() car_scr.goto(i+100, -10-j) car_scr.pendown() car_scr.color('black') car_scr.fillcolor('black') car_scr.begin_fill() car_scr.circle(20) car_scr.end_fill() car_scr.penup() car_scr.goto(i+300, -10-j) car_scr.pendown() car_scr.color('black') car_scr.fillcolor('black') car_scr.begin_fill() car_scr.circle(20) car_scr.end_fill() Draw_Car(-200,0) car_scr.done()当我们执行上面的代码时,系统屏幕上会出现一个新屏幕,汽车开始在应用程序的屏幕上绘制。 结论恭喜!现在您知道如何使用 Python 编程语言中的 Turtle 库在屏幕上绘制汽车。感谢您的阅读! 如果您喜欢本教程,我建议您也阅读以下教程: 如何使用 Tkinter 绘制不同的形状 使用 Tkinter 绘制线条 – 初学者基础知识 使用 OpenCV 绘制形状 – 完整的操作指南继续阅读以了解更多信息!快乐编码!😄 |
CopyRight 2018-2019 实验室设备网 版权所有 |