最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

小白自學(xué)筆記之Python入門(mén)-第四章 常用模塊-turtle

2023-06-30 08:52 作者:wangyanhpa  | 我要投稿

4.1 turtle

turtle庫(kù)是Python語(yǔ)言中一個(gè)內(nèi)置的繪圖的函數(shù)庫(kù),在一個(gè)橫軸為x、縱軸為y的坐標(biāo)系原點(diǎn),(0,0)開(kāi)始,它根據(jù)一組函數(shù)指令的控制,在這個(gè)平面坐標(biāo)系中繪制圖形。使用turtle庫(kù)前,要用import導(dǎo)入該庫(kù),下面直接介紹繪圖步驟。?

1導(dǎo)入turtle庫(kù)

使用turtle進(jìn)行繪圖必須導(dǎo)入turtle庫(kù),具體代碼如下

import turtle?

2設(shè)置畫(huà)布大?。梢圆辉O(shè)置)

畫(huà)布就是turtle給我們提供的繪圖區(qū)域,坐標(biāo)原點(diǎn)位于中心位置,向右x增加,向上y增加??梢允褂胻urtle的screensize函數(shù)設(shè)置畫(huà)布的大小和初始位置。turtle.screensize (canvwidth=None, canvheight=None, bg=None),參數(shù)分別為畫(huà)布的寬(以像素為單位),高和背景顏色。如:turtle.screensize(80,60, "green")。?

3使用畫(huà)筆繪圖

畫(huà)筆是圖形繪制的工具,畫(huà)筆的屬性,顏色、畫(huà)線的寬度可以使用設(shè)置。

(1) turtle.pensize():設(shè)置畫(huà)筆的寬度(以像素為單位)

(2) turtle.pencolor():沒(méi)有參數(shù)傳入,返回當(dāng)前畫(huà)筆顏色,傳入?yún)?shù)設(shè)置畫(huà)筆顏色,可以是字符串如"green", "red"。

(3) turtle.speed(speed):設(shè)置畫(huà)筆移動(dòng)速度,畫(huà)筆繪制的速度范圍[0,10]整數(shù),數(shù)字越大越快。使用turtle畫(huà)圖時(shí),能夠看到繪制過(guò)程,畫(huà)筆速度可以影響繪制速度。

繪圖有三種命令:一種為運(yùn)動(dòng)命令,一種為畫(huà)筆控制命令,還有一種是全局控制命令。

命令????????????說(shuō)明

turtle.forward(distance)????向當(dāng)前畫(huà)筆方向移動(dòng)distance像素長(zhǎng)度

turtle.backward(distance)????向當(dāng)前畫(huà)筆相反方向移動(dòng)distance像素長(zhǎng)度

turtle.right(degree)????順時(shí)針移動(dòng)degree°

turtle.left(degree)????逆時(shí)針移動(dòng)degree°

turtle.pendown()????移動(dòng)時(shí)繪制圖形,缺省時(shí)也為繪制

turtle.goto(x,y)????將畫(huà)筆移動(dòng)到坐標(biāo)為x,y的位置

turtle.penup()????提起筆移動(dòng),不繪制圖形,用于另起一個(gè)地方繪制

turtle.circle()????畫(huà)圓,半徑為正(負(fù)),表示圓心在畫(huà)筆的左邊(右邊)畫(huà)圓

setx( )????將當(dāng)前x軸移動(dòng)到指定位置

sety( )????將當(dāng)前y軸移動(dòng)到指定位置

setheading(angle)????設(shè)置當(dāng)前朝向?yàn)閍ngle角度

home()????設(shè)置當(dāng)前畫(huà)筆位置為原點(diǎn),朝向東。

dot(r)????繪制一個(gè)指定直徑和顏色的圓點(diǎn)

表 畫(huà)筆運(yùn)動(dòng)命令

turtle.circle()語(yǔ)法為 ?turtle.circle(radius, extent=None, steps=None)

