python學習資料如下,內容十分全面,十分適合初學者。
turtle是python內建的畫圖函式庫,功能十分強大,本文教大家畫奧運五環和此次北京冬奧會吉祥物(冰墩墩)。
沒有安裝python的朋友可以去看我的教程: 與python有關的文章都在我的專欄裏面:畫冰墩墩1:
在此之前你需要一張冰墩墩的圖片, 命名為bingdundun.png (當然你也可以根據你找的圖片改程式碼裏面的圖片名稱),和python程式碼在同一個目錄下(或者程式碼裏面圖片使用絕對路徑)。
完整程式碼1:
import
turtle
as
t
import
cv2
t
.
getscreen
()
.
colormode
(
255
)
img1
=
cv2
.
imread
(
'bingdundun.png'
)[
0
:
-
2
:
2
]
width
=
len
(
img1
[
0
])
height
=
len
(
img1
)
t
.
setup
(
width
=
width
/
2
+
100
,
height
=
height
+
100
)
t
.
pu
()
t
.
goto
(
-
width
/
4
+
10
,
height
/
2
-
10
)
t
.
pd
()
t
.
tracer
(
2000
)
for
k1
,
i
in
enumerate
(
img1
):
for
j
in
i
[::
2
]:
t
.
pencolor
((
j
[
0
],
j
[
1
],
j
[
2
]))
t
.
fd
(
1
)
t
.
pu
()
t
.
goto
(
-
width
/
4
+
10
,
height
/
2
-
10
-
k1
-
1
)
t
.
pd
()
t
.
done
()
效果1:
畫冰墩墩2:
上面的程式碼依賴cv2庫,生成的效果和找的圖片有關,下面給出完全使用turtle庫畫的冰墩墩程式碼。
完整程式碼2:
#北京奧運會吉祥錄制
import
turtle
turtle
.
title
(
'送你一個冰墩墩'
)
turtle
.
speed
(
40
)
# 可以自己調節速度
# 左手
turtle
.
penup
()
turtle
.
goto
(
177
,
112
)
turtle
.
pencolor
(
"lightgray"
)
turtle
.
pensize
(
3
)
turtle
.
fillcolor
(
"white"
)
turtle
.
begin_fill
()
turtle
.
pendown
()
turtle
.
setheading
(
80
)
turtle
.
circle
(
-
45
,
200
)
turtle
.
circle
(
-
300
,
23
)
turtle
.
end_fill
()
# 左手內
turtle
.
penup
()
turtle
.
goto
(
182
,
95
)
turt