2018年6月12日筆記
- 按win+q鍵換出搜索界面,輸入path,進入系統屬性,選擇高級,選擇環(huán)境變量。在系統變量中的PATHEXT這個變量中文本內容為.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC。如果這個文本內容中沒有.EXE,在cmd中輸入命令的時候則不能省略.exe的后綴,即原本pip install xlwt要寫成pip.exe install xlwt。
- 學習用python作畫:首先進入python的shell界面,方法是在安裝好python的情況下在cmd中運行python命令,就可以進入python的shell界面。
進入以后,導入turtle庫。方法是在python的shell中運行命令:from turtle import *。文章后面運行命令的環(huán)境都是python的shell。 - 畫一條直線,執(zhí)行下面的兩行命令可以實現。
pendown()
forward(100)
pendown()的作用是落筆,只有落筆才能作畫。
當不作畫卻想移動畫筆的時候要提筆,用函數penup()
forward是畫筆向前移動,函數當中參數為移動距離。
forward(100)的意思是畫筆向前移動100。
- 畫一個邊長為200的正方形。
for i in range(4):
forward(200)
right(90)
- 畫一個復雜圖形。
def draw1():
reset()
speed(10)
for i in range(36):
forward(200)
left(170)
reset()
speed(10)
draw1()
speed()中的參數1-10畫圖速度遞增,但是有一個反例參數為0時速度最快。
reset()會重置畫筆,畫布,作畫速度。
- 順時針方向畫一個200半徑的圓:circle(-200)。
逆時針方向畫一個200半徑的圓:circle(200)。
順時針畫一個100半徑的半圓:circle(-100,180)。
順時針畫一個邊長為150的正方形:circle(-150,360,4)。 - 將圖形涂色示例,畫一個紅色的半圓。
reset()
fillcolor('red')
begin_fill()
circle(100,180)
end_fill()
8.復雜圖形涂色示例,畫一個“太極”圖案。
reset()
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()
circle(100)與circle(100,360)兩條命令效果相同。
撤回一步:undo(),清空畫布:clear()。

畫出的太極圖形.png
- 畫一段曲線
for i in range(8):
circle(20,100)
circle(-20,100)
- 畫一個復雜圖形,利用循環(huán)嵌套方法
from turtle import *
reset()
speed(0)
pendown()
for i in range(6):
fd(150)
for j in range(10):
circle(40)
lt(36)
lt(60)

復雜圖形1.png
- 畫一個復雜圖形,利用循環(huán)嵌套方法
from turtle import *
reset()
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)

復雜圖形2.png
- 獲取畫筆當前位置:position() pos() 兩個函數用處一樣
設置畫筆位置:setposition() setpos()
獲取角度:heading()
設置角度setheading() seth() - 畫一個橢圓
reset()
setheading(45)
circle(10,90)
circle(90,90)
circle(10,90)
circle(90,90)
14.畫一個笑臉。下面的代碼作為一個單獨py文件可以運行。
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()

笑臉.png
直接在cmd中可能無法運行,需要先定義函數,再調用函數,如下圖所示,。

cmd中運行示例.png
- 畫一個酷炫圖形。
from turtle import *
reset()
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)

炫酷圖案.png