??? radius(半徑):半徑為正(負(fù)),表示圓心在畫(huà)筆的左邊(右邊)畫(huà)圓;

??? extent(弧度) (optional);弧形角度。當(dāng)無(wú)該參數(shù)或參數(shù)為None時(shí),繪制整個(gè)圓形。當(dāng)extent值為正數(shù)時(shí),順小海龜當(dāng)前方向繪制。當(dāng)extent值為負(fù)數(shù)時(shí),小海龜當(dāng)前方向的逆方向繪制。

??? steps (optional) (做半徑為radius的圓的內(nèi)切正多邊形,多邊形邊數(shù)為steps)。

?

命令????????????說(shuō)明

turtle.fillcolor(colorstring)????繪制圖形的填充顏色

turtle.color(color1, color2)????同時(shí)設(shè)置pencolor=color1, fillcolor=color2

turtle.filling()????返回當(dāng)前是否在填充狀態(tài)

turtle.begin_fill()????準(zhǔn)備開(kāi)始填充圖形

turtle.end_fill()????填充完成

turtle.hideturtle()????隱藏畫(huà)筆的turtle形狀

turtle.showturtle()????顯示畫(huà)筆的turtle形狀

表 畫(huà)筆控制命令

命令????????????說(shuō)明

turtle.clear()????清空turtle窗口,但是turtle的位置和狀態(tài)不會(huì)改變

turtle.reset()????清空窗口,重置turtle狀態(tài)為起始狀態(tài)

turtle.undo()????撤銷上一個(gè)turtle動(dòng)作

turtle.isvisible(????)????返回當(dāng)前turtle是否可見(jiàn)

stamp()????復(fù)制當(dāng)前圖形

turtle.write(s [,font=("font-name", font_size, "font_type")])

寫(xiě)文本,s為文本內(nèi)容,font是字體的參數(shù),分別為字體名稱,大小和類型;font為可選項(xiàng),font參數(shù)也是可選項(xiàng)

turtle.clear()????清空turtle窗口,但是turtle的位置和狀態(tài)不會(huì)改變

表 全局控制命令

例子們乘風(fēng)破浪的來(lái)了!此部分參考網(wǎng)絡(luò)資料。有的圖形可以自己參考改造,我等著你們的原創(chuàng),漂亮的圖形會(huì)替換下面的例子!有的圖形復(fù)制一下代碼看看運(yùn)行結(jié)果得了,反正現(xiàn)在也不明白!

(1)五角星

import turtle

turtle.screensize(200,200, "green")

turtle.pencolor("blue")

turtle.pensize(2)

turtle.speed(2)

?

turtle.penup()

turtle.goto(-100,50)

turtle.pendown()

turtle.color("red")

turtle.begin_fill()

for i in range(5):

??? turtle.forward(200)

??? turtle.right(144)

turtle.end_fill()

turtle.hideturtle()

turtle.done()

(2)心形

import turtle

turtle.color('red','pink')

turtle.begin_fill()

turtle.left(135)

turtle.fd(100)

turtle.right(180)

turtle.circle(50,-180)

turtle.left(90)

turtle.circle(50,-180)

turtle.right(180)

turtle.fd(100)

turtle.end_fill()

turtle.hideturtle()

turtle.done()

(3)太陽(yáng)花

import turtle

turtle.color("red", "yellow")

turtle.begin_fill()

for _ in range(50):

??? turtle.forward(200)

??? turtle.left(170)

turtle.end_fill()

turtle.hideturtle()

turtle.done()

(4)peppa pig

import turtle

turtle.screensize(200,200, "green")

turtle.pensize(4)

turtle.hideturtle()

turtle.colormode(255)

turtle.color((255, 155, 192), "pink")

turtle.setup(840, 500)

turtle.speed(10)

# 鼻子

turtle.pu()

turtle.goto(-100, 100)

turtle.pd()

