首页 > 编程语言 >实验6 turtle绘图和Python库应用编程体验

实验6 turtle绘图和Python库应用编程体验

时间:2023-06-06 13:12:10浏览次数:37  
标签:turtle 10 moveto 20 Python range 绘图 15 def

实验任务1

task1_1.py

源代码

 1 from turtle import*
 2 
 3 def move(x,y):
 4     penup()
 5     goto(x,y)
 6     pendown()
 7 
 8 def draw(n,size=100):
 9     for i in range(n):
10         fd(size)
11         left(360/n)
12 
13 def main():
14     pensize(2)
15     pencolor('red')
16 
17     move(-200,0)
18     draw(3)
19 
20     move(0,0)
21     draw(4)
22 
23     move(200,0)
24     draw(5)
25 
26     hideturtle()
27     done()
28 
29 main()

运行截图

task1_2.py

源代码

 1 from turtle import*
 2 
 3 def moveto(x,y):
 4     penup()
 5     goto(x,y)
 6     pendown()
 7 
 8 def main():
 9     pensize(2)
10     pencolor('blue')
11 
12     moveto(-150,0)
13     circle(50)
14 
15     moveto(0,0)
16     circle(50,steps=4)
17 
18     moveto(150,0)
19     circle(50,steps=5)
20 
21     moveto(300,0)
22     circle(50,steps=6)
23 
24     hideturtle()
25     done
26 
27 main()

运行截图

实验任务2

task2_1.py

源代码

 1 from turtle import*
 2 
 3 def moveto(x,y):
 4     penup()
 5     goto(x,y)
 6     pendown()
 7 
 8 def main():
 9     setup(800,600)
10 
11     radius=20
12     offset=20
13 
14     for i in range(9):
15         moveto(0,-radius)
16         circle(radius)
17         radius+=offset
18 
19     hideturtle()
20     done()
21 
22 main()

运行截图

task2_2

源代码

 1 from turtle import*
 2 from random import random
 3 
 4 def moveto(x,y):
 5     penup()
 6     goto(x,y)
 7     pendown()
 8 
 9 def gen_color():
10     return tuple((random() for i in range(3)))
11 
12 def main():
13     setup(800,600)
14 
15     radius=180
16     offset=20
17 
18     for i in range(8):
19         moveto(0,-radius)
20         color(gen_color())
21 
22         begin_fill()
23         circle(radius)
24         end_fill()
25 
26         radius-=offset
27 
28     hideturtle()
29     done()
30 
31 main()

运行截图

实验任务3

task3_1.py

源代码

 1 from turtle import*
 2 
 3 def square(size=50,rgb='pink'):
 4     pencolor(rgb)
 5     for i in range(4):
 6         fd(size)
 7         left(90)
 8 
 9 def main():
10     setup(800,600)
11     speed(0)
12 
13     n=10
14     for i in range(n):
15         square(80)
16         left(360/n)
17 
18     hideturtle()
19     done()
20 
21 main()

运行截图

task3_2.py

源代码

 1 from turtle import*
 2 
 3 setup(800,600)
 4 pencolor('pink')
 5 
 6 n=10
 7 for i in range(10):
 8     for j in range(2):
 9         circle(80,90)
10         left(90)
11 
12     right(360/n)
13 
14 hideturtle()
15 done()

运行截图

实验任务4

源代码

 1 from turtle import*
 2 
 3 setup(800,600)
 4 bgcolor('black')
 5 pencolor('white')
 6 speed(0)
 7 
 8 angle=0
 9 size=2
10 
11 n=5
12 count=50
13 for i in range(count):
14     fd(size)
15     angle+=360/n
16     seth(angle)
17     size+=5
18 
19 hideturtle()
20 done()

运行截图

实验任务5

task5_1.py

源代码

 1 from turtle import*
 2 
 3 def moveto(x,y):
 4     penup()
 5     goto(x,y)
 6     pendown()
 7 
 8 color('black')
 9 moveto(-100,-100)
10 begin_fill()
11 for i in range(4):
12     fd(200)
13     left(90)
14 end_fill()
15 
16 color('red')
17 moveto(100,0)
18 begin_fill()
19 left(135)
20 for i in range(4):
21     fd(100*(2**0.5))
22     left(90)
23 end_fill()
24 
25 hideturtle()
26 down()

运行截图

task5_2.py

源代码 

 1 from turtle import*
 2 
 3 pensize(2)
 4 pencolor('blue')
 5 speed=0
 6 
 7 def moveto(x,y):
 8     penup()
 9     goto(x,y)
10     pendown()
11 
12 
13 x=0
14 for i in range(5):
15     moveto(40+x,0)
16     left(90)
17     circle(40+x,90)
18     left(90)
19     fd(40+x)
20     left(90)
21     fd(40+x)
22     x+=20
23 
24 hideturtle()
25 down()

运行截图

实验任务6

task6_1

源代码

 1 from turtle import*
 2 from random import random
 3 
 4 def moveto(x,y):
 5     penup()
 6     goto(x,y)
 7     pendown()
 8 
 9 def gen_color():
