1. 本际云推荐 - 专业推荐VPS、服务器,IDC点评首页
  2. 云主机运维
  3. VPS运维

如何使用Python+turtle绘制图形实例代码

python turtle绘图实例

作为Python从业者,要提升自己,就需要多阅读一些书籍多去找一些资料,然后去进行阅读。今天,小编就来给大家介绍一下如何利用python里面的turtle来绘制图形的实例代码。

如何使用Python+turtle绘制图形实例代码

图一

下面这张图是由拼接而成的,感兴趣的朋友可以自行操作一下。具体代码如下:

#-*-coding:UTF-8-*-
import os
import time
import pygame
import turtle as t
t.title(‘阿黎逸阳的代码公众号’)
t.speed(10)
t.setup(startx=0,starty=0,width=800,height=600)
#第一幅图
def w_sg1(theta):
    t.setheading(theta)
    t.color(‘green’)
    t.begin_fill()
    t.forward(60)
    t.left(100)
    t.forward(20)
    t.left(100)
    t.forward(60)
    t.end_fill()
for i in range(8):
    w_sg1(70+i*45)
t.hideturtle()

图二

这张图是风火轮性质的,具体代码操作如下所示:

#-*-coding:UTF-8-*-
import os
import time
import pygame
import turtle as t
t.title(‘阿黎逸阳的代码公众号’)
t.speed(10)
t.setup(startx=0,starty=0,width=800,height=600)
#第二幅图
def w_sg2(theta):
    t.setheading(theta)
    t.color(‘green’)
    t.begin_fill()
    t.forward(55)
    t.left(100)
    t.forward(20)
    t.left(100)
    t.forward(60)
    t.end_fill()
for i in range(24):
    w_sg2(70+i*15)
t.hideturtle()

图三

下面这张图是八叶画花,可以尝试去改变一下颜色。具体代码如下:

#-*-coding:UTF-8-*-
import os
import time
import pygame
import turtle as t
t.title
t.speed(10)
t.setup(startx=0,starty=0,width=800,height=600)
#第三幅图
def w_sg3(theta):
    t.color(‘green’)
    t.begin_fill()
    t.setheading(theta)
    t.circle(80,50)
    t.left(130)
    t.circle(80,50)
    t.end_fill()
for i in range(8):
    w_sg3(30+i*45)
t.hideturtle()

图四

第四个图是16叶花,可以通过调整8叶花代码,绘制出如下图形。具体代码如下:

t.clearscreen()
#第四幅图
for i in range(16):
    w_sg3(30+i*30)
t.hideturtle()
t.goto(0,-3)
t.color(‘white’)
t.begin_fill()
t.circle(6,360)
t.end_fill()

图五

第五个图是小太阳,可以通过调整8叶花代码,绘制出如下图形。具体代码如下:

#-*-coding:UTF-8-*-
import os
import time
import pygame
import turtle as t
t.title(‘阿黎逸阳的代码公众号’)
t.speed(10)
t.setup(startx=0,starty=0,width=800,height=600)
#第五幅图
def w_sg3(theta):
    t.color(‘red’)
    t.begin_fill()
    t.setheading(theta)
    t.circle(80,50)
    t.left(130)
    t.circle(80,50)
    t.end_fill()
for i in range(24):
    w_sg3(30+i*15)
t.hideturtle()

图六

最后一张图是阴阳图,具体代码如下:

#-*-coding:UTF-8-*-
import os
import time
import pygame
import turtle as t
t.title(‘阿黎逸阳的代码公众号’)
t.speed(10)
t.setup(startx=0,starty=0,width=800,height=600)
#阴阳图
def w_sg4():
    t.color(‘black’)
    t.begin_fill()
    t.circle(80,360)
    t.end_fill()
    t.color(‘black’,’white’)
    t.begin_fill()
    t.circle(80,180)
    t.circle(40,180)
    t.circle(-40,180)
    t.end_fill()
    t.penup()
    t.goto(0,130)
    t.pendown()
    t.begin_fill()
    t.color(‘black’)
    t.circle(8,360)
    t.end_fill()
    t.penup()
    t.goto(0,130-90)
    t.pendown()
    t.begin_fill()
    t.color(‘white’)
    t.circle(8,360)
    t.end_fill()
    t.hideturtle()
w_sg4()

通过以上的几个例子,希望给大家带来一定的帮助。

原创文章,作者:小编小本本,如若转载,请注明出处:https://www.benjiyun.com/yunzhujiyunwei/vps-yunwei/7411.html