turtle.seth(-30)

turtle.begin_fill()

a = 0.4

for i in range(120):

??? if 0 <= i < 30 or 60 <= i < 90:

??????? a = a + 0.08

??????? turtle.lt(3)? # 向左轉(zhuǎn)3度

??????? turtle.fd(a)? # 向前走a的步長(zhǎng)

??? else:

??????? a = a - 0.08

??????? turtle.lt(3)

??????? turtle.fd(a)

turtle.end_fill()

?

turtle.pu()

turtle.seth(90)

turtle.fd(25)

turtle.seth(0)

turtle.fd(10)

turtle.pd()

turtle.pencolor(255, 155, 192)

turtle.seth(10)

turtle.begin_fill()

turtle.circle(5)

turtle.color(160, 82, 45)

turtle.end_fill()

?

turtle.pu()

turtle.seth(0)

turtle.fd(20)

turtle.pd()

turtle.pencolor(255, 155, 192)

turtle.seth(10)

turtle.begin_fill()

turtle.circle(5)

turtle.color(160, 82, 45)

turtle.end_fill()

# 頭

turtle.color((255, 155, 192), "pink")

turtle.pu()

turtle.seth(90)

turtle.fd(41)

turtle.seth(0)

turtle.fd(0)

turtle.pd()

turtle.begin_fill()

turtle.seth(180)

turtle.circle(300, -30)

turtle.circle(100, -60)

turtle.circle(80, -100)

turtle.circle(150, -20)

turtle.circle(60, -95)

turtle.seth(161)

turtle.circle(-300, 15)

turtle.pu()

turtle.goto(-100, 100)

turtle.pd()

turtle.seth(-30)

a = 0.4

for i in range(60):

??? if 0 <= i < 30 or 60 <= i < 90:

??????? a = a + 0.08

??????? turtle.lt(3)? # 向左轉(zhuǎn)3度

??????? turtle.fd(a)? # 向前走a的步長(zhǎng)

??? else:

??????? a = a - 0.08

??????? turtle.lt(3)

??????? turtle.fd(a)

turtle.end_fill()

# 耳朵

turtle.color((255, 155, 192), "pink")

turtle.pu()

turtle.seth(90)

turtle.fd(-7)

turtle.seth(0)

turtle.fd(70)

turtle.pd()

turtle.begin_fill()

turtle.seth(100)

turtle.circle(-50, 50)

turtle.circle(-10, 120)

turtle.circle(-50, 54)

turtle.end_fill()

?

turtle.pu()

turtle.seth(90)

turtle.fd(-12)

turtle.seth(0)

turtle.fd(30)

turtle.pd()

turtle.begin_fill()

turtle.seth(100)

turtle.circle(-50, 50)

turtle.circle(-10, 120)

turtle.circle(-50, 56)

turtle.end_fill()

# 眼睛

turtle.color((255, 155, 192), "white")

turtle.pu()

turtle.seth(90)

turtle.fd(-20)

turtle.seth(0)

turtle.fd(-95)

turtle.pd()

turtle.begin_fill()

turtle.circle(15)

turtle.end_fill()

?

turtle.color("black")

turtle.pu()

turtle.seth(90)

turtle.fd(12)

turtle.seth(0)

turtle.fd(-3)

turtle.pd()

turtle.begin_fill()

turtle.circle(3)

turtle.end_fill()

?

turtle.color((255, 155, 192), "white")

turtle.pu()

turtle.seth(90)

turtle.fd(-25)

turtle.seth(0)

turtle.fd(40)

turtle.pd()

turtle.begin_fill()

turtle.circle(15)

turtle.end_fill()

?

turtle.color("black")

turtle.pu()

turtle.seth(90)

turtle.fd(12)

turtle.seth(0)

turtle.fd(-3)

turtle.pd()

turtle.begin_fill()

turtle.circle(3)

turtle.end_fill()

# 腮

turtle.color((255, 155, 192))