10     return tuple((random() for i in range(3)))
11 
12 setup(800,600)
13 x=0
14 for i in range(20):
15     moveto(-200+x,0)
16     begin_fill()
17     color(gen_color())
18     for j in range(2):
19         fd(20)
20         left(90)
21         fd(60)
22         left(90)
23     end_fill()
24     x+=20
25 
26 hideturtle()
27 done()

运行截图

task6_2

源代码

 1 setup(800,600)
 2 
 3 def moveto(x,y):
 4     penup()
 5     goto(x,y)
 6     pendown()
 7 
 8 radius = 150
 9 color('yellow')
10 begin_fill()
11 moveto(radius/2**0.5,radius/2**0.5)
12 left(135)
13 circle(150,360-90)
14 end_fill()
15 
16 radius1 = 15
17 color('black')
18 begin_fill()
19 moveto(0,150/2)
20 circle(15)
21 end_fill()
22 
23 radius2 = 5
24 color('white')
25 begin_fill()
26 moveto(-15,165/2)
27 circle(5)
28 end_fill()
29 
30 hideturtle()
31 done()

运行截图

 

标签:turtle,10,moveto,20,Python,range,绘图,15,def
From: https://www.cnblogs.com/zyj-/p/17458531.html

相关文章

  • 如何在Python中使用JSON模块
    JSON(JavaScriptObjectNotation)是一种流行的轻量级数据交换标准。它表示由键值对组成的数据结构,非常简单易懂。JSON已成为在线服务之间数据交换的行业标准。它广泛用于现代编程语言,包括Python。JSON数据经常表示为嵌套字典、列表和标量值,例如文本、数字、布尔值和空值。之所......
  • python离线下载安装包
    1.背景内网服务器不能直接连接外网,但是需要Python的mysql-connector-2.1.7包2.步骤#下载相关tar包https://pypi.doubanio.com/simple/mysql-connector/mysql-connector-2.1.7.tar.gz#上传到服务器后,解压tar-zxvfmysql-connector-2.1.7.tar.gz#进入解压目录,安装cdm......
  • python redis 链接集群 阿里云集群
    前言集群redis不支持选dbcluster方法里没有支持选中db的选项,javapy都不行#pipinstallredis==3.5.3#pipinstallredis-py-cluster==2.1.3#亲测,我是使用的这两个版本进行处理的fromredisclusterimportRedisClusternodes=[{"host":"dsfwwqfggy65aadfggi.redis.r......
  • python中同时指定多个分隔符将字符串拆分为列表
     001、>>>str1="ab_cdef_ghij_kl"##测试字符串>>>str1.split("")##一句空格进行拆分['ab_cd','ef_gh','ij_kl']>>>importre>>>re.split("......
  • Centos7 离线编译安装python3
    一,安装依赖yum-yinstallzlib-develbzip2-developenssl-develncurses-develreadline-develtk-develgccmake安装libffi-devel依赖yuminstalllibffi-devel-y注意:如果不安装这个包,python3可以装成功,但是后面装flask、uwsgi等依赖python3中有个内置模块叫ctype......
  • ARM架构---Python环境部署
    ARM架构---Python环境部署编译方式百度下即可,在ARM服务器编译出来就可以用1、上传python37.tar.gz文件到服务器py环境是在ARM架构上编译好的,可以直接拿编译产物去运行#例如上传到/data/software/目录cd/data/software/#解压tar-xfpython37.tar.gz#做软链接......
  • 万能的Python爬虫模板来了
    Python是一种非常适合用于编写网络爬虫的编程语言。以下是一些Python爬虫的基本步骤:1、导入所需的库:通常需要使用requests、BeautifulSoup、re等库来进行网络请求、解析HTML页面和正则表达式匹配等操作。2、发送网络请求:使用requests库发送HTTP请求,获取目标网页的HTML源代码。3、解......
  • 《深度剖析CPython解释器》19. Python类机制的深度解析(第三部分): 自定义类的底层实
    https://www.cnblogs.com/traditional/p/13593927.html楔子Python除了给我提供了很多的类之外,还支持我们定义属于自己的类,那么Python底层是如何做的呢?我们下面就来看看。自定义class老规矩,如果想知道底层是怎么做的,那么就必须要通过观察字节码来实现。classGirl:nam......
  • Python Exit——如何在Python中使用Exit函数来停止程序
    Python中的函数exit()用于退出或终止当前运行的脚本或程序。您可以使用它随时停止程序的执行。当exit()函数被调用时,程序会立即停止运行并退出。该函数的语法exit()是: exit([status])这里,status是一个可选参数,表示程序的退出状态。退出状态是一个整数值,表示程序终止的原因。按......
  • 实验6 turtle绘图与python库应用编程体验
    实验任务1task1_11fromturtleimport*23defmoveto(x,y):4'''5画笔移动到坐标(x,y)处6'''7penup()8goto(x,y)9pendown()1011defdraw(n,size=100):12'''13绘制边长为s......