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

实验6 turtle绘图与python库应用编程体验

时间:2023-06-11 15:33:46浏览次数:40  
标签:turtle 10 moveto 20 python 绘图 fill def left

task1_1

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

 task1_2

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

 实验2

task2_1

 1 from turtle import *
 2 
 3 
 4 def moveto(x, y):
 5     penup()
 6     goto(x, y)
 7     pendown()
 8 
 9 
10 def main():
11     setup(800, 600)
12 
13     radius = 20     # 圆初始半径
14     offset = 20     # 同心圆每次位移量
15 
16     for i in range(9):
17         moveto(0, -radius)
18         circle(radius)
19         radius += offset
20 
21     hideturtle()
22     done()
23 
24 
25 main()
复制代码

 task2_2

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

 实验任务3:
task3_1

1 from turtle import *
 2 
 3 
 4 def square(size=50, rgb='pink'):
 5     """
 6     绘制正方形
 7     :para: size:int 指定边长 (如未指定参数,使用默认值)
 8     :para: c: str 指定画笔颜色值 (如未指定参数,使用默认值)
 9     """
10     pencolor(rgb)
11     for i in range(4):
12         fd(size)
13         left(90)
14 
15 
16 def main():
17     setup(800, 600)
18     speed(0)
19 
20     n = 10
21     for i in range(n):
22         square(80, rgb='orange')
23         left(360/n)
24 
25     hideturtle()
26     done()
27 
28 
29 main()

 task3_2

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

 实验任务4:
task4

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       # 螺旋n边形
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

 1 from turtle import *
 2 
 3 
 4 def moveto(x, y):
 5     penup()
 6     goto(x, y)
 7     pendown()
 8 
 9 
10 setup(800, 600)
11 speed(0)
12 moveto(-100, -100)
13 
14 
15 def fill_square(rgb, sl):
16     fillcolor(rgb)
17     begin_fill()
18     for i in range(4):
19         fd(sl)
20         left(90)
21     end_fill()
22     hideturtle()
23 
24 
25 fill_square('black', 200)
26 
27 moveto(0, -100)
28 left(45)
29 fill_square('red', 100*2**0.5)
30 
31 done()

 

1 from turtle import *
 2 
 3 setup(400, 400)
 4 pencolor('blue')
 5 pensize(2)
 6 speed(0)
 7 
 8 
 9 def func(n, r):
10     for i in range(n):
11         fd(r)
12         left(90)
13         circle(r, 90)
14         left(90)
15         fd(r)
16         left(90)
17         r += 20
18 
19 
20 func(5, 40)
21 hideturtle()
22 done()
复制代码

 实验任务6:
task6_1

 1 from turtle import *
 2 from random import random
 3 
 4 setup(800, 600)
 5 speed(0)
 6 penup()
 7 goto(-200, 0)
 8 pendown()
 9 
10 
11 def get_color():
12     return tuple(random() for i in range(3))
13 
14 
15 def rect(width, length):
16     for i in range(20):
17         rgb = get_color()
18         pencolor(rgb)
19         fillcolor(rgb)
20         begin_fill()
21         fd(width)
22         left(90)
23         fd(length)
24         left(90)
25         fd(width)
26         left(90)
27         fd(length)
28         left(90)
29         end_fill()
30         fd(width)
31 
32 
33 rect(20, 80)
34 hideturtle()
35 done()
复制代码

 

 1 from turtle import *
 2 
 3 setup(600, 600)
 4 bgcolor('black')
 5 fillcolor('yellow')
 6 speed(0)
 7 
 8 begin_fill()
 9 left(45)
10 fd(120)
11 left(90)
12 circle(120, 270)
13 left(90)
14 fd(120)
15 end_fill()
16 
17 
18 def moveto(x, y):
19     penup()
20     goto(x, y)
21     pendown()
22 
23 
24 def fill_circle(r, rgb):
25     fillcolor(rgb)
26     begin_fill()
27     left(180)
28     circle(r)
29     end_fill()
30 
31 
32 moveto(0, 60)
33 fill_circle(10, 'black')
34 
35 moveto(8, 74)
36 fill_circle(4, 'white')
37 
38 
39 hideturtle()
40 done()

 

