turtle画图基本操作
基本代码
直接生成图形,不显示乌龟运动轨迹。turtle.tracer(False)
import turtle
turtle.pensize(1)
turtle.tracer(False)#直接生成图形,不显示乌龟运动轨迹。
for i in range(1,100,10):
turtle.circle(i)
生成图形:
生成几何图形
双层圆
运行如下代码:
import turtle
turtle.setup(500,500,None,None)
turtle.circle(50)
turtle.goto(4,6)
turtle.circle(30)
turtle.pendown()
turtle.home()
生成图形如下
三角形、六边形、圆
运行以下这段代码
import turtle
turtle.pensize(3)
turtle.penup()
turtle.goto(-200,-50)
turtle.pendown()
turtle.begin_fill()
turtle.color('red')
turtle.circle(100,steps=3)#运动一圈 circle,三步step=3,三角形
turtle.end_fill()
turtle.goto(0,-50)
turtle.pendown()
turtle.begin_fill()
turtle.color('yellow')
turtle.circle(100,steps=6)#运动一圈 circle,六步step=6,六边形
turtle.end_fill()
turtle.goto(200,-50)
turtle.dot(50,"green")#运动一圈,一个圆形 dot
生成如下图形:
星星
图形在右,代码在左。
五彩圆
程序代码
import turtle
colors=['red','orange','yellow','green','blue','indigo','purple']
for i in range(7):
c=colors[i]
turtle.color(c,c)
turtle.begin_fill()
turtle.rt(360/7)
turtle.circle(50)
turtle.end_fill()
turtle.done()
生成图形
组合体
积累程序-turtle-数学几何图形-不断调试中发现新东西-新图形。
程序代码
from turtle import *
setup(800,600)
speed(10)
for i in range(5):
goto(40*i,30*i)
goto(40*i,0)
goto(0,0)
penup()
goto(0,50*i)
pendown()
circle(50*i)
goto(0,0)
生成图形
循环网
运行代码:
from turtle import *
tracer(False)
myNum=6
for m in range(6):
right(30)
circle(10)
for i in range(1,100,5):
pendown()
circle(i)
penup()
goto(i,i)
生成图形:
生成数学图像
不同角度的螺线
程序代码
运行代码,输入不同角度。
import turtle
import time
myjiaodu=int(input("请输入旋转角度:"))
turtle.tracer(False)
#turtle.speed("fastest")#画笔速度
turtle.pensize(2)
turtle.bgcolor("white")
colors=["red","yellow","blue","purple"]
for i in range(400):
turtle.forward(2*i)
# turtle.color(colors[i%4])
turtle.left(myjiaodu)
turtle.done()
用IDLE运行代码的时候,较慢。所以可以用CMD运行。
用CMD运行,生成图形如下:
12度角
35度角
66度角
97度角
108度角
170度角
直线切割出螺线
代码在左,右为生成图形
不同角度和颜色的螺线
程序代码
import turtle
import time
myjiaodu=int(input("请输入旋转角度:"))
turtle.tracer(False)
#turtle.speed("fastest")#画笔速度
turtle.pensize(2)
turtle.bgcolor("black")
colors=["red","yellow","blue","purple"]
for i in range(400):
turtle.forward(2*i)
turtle.color(colors[i%4])
turtle.left(myjiaodu)
turtle.done()
输入不同角度数值,生成不同的图像。
生成复杂图形
几何旋转花瓣,
运行代码如下:
#!/usr/bin/env python3
""" turtle-example-suite:
tdemo_bytedesign.py
An example adapted from the example-suite
of PythonCard's turtle graphics.
It's based on an article in BYTE magazine
Problem Solving with Logo: Using Turtle
Graphics to Redraw a Design
November 1982, p. 118 - 134
-------------------------------------------
Due to the statement
t.delay(0)
in line 152, which sets the animation delay
to 0, this animation runs in "line per line"
mode as fast as possible.
"""
from turtle import Turtle, mainloop
from time import perf_counter as clock
# wrapper for any additional drawing routines
# that need to know about each other
class Designer(Turtle):
def design(self, homePos, scale):
self.up()
for i in range(5):
self.forward(64.65 * scale)
self.down()
self.wheel(self.position(), scale)
self.up()
self.backward(64.65 * scale)
self.right(72)
self.up()
self.goto(homePos)
self.right(36)
self.forward(24.5 * scale)
self.right(198)
self.down()
self.centerpiece(46 * scale, 143.4, scale)
self.getscreen().tracer(True)
def wheel(self, initpos, scale):
self.right(54)
for i in range(4):
self.pentpiece(initpos, scale)
self.down()
self.left(36)
for i in range(5):
self.tripiece(initpos, scale)
self.left(36)
for i in range(5):
self.down()
self.right(72)
self.forward(28 * scale)
self.up()
self.backward(28 * scale)
self.left(54)
self.getscreen().update()
def tripiece(self, initpos, scale):
oldh = self.heading()
self.down()
self.backward(2.5 * scale)
self.tripolyr(31.5 * scale, scale)
self.up()
self.goto(initpos)
self.setheading(oldh)
self.down()
self.backward(2.5 * scale)
self.tripolyl(31.5 * scale, scale)
self.up()
self.goto(initpos)
self.setheading(oldh)
self.left(72)
self.getscreen().update()
def pentpiece(self, initpos, scale):
oldh = self.heading()
self.up()
self.forward(29 * scale)
self.down()
for i in range(5):
self.forward(18 * scale)
self.right(72)
self.pentr(18 * scale, 75, scale)
self.up()
self.goto(initpos)
self.setheading(oldh)
self.forward(29 * scale)
self.down()
for i in range(5):
self.forward(18 * scale)
self.right(72)
self.pentl(18 * scale, 75, scale)
self.up()
self.goto(initpos)
self.setheading(oldh)
self.left(72)
self.getscreen().update()
def pentl(self, side, ang, scale):
if side < (2 * scale): return
self.forward(side)
self.left(ang)
self.pentl(side - (.38 * scale), ang, scale)
def pentr(self, side, ang, scale):
if side < (2 * scale): return
self.forward(side)
self.right(ang)
self.pentr(side - (.38 * scale), ang, scale)
def tripolyr(self, side, scale):
if side < (4 * scale): return
self.forward(side)
self.right(111)
self.forward(side / 1.78)
self.right(111)
self.forward(side / 1.3)
self.right(146)
self.tripolyr(side * .75, scale)
def tripolyl(self, side, scale):
if side < (4 * scale): return
self.forward(side)
self.left(111)
self.forward(side / 1.78)
self.left(111)
self.forward(side / 1.3)
self.left(146)
self.tripolyl(side * .75, scale)
def centerpiece(self, s, a, scale):
self.forward(s); self.left(a)
if s < (7.5 * scale):
return
self.centerpiece(s - (1.2 * scale), a, scale)
def main():
t = Designer()
t.speed(0)
t.hideturtle()
t.getscreen().delay(0)
t.getscreen().tracer(0)
at = clock()
t.design(t.position(), 2)
et = clock()
return "runtime: %.2f sec." % (et-at)
if __name__ == '__main__':
msg = main()
print(msg)
mainloop()
生成图形如下:
五彩雪景
运行代码:
'''
【目标】绘制雪景图片
turtle.pensize(3)
turtle.penup()
setx(240)
sety(300)
turtle.circle(10)
turtle.pendown()
'''
#第一步,构建图的背景
from turtle import *
from random import *
import turtle
setup(800,600,200,200)
tracer(False)#不用跟踪画画的轨迹
bgcolor("black")#背景颜色
#第二步,绘制雪花效果。随机数,雪花
hideturtle()
pensize(2)
for i in range(100):
r,g,b=random(),random(),random()
pencolor(r,g,b)
penup()
setx(randint(-350,350))#修改画笔的横坐标到x,纵坐标不变
sety(randint(-300,300))#修改画笔的纵标到y,横坐标不变,注意没有等号
pendown()
dens=randint(8,12)#dens雪花花瓣数
snowsize=randint(10,14)#雪花大小
for j in range(dens):#画每一个雪花线条,来回一条线
forward(snowsize)
backward(snowsize)
right(360/dens)#旋转角度
#第三步,绘制雪地效果。横线,灰度
hideturtle()
for i in range(400):
pensize(randint(1,5))
x=randint(-400,350)#注意有等号
y=randint(-280,-1)
r,g,b=0,0,0
pencolor=(r,g,b)
penup()
goto(x,y)
pendown()
forward(randint(40,100))
done()
生成图形:
运动星系
程序代码
'''
This example is
inspired by the Wikipedia article on turtle
graphics. (See example wikipedia1 for URLs)
First we create (ne-1) (i.e. 35 in this
example) copies of our first turtle p.
Then we let them perform their steps in
parallel.
Followed by a complete undo().
'''
from turtle import Screen, Turtle, mainloop
from time import perf_counter as clock, sleep
def mn_eck(p, ne,sz):
turtlelist = [p]
#create ne-1 additional turtles
for i in range(1,ne):
q = p.clone()
q.rt(360.0/ne)
turtlelist.append(q)
p = q
for i in range(ne):
c = abs(ne/2.0-i)/(ne*.7)
# let those ne turtles make a step
# in parallel:
for t in turtlelist:
t.rt(360./ne)
t.pencolor(1-c,0,c)
t.fd(sz)
def main():
s = Screen()
s.bgcolor("black")
p=Turtle()
p.speed(0)
p.hideturtle()
p.pencolor("red")
p.pensize(3)
s.tracer(36,0)
at = clock()
mn_eck(p, 36, 19)
et = clock()
z1 = et-at
sleep(1)
at = clock()
while any(t.undobufferentries() for t in s.turtles()):
for t in s.turtles():
t.undo()
et = clock()
return "runtime: %.3f sec" % (z1+et-at)
if __name__ == '__main__':
msg = main()
print(msg)
mainloop()
图形如下: