python turtle 库 介绍
控制画笔绘制状态的函数
pendown() | pd() | down()
penup() | pu() | up()
pensize(wid ) | width(wid)
forward(distance) | fd(distance)
backward(distance)| bk(distance)
|back(distance)
right(angle) | rt(angle)
left(angle) | lt(angle)
setheading(to_angle)
position() | pos()
goto(x,y )
setposition(x,y ) | setpos(x,y )
circle(radius,extent ,steps )
dot(size ,*color) radians()
stamp() speed(speed )
clearstamp(stamp_id)
clearstamps(n ) undo()
speed(speed ) heading()
towards(x,y ) distance(x,y )
xcor() ycor()
setx(x) sety(y)
home() undo()
degrees(fullcircle = 360.0)
控制画笔运动的函数
控制画笔颜色和字体函数
color() reset()
begin_fill() end_fill()
filling() clear()
screensize()
showturtle() | st()
hideturtle() | ht()
isvisible()
write(arg,move=False,align="left"
,font =("Arial",8,"normal") )
>>>import turtle
>>>from turtle import *
引入方式
bgcolor(*args)
bgpic(picname )
clearscreen()
resetscreen()
screensize(cwid ,canvh,bg )
tracer(n ,delay )
listen(xdummy ,ydummy )
onkey((fun,key)
onkeyrelease((fun,key)
onkeypress(fun,key )
onscreenclick(fun,btn=1,add )
TurtleScreen/Screen类的函数
getcanvas()
getshapes()
turtles()
window_height()
window_width()
bye()
exitonclick()
title(titlestring)
setup(wid=_CFG["wid"],h=_CFG["h"],
startx=_CFG["leftright"],
starty=_CFG["topbottom"])
turtle库
Python Quick Reference Series Python快速参考
python绘制等边三角形
import turtle
import random
def drawsnake(rad):
turtle.fd(rad)
turtle.seth(120)
turtle.fd(rad)
turtle.seth(-120)
turtle.fd(rad)
def main():
turtle.setup(1300,800,0,0)
pythonsize = 30
turtle.pensize(1)
c = random.uniform(0,1)
d = random.randint(0,1)
e = random.randint(0,1)
f = (c,d,e)
turtle.pencolor(f)
turtle.seth(0)
drawsnake(pythonsize * 6)
main()
python绘制彩蟒
import turtle
import random
def drawsnake(rad,angle,len,neckrad):
for i in range(len):
c = random.uniform(0,1)
d = random.randint(0,1)
e = random.randint(0,1)
f = (c,d,e)
turtle.pencolor(f)
turtle.circle(rad,angle)
c = random.uniform(0,1)
d = random.randint(0,1)
e = random.randint(0,1)
f = (c,d,e)
turtle.pencolor(f)
turtle.circle(-rad,angle)
c = random.uniform(0,1)
d = random.randint(0,1)
e = random.randint(0,1)
f = (c,d,e)
turtle.pencolor(f)
c = random.uniform(0,1)
d = random.randint(0,1)
e = random.randint(0,1)
f = (c,d,e)
turtle.pencolor(f)
turtle.circle(rad,angle/2)
c = random.uniform(0,1)
d = random.randint(0,1)
e = random.randint(0,1)
f = (c,d,e)
turtle.pencolor(f)
turtle.fd(rad)
c = random.uniform(0,1)
d = random.randint(0,1)
e = random.randint(0,1)
f = (c,d,e)
turtle.pencolor(f)
turtle.circle(neckrad+1,180)
c = random.uniform(0,1)
d = random.randint(0,1)
e = random.randint(0,1)
f = (c,d,e)
turtle.pencolor(f)
turtle.fd(rad*2/3)
def main():
turtle.setup(1300,800,0,0)
pythonsize = 30
turtle.pensize(pythonsize)
c = random.uniform(0,1)
d = random.randint(0,1)
e = random.randint(0,1)
f = (c,d,e)
turtle.pencolor(f)
turtle.seth(-220)
drawsnake(40,80,5,pythonsize/2)
main()
turtle库