Turtle Demo 是 Python 自带的一个演示程序,无论是 Anaconda 安装还是使用原生 Python 安装都会有这个程序。下面所使用的各种图像,都是用turtle库绘制完成的。
下面就用pycharm给大家示范几个简单的案例:
演示案例:
向日葵
import turtle
import time
turtle.speed(10)
turtle.color("red", "yellow")
turtle.begin_fill()
for _ in range(50):
turtle.forward(200)
turtle.left(170)
turtle.end_fill()
turtle.done()
效果展示:
正方形阵图
import turtle
turtle.color('black', 'red')
turtle.speed("fastest")
turtle.begin_fill()
for x in range(100):
turtle.forward(2*x)
turtle.left(90)
turtle.end_fill()
turtle.done()
效果展示:
七彩螺旋纹
import turtle
turtle.tracer(0)
turtle.pensize(2)
turtle.bgcolor("black")
colors=["red","blue","yellow","purple"]
for x in range(300):
turtle.color(colors[x%4])
turtle.forward(2*x)
turtle.left(91)
turtle.done()
效果展示:
要是想超快的绘制,把turtle.speed(“fastest”)换成turtle.tracer(0),打开直接就是结果。
Python自带Demo
启动命令:
python –m turtledemo
模式解析:
模式案例
bytedesign
from turtle import Turtle, mainloop
from time import perf_counter as clock
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