标签:turtle,10,moveto,20,python,绘图,fill,def,left
From: https://www.cnblogs.com/hegejiushishiwo/p/17472984.html

相关文章

  • Linux下安装python3.7.9
    操作系统与原有python[pythondemo@localhost~]$cat/etc/centos-releaseCentOSLinuxrelease7.4.1708(Core)[pythondemo@localhost~]$pythonPython2.7.5(default,Aug42017,00:39:18)[GCC4.8.520150623(RedHat4.8.5-16)]onlinux2Type"help",......
  • python第一次cli程序的坑
    错误一:appacheerror记录到错误:AH01215:(13)Permissiondenied:execof'/var/www/cgi-bin/cli_test.py'failed1.对应程序加上执行权限2.selinux=disable3.指定安全上下文 chcon-R-thttpd_sys_content_t/var/www/cgi-bin这三个方法可以试一下错误二:malformedheade......
  • 【技术积累】Python中的Pandas库【三】
    什么是SeriesSeries是一种带有标签的一维数组,可以容纳各种类型的数据(例如整数,浮点数和字符串)。每个Series对象都有一个索引,它可以用来引用每个元素。Series对象的主要特征是可以进行矢量化操作(即一次对整个序列进行操作),因此非常适合处理数值数据。什么是DataFrame?DataFrame是一......
  • Python实现猜拳小游戏的多种方式
    简介猜拳小游戏是一个经典的小游戏项目,也是初学者学习编程的必要练手题目之一。在Python中,我们可以使用多种方式来实现一个简单的猜拳小游戏。本文将依次介绍六种Python实现猜拳小游戏的方法,包括:使用if-else条件语句、使用random模块、使用字典映射胜负关系、for循环、whi......
  • python数组避坑操作(比如删除数组中的所有0)
    一、演示坑tracks=[0,0,0,1,1,1]fortrackintracks:iftrack==0:tracks.remove(track)print(tracks)#[0,1,1,1]发现:有一个0没有被删去,why???二、这次遍历时,带上索引打印tracks=[0,0,0,1,1,1]forindex,trackinenumerate(tracks......
  • python变量前的星号
    变量前单星号表示将参数转化成元组变量前双星号表示将参数转化成字典函数传参顺序从左到右(一般):位置参数、默认参数、单星号参数、关键字传参、双星号参数传参解压功能单星号对list或元组进行解压,输入的参数不是一个list或元组,而是其中的元素。双星号对字典进......
  • python整型/字符串/浮点 地址
    相同整数/浮点数/字符串-同一内存地址不同整数/浮点数/字符串-不同内存地址......
  • 日报 python
    (一)、中国大学排名数据分析与可视化;(写到实验报告中)【源代码程序】importrequestsfrombs4importBeautifulSoupasbsimportpandasaspdfrommatplotlibimportpyplotasplt  defget_rank(url):   count=0   rank=[]   headers={     ......
  • 盘点一个Python自动化办公过程中Excel数据为空的处理
    大家好,我是皮皮。一、前言前几天在Python群,粉丝问了一个Python自动化办公的问题,这里拿出来给大家分享下。这个问题相信很多人都会遇到,原始Excel数据中,这个【编号】列一般是有相关数据的,但是如果没有的话,就先写为“暂无编号”,如下图所示:后来发现通过Python代码,将其写入到word文件,不......
  • 盘点一个Python网络爬虫问题
    大家好,我是皮皮。一、前言前几天在Python最强王者群【刘桓鸣】问了一个Python网络爬虫的问题,这里拿出来给大家分享下。他自己的代码如下:importrequestskey=input("请输入关键字")res=requests.post(url="https://jf.10086.cn/cmcc-web-shop/search/query",data=......