python海龟画笔如何运行_Python海龟绘图:turtle的简单使用
Python海龜繪圖:turtle的簡單使用
Python海龜繪圖:turtle的簡單使用
? ?Turtle庫是Python語言中一個很流行的繪制圖像的函數庫,想象一個小烏龜,在一個橫軸為x、縱軸為y的坐標系原點,(0,0)位置開始,它根據一組函數指令的控制,在這個平面坐標系中移動,從而在它爬行的路徑上繪制了圖形。
? ?使用時需要添加頭文件
import turtle
一、畫布(canvas)
? ?畫布就是turtle為我們展開用于繪圖區域, 我們可以設置它的大小和初始位置
設置畫布的大小:
turtle.screensize(canvwidth=None, canvheight=None, bg=None)
canvwidth:畫布的寬(單位像素,默認值400)
canvheight:畫布的高(單位像素,默認值300)
bg:背景顏色
使用:
t.screensize(800,600,'blue')
turtle.setup(width=0.5, height=0.75, startx=None, starty=None)
width, height: 輸入寬和高為整數時, 表示像素; 為小數時, 表示占據電腦屏幕的比例
startx, starty: 這一坐標表示 矩形窗口左上角頂點的位置, 如果為空,則窗口位于屏幕中心
使用:
turtle.setup(width=0.9, height=0.9)
turtle.setup(0.9,0.9)#和上面代碼效果相同
turtle.setup(width=800, height=800, startx=100, starty=100)
二、畫筆
2.1畫筆的狀態
? ?在畫布上,默認有一個坐標原點為畫布中心的坐標軸, 坐標原點上有一只面朝x軸正方向小烏龜.
? ?這里我們描述小烏龜時使用了兩個詞語:坐標原點(位置),面朝x軸正方向(方向), turtle繪圖中, 就是使用位置方向描述小烏龜(畫筆)的狀態
2.2 畫筆的屬性
畫筆(畫筆的屬性,顏色、畫線的寬度)
turtle.pensize():設置畫筆的寬度;
turtle.pencolor(); 沒有參數傳入,返回當前畫筆顏色,傳入參數設置畫筆顏色,可以是字符串如"green", “red”,也可以是RGB 3元組
turtle.speed(speed): 設置畫筆移動速度,畫筆繪制的速度范圍[0,10]整數, 數字越大越快
2.3 繪圖命令
? ?操縱海龜繪圖有著許多的命令,這些命令可以劃分為3種:一種為運動命令,一種為畫筆控制命令,還有一種是全局控制命令
1、畫筆運動的命令:
2、畫筆控制命令
3、全局控制命令
4、其他命令
三、circle函數
以給定半徑畫圓
turtle.circle(radius, extent=None, steps=None)
radius(半徑); 半徑為正(負),表示圓心在畫筆的左邊(右邊)畫圓
extent(弧度) (optional);
steps (optional) (做半徑為radius的圓的內切正多邊形,多邊形邊數為steps)
舉例:
turtle.circle(50) # 整圓;
turtle.circle(50,steps=3) # 三角形;
turtle.circle(120, 180) # 半圓
四、繪圖舉例
奧運五環
實現代碼
import turtle as t
#直接到達坐標x,y的位置
def go(x,y) :
t.penup()
t.goto(x,y)
t.pendown()
#設置畫布大小、畫筆大小、畫筆粗細
def pen() :
t.screensize(0.99, 0.99)
t.pensize(10)
t.speed(10)
def main() :
pen()
pencolor = ['blue','black','red','yellow','green']#列表存儲畫筆顏色
x = -450
y = 0
for i in range(5) :
if i == 3 :
x = -225
y = -150
if i < 3 :
go(x + i * 450,y)
t.pencolor(pencolor[i])
t.circle(200)
else :
go(x + ( i - 3 ) * 450, y)
t.pencolor(pencolor[i])
t.circle(200)
if __name__ == '__main__':
main()
520
實現代碼
from turtle import *
def go_to(x, y):
up()
goto(x, y)
down()
def big_Circle(size): #函數用于繪制心的大圓
speed(10)
for i in range(150):
forward(size)
right(0.3)
def small_Circle(size): #函數用于繪制心的小圓
speed(10)
for i in range(210):
forward(size)
right(0.786)
def line(size):
speed(10)
forward(51*size)
def heart( x, y, size):
go_to(x, y)
left(150)
begin_fill()
line(size)
big_Circle(size)
small_Circle(size)
left(120)
small_Circle(size)
big_Circle(size)
line(size)
end_fill()
def arrow():
pensize(10)
setheading(0)
go_to(-400, 0)
left(15)
forward(150)
go_to(339, 178)
forward(150)
def arrowHead():
pensize(1)
speed(10)
color('red', 'red')
begin_fill()
left(120)
forward(20)
right(150)
forward(35)
right(120)
forward(35)
right(150)
forward(20)
end_fill()
def main():
pensize(2)
color('red', 'pink')
#getscreen().tracer(30, 0) #取消注釋后,快速顯示圖案
heart(200, 0, 1) #畫出第一顆心,前面兩個參數控制心的位置,函數最后一個參數可控制心的大小
setheading(0) #使畫筆的方向朝向x軸正方向
heart(-80, -100, 1.5) #畫出第二顆心
arrow() #畫出穿過兩顆心的直線
arrowHead() #畫出箭的箭頭
go_to(400, -300)
done()
if __name__ == '__main__':
main()
小豬佩奇
實現代碼
from turtle import*
speed(10)
def nose(x,y):#鼻子
penup()#提起筆
goto(x,y)#定位
pendown()#落筆,開始畫
setheading(-30)#將烏龜的方向設置為to_angle/為數字(0-東、90-北、180-西、270-南)
begin_fill()#準備開始填充圖形
a=0.4
for i in range(120):
if 0<=i<30 or 60<=i<90:
a=a+0.08
left(3) #向左轉3度
forward(a) #向前走a的步長
else:
a=a-0.08
left(3)
forward(a)
end_fill()#填充完成
penup()
setheading(90)
forward(25)
setheading(0)
forward(10)
pendown()
pencolor(255,155,192)#畫筆顏色
setheading(10)
begin_fill()
circle(5)
color(160,82,45)#返回或設置pencolor和fillcolor
end_fill()
penup()
setheading(0)
forward(20)
pendown()
pencolor(255,155,192)
setheading(10)
begin_fill()
circle(5)
color(160,82,45)
end_fill()
def head(x,y):#頭
color((255,155,192),"pink")
penup()
goto(x,y)
setheading(0)
pendown()
begin_fill()
setheading(180)
circle(300,-30)
circle(100,-60)
circle(80,-100)
circle(150,-20)
circle(60,-95)
setheading(161)
circle(-300,15)
penup()
goto(-100,100)
pendown()
setheading(-30)
a=0.4
for i in range(60):
if 0<=i<30 or 60<=i<90:
a=a+0.08
lt(3) #向左轉3度
fd(a) #向前走a的步長
else:
a=a-0.08
lt(3)
fd(a)
end_fill()
def ears(x,y): #耳朵
color((255,155,192),"pink")
penup()
goto(x,y)
pendown()
begin_fill()
setheading(100)
circle(-50,50)
circle(-10,120)
circle(-50,54)
end_fill()
penup()
setheading(90)
forward(-12)
setheading(0)
forward(30)
pendown()
begin_fill()
setheading(100)
circle(-50,50)
circle(-10,120)
circle(-50,56)
end_fill()
def eyes(x,y):#眼睛
color((255,155,192),"white")
penup()
setheading(90)
forward(-20)
setheading(0)
forward(-95)
pendown()
begin_fill()
circle(15)
end_fill()
color("black")
penup()
setheading(90)
forward(12)
setheading(0)
forward(-3)
pendown()
begin_fill()
circle(3)
end_fill()
color((255,155,192),"white")
penup()
seth(90)
forward(-25)
seth(0)
forward(40)
pendown()
begin_fill()
circle(15)
end_fill()
color("black")
penup()
setheading(90)
forward(12)
setheading(0)
forward(-3)
pendown()
begin_fill()
circle(3)
end_fill()
def cheek(x,y):#腮
color((255,155,192))
penup()
goto(x,y)
pendown()
setheading(0)
begin_fill()
circle(30)
end_fill()
def mouth(x,y): #嘴
color(239,69,19)
penup()
goto(x,y)
pendown()
setheading(-80)
circle(30,40)
circle(40,80)
def body(x,y):#身體
color("red",(255,99,71))
penup()
goto(x,y)
pendown()
begin_fill()
setheading(-130)
circle(100,10)
circle(300,30)
setheading(0)
forward(230)
setheading(90)
circle(300,30)
circle(100,3)
color((255,155,192),(255,100,100))
setheading(-135)
circle(-80,63)
circle(-150,24)
end_fill()
def hands(x,y):#手
color((255,155,192))
penup()
goto(x,y)
pendown()
setheading(-160)
circle(300,15)
penup()
setheading(90)
forward(15)
setheading(0)
forward(0)
pendown()
setheading(-10)
circle(-20,90)
penup()
setheading(90)
forward(30)
setheading(0)
forward(237)
pendown()
setheading(-20)
circle(-300,15)
penup()
setheading(90)
forward(20)
setheading(0)
forward(0)
pendown()
setheading(-170)
circle(20,90)
def foot(x,y):#腳
pensize(10)
color((240,128,128))
penup()
goto(x,y)
pendown()
setheading(-90)
forward(40)
setheading(-180)
color("black")
pensize(15)
fd(20)
pensize(10)
color((240,128,128))
penup()
setheading(90)
forward(40)
setheading(0)
forward(90)
pendown()
setheading(-90)
forward(40)
setheading(-180)
color("black")
pensize(15)
fd(20)
def tail(x,y):#尾巴
pensize(4)
color((255,155,192))
penup()
goto(x,y)
pendown()
seth(0)
circle(70,20)
circle(10,330)
circle(70,30)
def setting(): #參數設置
speed(99)
pensize(4)
hideturtle() #使烏龜無形(隱藏)
colormode(255) #將其設置為1.0或255.隨后 顏色三元組的r,g,b值必須在0 .. cmode范圍內
color((255,155,192),"pink")
setup(840,500)
def main():
setting() #畫布、畫筆設置
nose(-100,100) #鼻子
head(-69,167) #頭
ears(0,160) #耳朵
eyes(0,140) #眼睛
cheek(80,10) #腮
mouth(-20,30) #嘴
body(-32,-8) #身體
hands(-56,-45) #手
foot(2,-177) #腳
tail(148,-155) #尾巴
done()
if __name__ == '__main__':
main()
Python海龜繪圖:turtle的簡單使用相關教程
總結
以上是生活随笔為你收集整理的python海龟画笔如何运行_Python海龟绘图:turtle的简单使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 红河多少钱啊?
- 下一篇: 斗罗大陆魂师对决秦明怎么加点?