turtle.pu()

turtle.seth(90)

turtle.fd(-95)

turtle.seth(0)

turtle.fd(65)

turtle.pd()

turtle.begin_fill()

turtle.circle(30)

turtle.end_fill()

# 嘴turtle.color(239, 69, 19)

turtle.pu()

turtle.seth(90)

turtle.fd(15)

turtle.seth(0)

turtle.fd(-100)

turtle.pd()

turtle.seth(-80)

turtle.circle(30, 40)

turtle.circle(40, 80)

# 身體

turtle.color("red", (255, 99, 71))

turtle.pu()

turtle.seth(90)

turtle.fd(-20)

turtle.seth(0)

turtle.fd(-78)

turtle.pd()

turtle.begin_fill()

turtle.seth(-130)

turtle.circle(100, 10)

turtle.circle(300, 30)

turtle.seth(0)

turtle.fd(230)

turtle.seth(90)

turtle.circle(300, 30)

turtle.circle(100, 3)

turtle.color((255, 155, 192), (255, 100, 100))

turtle.seth(-135)

turtle.circle(-80, 63)

turtle.circle(-150, 24)

turtle.end_fill()

# 手

turtle.color((255, 125, 192))

turtle.pu()

turtle.seth(90)

turtle.fd(-40)

turtle.seth(0)

turtle.fd(-27)

turtle.pd()

turtle.seth(-160)

turtle.circle(300, 15)

turtle.pu()

turtle.seth(90)

turtle.fd(15)

turtle.seth(0)

turtle.fd(0)

turtle.pd()

turtle.seth(-10)

turtle.circle(-20, 90)

?

turtle.pu()

turtle.seth(90)

turtle.fd(30)

turtle.seth(0)

turtle.fd(237)

turtle.pd()

turtle.seth(-20)

turtle.circle(-300, 15)

turtle.pu()

turtle.seth(90)

turtle.fd(20)

turtle.seth(0)

turtle.fd(0)

turtle.pd()

turtle.seth(-170)

turtle.circle(20, 90)

# 腳

turtle.pensize(10)

turtle.color((112, 128, 128))

turtle.pu()

turtle.seth(90)

turtle.fd(-75)

turtle.seth(0)

turtle.fd(-180)

turtle.pd()

turtle.seth(-90)

turtle.fd(40)

turtle.seth(-180)

turtle.color("black")

turtle.pensize(15)

turtle.fd(20)

?

turtle.pensize(10)

turtle.color((240, 128, 128))

turtle.pu()

turtle.seth(90)

turtle.fd(40)

turtle.seth(0)

turtle.fd(90)

turtle.pd()

turtle.seth(-90)

turtle.fd(40)

turtle.seth(-180)

turtle.color("black")

turtle.pensize(15)

turtle.fd(20)

# 尾巴

turtle.pensize(4)

turtle.color((255, 155, 192))

turtle.pu()

turtle.seth(90)

turtle.fd(70)

turtle.seth(0)

turtle.fd(95)

turtle.pd()

turtle.seth(0)

turtle.circle(70, 20)

turtle.circle(10, 330)

turtle.circle(70, 30)

turtle.done()

(5)櫻花樹(shù):這個(gè)就屬于復(fù)制一下代碼看看運(yùn)行結(jié)果得了,反正現(xiàn)在也不明白!

import turtle as T

import random

import time

?

# 畫(huà)櫻花的軀干(60,t)

def Tree(branch, t):

??? time.sleep(0.0005)

??? if branch > 3:

??????? if 8 <= branch <= 12:

??????????? if random.randint(0, 2) == 0:

??????????????? t.color('snow')? # 白

??????????? else:

??????????????? t.color('lightcoral')? # 淡珊瑚色

??????????? t.pensize(branch / 3)

?????? ?elif branch < 8:

??????????? if random.randint(0, 1) == 0:

??????????????? t.color('snow')

