turtle库的⼏个案例进阶,代码可直接运⾏(python经典编程案例)1. 画出奥运五环图
代码如下:
import turtle
turtle.width(10)
turtle.circle(50)
turtle.penup()
<(120,0)
turtle.pendown()
turtle.circle(50)
turtle.penup()
xml文件怎么运行的<(240,0)
kotlin开发网页应用turtle.pendown()
turtle.circle(50)
turtle.penup()
nginx和(60,-50)
turtle.pendown()
turtle.circle(50)
turtle.penup()
<(180,-50)
turtle.pendown()
turtle.circle(50)
执⾏结果如下图所⽰:
2. 定义多点坐标并计算起始点和终点距离
import turtle
import math
# 定义多个点的坐标
x1, y1 =100,100
x2, y2 =100,-100
x3, y3 =-100,-100
x4, y4 =-100,100
# 绘制折线
turtle.penup()
<(x1, y1)
turtle.pendown()
<(x2, y2)
<(x3, y3)
<(x4, y4)
# 计算起始点和终点的距离
distance = math.sqrt((x1-x4)**2+(y1-y4)**2) turtle.write(distance)
3. 画玫瑰
⽤Python 画出玫瑰图案,代码如下:import turtle as t
# 定义⼀个曲线绘制函数
def DegreeCurve(n, r, d=1):
for i in range(n):
t.left(d)
t.circle(r,abs(d))
# 初始位置设定
s =0.2# size
t.setup(450*5*s,750*5*s)课程表翻译成英语
t.pencolor("black")
t.fillcolor("red")
t.speed(100)
t.penup()
python代码画图案<(0,900*s)
t.pendown()
# 绘制花朵形状
t.begin_fill()
t.circle(200*s,30)
DegreeCurve(60,50*s)
t.circle(200*s,30)
DegreeCurve(4,100*s)
t.circle(200*s,50)
DegreeCurve(50,50*s)
t.circle(350*s,65)
DegreeCurve(40,70*s)
DegreeCurve(40,70*s)
t.circle(150*s,50) DegreeCurve(20,50*s,-1) t.circle(400*s,60) DegreeCurve(18,50*s)
t.fd(250*s)
t.right(150)
t.circle(-500*s,12)
t.left(140)
t.circle(550*s,110)
t.left(27)
t.circle(650*s,100)
t.left(130)
t.circle(-300*s,20)
t.right(123)
t.circle(220*s,57)
# 绘制花枝形状
t.left(120)
t.fd(280*s)
t.left(115)
t.circle(300*s,33)
t.left(180)
t.circle(-300*s,33) DegreeCurve(70,225*s,-1) t.circle(350*s,104)
t.left(90)
t.circle(200*s,105)
t.circle(-500*s,63)
t.penup()
<(170*s,-30*s)
t.pendown()
t.left(160)
DegreeCurve(20,2500*s) DegreeCurve(220,250*s,-1) # 绘制⼀个绿⾊叶⼦
t.fillcolor('green')
t.penup()
<(670*s,-180*s)
t.pendown()
t.right(140)
t.begin_fill()
t.circle(300*s,120)
t.left(60)
t.circle(300*s,120)
t.penup()
<(180*s,-550*s)
t.pendown()
t.right(85)
t.circle(600*s,40)
# 绘制另⼀个绿⾊叶⼦
t.penup()
<(-150*s,-1000*s)
t.pendown()
t.begin_fill()
<(120)
t.circle(300*s,115)
t.left(75)
t.circle(300*s,100)
t.penup()
<(430*s,-1070*s)
t.pendown()
t.right(30)
t.circle(-600*s,35)
t.circle(-600*s,35)
t.done()
执⾏结果如下图:
4. 绘制多个同⼼圆代码如下:
import turtle
t = turtle.Pen()
my_colors =("red","green","yellow","black") t.width(4)
t.speed(1)
for i in range(10):# 0 1 2 3 4
t.penup()
<(0,-i*10)# 0, -100,-200,-300,-400 t.pendown()
t.circle(15+i*10)# 100,200,300, 400,, 500 turtle.done()# 程序执⾏完,窗⼝仍然在
执⾏结果如下图:
5. ⽤Python 绘制 18*18 棋盘
import turtle
width =30
num =18
x1 =[(-400,400),(-400+width*num,400)]
y1 =[(-400,400),(-400,400-width*num)]
t = turtle.Pen()
t.speed(10)
for i in range(0,19):
t.penup()
<(x1[0][0], x1[0][1]-30*i)
t.pendown()
<(x1[1][0], x1[1][1]-30*i)
for i in range(0,19):
t.penup()
<(y1[0][0]+30*i,y1[0][1])
t.pendown()
<(y1[1][0]+30*i,y1[1][1])
t.hideturtle()# 隐藏画笔
turtle.done()# 保证运⾏窗⼝不被⾃动关闭
6. 画樱花
import turtle as T
import random
import time
# 画樱花的躯⼲(60,t)
def Tree(branch, t):
time.sleep(0.0005)
if branch >3:
if8<= branch <=12:
if random.randint(0,2)==0:
flexibility作品t.color('snow')# ⽩
else:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论