本文先從簡(jiǎn)單圖形開始,最后繪制文章封面的炫酷圖形。
首先導(dǎo)入turtle庫(kù)
from turtle import*
例1:繪制一條直線
pendown()
forward(100)
注:
pendown()的作用是落筆,只有落筆才能作畫。當(dāng)不作畫卻想移動(dòng)畫筆的時(shí)候要提筆,用函數(shù)penup()。forward是畫筆向前移動(dòng),函數(shù)當(dāng)中參數(shù)為移動(dòng)距離。
forward(100)是畫筆向前移動(dòng)100。
例2畫一個(gè)邊長(zhǎng)為100的正方形
for i in range(4):
forward(100)
right(90)
或者,以下為順時(shí)針畫一個(gè)邊長(zhǎng)為100的正方形
circle(-100,360,4)
例3:順時(shí)針?lè)较虍嬕粋€(gè)100半徑的圓
circle(-100,360)
例4:畫一個(gè)紅色的半圓,填充紅色
fillcolor('red')
begin_fill()
circle(100,180)
end_fill()
例5:畫一段曲線
for i in range(5):
circle(20,150)
circle(-20,150)
例6:畫五角星
color("red")#設(shè)置筆藍(lán)色和填充紅色
begin_fill()#開始填充
for i in range(5):#循環(huán)次數(shù),也可以使用while循環(huán)
forward(100)#畫筆繪制的方向,向前移動(dòng)指定的距離
right(144)#向右旋轉(zhuǎn)144度
end_fill()#結(jié)束填充
ht()
例7畫個(gè)太極圖
speed(10)
pendown()
circle(100,180)
circle(200,180)
circle(100,-180)
fillcolor('black')
begin_fill()
circle(100,180)
circle(200,180)
circle(100,-180)
end_fill()
penup()
goto(0,100)
dot(50)
goto(0,-100)
pencolor('white')
dot(50)
hideturtle()
效果:
例8:畫一個(gè)橢圓
setheading(45)
circle(10,90)
circle(90,90)
circle(10,90)
circle(90,90)
例9:畫一個(gè)笑臉
from turtle import*
def go(x,y):
penup()
goto(x,y)
pendown()
def arc(radius):
circle(radius,90)
reset()
speed(0)
go(0,-150)
circle(200)
go(50,100)
seth(225)
arc(10)
arc(50)
arc(10)
arc(50)
go(-50,100)
seth(-45)
arc(-10)
arc(-50)
arc(-10)
arc(-50)
go(-70,-50)
arc(100)
hideturtle()
效果:
例10畫一個(gè)復(fù)雜圖形
speed(0)
pendown()
for i in range(6):
fd(150)
for j in range(10):
circle(40)
lt(36)
lt(60)
效果:
例11再畫個(gè)復(fù)雜的
speed(0)
for i in range(6):
pendown()
fd(150)
for j in range(10):
circle(40)
lt(36)
lt(60)
penup()
goto(0,0)
效果:
例12畫一個(gè)太陽(yáng)花
color("red","yellow")
begin_fill()
for i in range(50):
forward(200)
left(170)
end_fill()
mainloop()
或者
color('red','yellow')
begin_fill()
while True:
forward(200)
left(170)
if abs(pos())<1:
break
end_fill()
done()
效果:
例13畫一個(gè)酷炫圖形
bgcolor('black')
speed(0)
colors=['red','orange','green','cyan','blue','purple']
for i in range(360):
pencolor(colors[i%6])
fd(i*3/6+i)
left(61)
pensize(i*6/200)
效果:
例14畫五星紅旗
color('red','red')
begin_fill()
while True:
forward(288)
right(90)
forward(192)
right(90)
if abs(pos())<1:
break
end_fill()
#畫大星星
penup()
setposition(19.2,-38.4)
pendown()
color('yellow','yellow')
begin_fill()
while True:
forward(57.6)
right(144)
if round(xcor())==19 and round(ycor())==-38:
break
end_fill()
#畫四個(gè)小星星
penup()
setposition(86.4,-24)
pendown()
left(60)
color('yellow','yellow')
begin_fill()
while True:
forward(19.2)
right(144)
if round(xcor())==86 and round(ycor())==-24:
break
end_fill()
penup()
setposition(108,-40)
pendown()
right(30)
color('yellow','yellow')
begin_fill()
while True:
forward(19.2)
right(144)
if round(xcor())==108 and round(ycor())==-40:
break
end_fill()
penup()
setposition(108,-65)
pendown()
right(30)
color('yellow','yellow')
begin_fill()
while True:
forward(19.2)
right(144)
if round(xcor())==108 and round(ycor())==-65:
break
end_fill()
penup()
setposition(86,-80)
pendown()
right(30)
color('yellow','yellow')
begin_fill()
while True:
forward(19.2)
right(144)
if round(xcor())==86 and round(ycor())==-80:
break
end_fill()
penup()
setposition(0,0)
pendown()
done()
例15畫個(gè)會(huì)走的表
import turtle
from datetime import*
#抬起畫筆,向前運(yùn)動(dòng)一段距離放下
def Skip(step):
turtle.penup()
turtle.forward(step)
turtle.pendown()
def mkHand(name,length):
#注冊(cè)Turtle形狀,建立表針Turtle
turtle.reset()
Skip(-length*0.1)
#開始記錄多邊形的頂點(diǎn)。當(dāng)前的烏龜位置是多邊形的個(gè)頂點(diǎn)。
turtle.begin_poly()
turtle.forward(length*1.1)
#停止記錄多邊形的頂點(diǎn)。當(dāng)前的烏龜位置是多邊形的最后一個(gè)頂點(diǎn)。將與個(gè)頂點(diǎn)相連。
turtle.end_poly()
#返回最后記錄的多邊形。
handForm=turtle.get_poly()
turtle.register_shape(name,handForm)
def Init():
global secHand,minHand,hurHand,printer
#重置Turtle指向北
turtle.mode("logo")
#建立三個(gè)表針Turtle并初始化
mkHand("secHand",135)
mkHand("minHand",125)
mkHand("hurHand",90)
secHand=turtle.Turtle()
secHand.shape("secHand")
minHand=turtle.Turtle()
minHand.shape("minHand")
hurHand=turtle.Turtle()
hurHand.shape("hurHand")
for hand in secHand,minHand,hurHand:
hand.shapesize(1,1,3)
hand.speed(0)
#建立輸出文字Turtle
printer=turtle.Turtle()
#隱藏畫筆的turtle形狀
printer.hideturtle()
printer.penup()
def SetupClock(radius):
#建立表的外框
turtle.reset()
turtle.pensize(7)
for i in range(60):
Skip(radius)
if i%5==0:
turtle.forward(20)
Skip(-radius-20)
Skip(radius+20)
if i==0:
turtle.write(int(12),align="center",font=("Courier",14,"bold"))
elif i==30:
Skip(25)
turtle.write(int(i/5),align="center",font=("Courier",14,"bold"))
Skip(-25)
elif(i==25 or i==35):
Skip(20)
turtle.write(int(i/5),align="center",font=("Courier",14,"bold"))
Skip(-20)
else:
turtle.write(int(i/5),align="center",font=("Courier",14,"bold"))
Skip(-radius-20)
else:
turtle.dot(5)
Skip(-radius)
turtle.right(6)
def Week(t):
week=["星期一","星期二","星期三",
"星期四","星期五","星期六","星期日"]
return week[t.weekday()]
def Date(t):
y=t.year
m=t.month
d=t.day
return"%s%d%d"%(y,m,d)
def Tick():
#繪制表針的動(dòng)態(tài)顯示
t=datetime.today()
second=t.second+t.microsecond*0.000001
minute=t.minute+second/60.0
hour=t.hour+minute/60.0
secHand.setheading(6*second)
minHand.setheading(6*minute)
hurHand.setheading(30*hour)
turtle.tracer(False)
printer.forward(65)
printer.write(Week(t),align="center",
font=("Courier",14,"bold"))
printer.back(130)
printer.write(Date(t),align="center",
font=("Courier",14,"bold"))
printer.home()
turtle.tracer(True)
#100ms后繼續(xù)調(diào)用tick
turtle.ontimer(Tick,100)
def main():
#打開/關(guān)閉龜動(dòng)畫,并為更新圖紙?jiān)O(shè)置延遲。
turtle.tracer(False)
Init()
SetupClock(160)
turtle.tracer(True)
Tick()
turtle.mainloop()
if __name__=="__main__":
main()