??????????? else:

??????????????? t.color('lightcoral')? # 淡珊瑚色

??????????? t.pensize(branch / 2)

??????? else:

??????????? t.color('sienna')? # 赭(zhě)色

??????????? t.pensize(branch / 10)? # 6

??????? t.forward(branch)

??????? a = 1.5 * random.random()

??????? t.right(20 * a)

??????? b = 1.5 * random.random()

??????? Tree(branch - 10 * b, t)

??????? t.left(40 * a)

??????? Tree(branch - 10 * b, t)

??????? t.right(20 * a)

??????? t.up()

??????? t.backward(branch)

??????? t.down()

?

# 掉落的花瓣

def Petal(m, t):

??? for i in range(m):

??????? a = 200 - 400 * random.random()

??????? b = 10 - 20 * random.random()

??????? t.up()

??????? t.forward(b)

??????? t.left(90)

??????? t.forward(a)

??????? t.down()

??????? t.color('lightcoral')? # 淡珊瑚色

??????? t.circle(1)

??????? t.up()

??????? t.backward(a)

??????? t.right(90)

??????? t.backward(b)

?

# 繪圖區(qū)域

t = T.Turtle()

# 畫(huà)布大小

w = T.Screen()

t.hideturtle()? # 隱藏畫(huà)筆

t.getscreen().tracer(5, 0)

w.screensize(bg='wheat')? # wheat小麥

t.left(90)

t.up()

t.backward(150)

t.down()

t.color('sienna')

?

# 畫(huà)櫻花的軀干

Tree(60, t)

# 掉落的花瓣

Petal(200, t)

w.exitonclick()

(6)鬧鐘:這個(gè)是目前難度最大的,復(fù)制一下代碼看看運(yùn)行結(jié)果得了,反正現(xiàn)在也不明白!

import turtle

from datetime import *

?

# 抬起畫(huà)筆,向前運(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)

??? # 開(kāi)始記錄多邊形的頂點(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()

??? # 隱藏畫(huà)筆的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),, font=("Courier", 14, "bold"))

??????????? elif i == 30:

??????????????? Skip(25)

??????????????? turtle.write(int(i / 5),, font=("Courier", 14, "bold"))

??????????????? Skip(-25)

??????????? elif (i == 25 or i == 35):

??????????????? Skip(20)

??????????????? turtle.write(int(i / 5),, font=("Courier", 14, "bold"))

??????????????? Skip(-20)

? ??????????else:

??????????????? turtle.write(int(i / 5),, 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),,

????????????????? font=("Courier", 14, "bold"))

??? printer.back(130)

??? printer.write(Date(t),,

????????????????? font=("Courier", 14, "bold"))

??? printer.home()

??? turtle.tracer(True)

?

??? # 100ms后繼續(xù)調(diào)用tick

??? turtle.ontimer(Tick, 100)


def main():

??? # 關(guān)閉動(dòng)畫(huà),并為更新圖紙?jiān)O(shè)置延遲。

??? turtle.tracer(False)

??? Init()

??? SetupClock(160)

??? turtle.tracer(True)

??? Tick()

??? turtle.mainloop()

?

if __name__ == "__main__":

??? main()

小白自學(xué)筆記之Python入門(mén)-第四章 常用模塊-turtle的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
临漳县| 桂阳县| 晋城| 吉水县| 临汾市| 邓州市| 天门市| 漯河市| 长汀县| 繁昌县| 辉南县| 翼城县| 图片| 顺义区| 张掖市| 靖边县| 八宿县| 荣昌县| 邹平县| 鄂托克旗| 西宁市| 梧州市| 延寿县| 江孜县| 荆州市| 滦南县| 通海县| 绍兴县| 商南县| 丰城市| 枝江市| 阿克陶县| 桓台县| 呼图壁县| 洞头县| 墨脱县| 睢宁县| 新蔡县| 晴隆县| 保德县| 福海县|