Please enable java script to visit.
NOTEBOOK
HOMEPHP / MySQLJS / HTMLWXappPythonC++Blender其他
Python 海龟画图基本示例 - NOTEBOOK
Python 海龟画图基本示例
Python
Posted on 2023-05-18
摘要 :
❱ 移动 画线


# 移动 画线
import turtle
turtle.showturtle()
turtle.write('hello ni hao') #文字内容
turtle.forward(300)
turtle.color('red')
turtle.left(90)
turtle.forward(300)
turtle.goto(0,50)
turtle.goto(0,0)
turtle.penup()
turtle.goto(0,300)
turtle.pendown()
turtle.goto(200,-300)
turtle.done()



❱ 画棋盘


import turtle
t = turtle.Pen()
t.speed(0)
x0 = -300
y0 = 300
dis = 20
l = 360
s1 = [[(x0+x*dis,y0-y*l) for y in range(2) ] for x in range(19)]
s2 = [[(x0+x*l,y0-y*dis) for x in range(2) ] for y in range(19)]
for s in s1:
t.penup()
t.goto(s[0][0],s[0][1])
t.pendown()
t.goto(s[1][0],s[1][1])
for s in s2:
t.penup()
t.goto(s[0][0],s[0][1])
t.pendown()
t.goto(s[1][0],s[1][1])
turtle.done()


import turtle
t = turtle.Pen()
dis = 20
l = 360
x = -200
y = 200
t.speed(0)
for a in range(19):
t.penup()
t.goto(x,y)
t.pendown()
t.goto(x,y-l)
x += dis
x = -200
y = 200
for a in range(19):
t.penup()
t.goto(x,y)
t.pendown()
t.goto(x+l,y)
y -= dis
turtle.done()



❱ 同心圆


import turtle
t = turtle.Pen() # 画笔默认坐标 0,0 方向向右
r = 20 #半径
colors = ['red','green','yellow','black']
t.speed(0) # 速度为0 最快
for x in range(10):
t.width((x+1)*1.5) #画笔大小
t.penup()
t.goto(0,-x*r)
t.pendown()
t.color(colors[x%len(colors)]) # x 对4 取余数,即只能0-3的值
t.circle((x+1)*r) # 半径 200
turtle.done()



# 画同心圆
import turtle
t = turtle.Pen() # 画笔默认坐标 0,0 方向向右

t.speed(0) # 笔画速度
t.circle(100)
t.penup()
t.goto(0,-100)
t.pendown()
t.circle(200)
t.penup()
t.goto(0,-200)
t.pendown()
t.circle(300)
t.penup()
t.goto(0,-300)
t.pendown()
t.circle(400)

turtle.done()


❱ 循环画图


# 循环画图
import turtle
turtle.showturtle()
for x in range(360): #循环步骤
turtle.forward(x) #移动距离和循环步骤一致
turtle.left(59)
turtle.done()



❱ 绘制奥运五环


# 绘制奥运五环
import turtle

turtle.width(10) #画笔粗细

turtle.color('blue') #画笔颜色
turtle.circle(50) #画圆
turtle.penup() #抬笔

turtle.goto(120,0) #移动
turtle.pendown() #落笔
turtle.color('black')
turtle.circle(50)
turtle.penup()

turtle.goto(240,0)
turtle.pendown()
turtle.color('red')
turtle.circle(50)
turtle.penup()

turtle.goto(60,-60)
turtle.pendown()
turtle.color('green')
turtle.circle(50)
turtle.penup()

turtle.goto(180,-60)
turtle.pendown()
turtle.color('yellow')
turtle.circle(50)

turtle.done()