20211321 李心怡《Python程式設計》實驗四 Python綜合實踐實驗報告
課程 《python程式設計》
班級 2113
姓名 李心怡
學號 20211321
實驗教師 王志強
實驗日期 2021/5/28
必修/選修 公選課
Python海龜庫的使用
使用python畫了一個倒立的小鳥
開始是因為覺得微信表情包裡的小鳥很可愛,在聽到有python 大作業,瞭解到有Python海龜庫畫圖之後,決定用Python編出一個小鳥的圖案(雖然不是很像哈哈哈哈)
自己的圖畫
附上程式碼
import turtle as t t.setup(800, 850) t.speed(0)#速度 #地板 t.penup() t.goto(-250, -200) t.pencolor("black") t.pensize(3) t.pendown() t.setheading(0) t.forward(500) #身子 t.penup() t.goto(116, -168) t.pensize(2) t.fillcolor("gold") t.begin_fill() t.pendown() t.setheading(40) t.circle(250, 120) t.circle(200, 30) t.circle(235,150) t.end_fill() #腦袋 t.penup() t.goto(135, -200) t.pencolor("black") t.fillcolor("gold") t.pensize(2) t.begin_fill() t.pendown() t.setheading(90) t.circle(100, 40) t.circle(150, 100) t.circle(100, 40) t.end_fill() #左眼 t.penup() t.goto(-90, -168) t.pencolor("black") t.fillcolor("white") t.pensize(1) t.begin_fill() t.pendown() t.circle(32,360) t.end_fill() #右眼 t.penup() t.goto(26, -168) t.pencolor("black") t.fillcolor("white") t.pensize(1) t.begin_fill() t.pendown() t.circle(32, 360) t.end_fill() #左眼珠 t.penup() t.goto(-80, -168) t.pencolor("black") t.fillcolor("black") t.pensize(1) t.begin_fill() t.pendown() t.circle(22, 360) t.end_fill() #右眼珠 t.penup() t.goto(36, -168) t.pencolor("black") t.fillcolor("black") t.pensize(1) t.begin_fill() t.pendown() t.circle(22, 360) t.end_fill() #下嘴 t.penup() t.goto(-27, -140) t.pensize(3) t.fillcolor("saddle brown") t.begin_fill() t.pendown() t.setheading(285) t.forward(100) t.setheading(77) t.forward(100) t.end_fill() #嘴 t.penup() t.goto(0, -110) t.pencolor("black") t.pensize(3) t.fillcolor("saddle brown") t.begin_fill() t.pendown() t.setheading(-50) t.forward(40) t.setheading(230) t.forward(40) t.setheading(130) t.forward(40) t.setheading(50) t.forward(40) t.end_fill() #尾巴 t.penup() t.goto(-45, 180) t.pensize(3) t.fillcolor("DarkOrange3") t.begin_fill() t.pendown() t.setheading(60) t.forward(270) t.setheading(263) t.forward(250) t.end_fill() #肚飢 t.penup() t.goto(-50, 180) t.pendown() t.setheading(20) t.circle(-75, 70) #肚子上的叉叉 t.penup() t.pensize(3) t.goto(10, 165) t.pendown() t.setheading(-130) t.circle(-35, 80) t.penup() t.goto(-20, 175) t.setheading(-75) t.pendown() t.circle(35, 80) #左腿 t.penup() t.fillcolor("gold") t.begin_fill() t.goto(-70, -75) t.pendown() t.goto(-75, -90) t.penup() t.goto(-85, -85) t.pendown() t.goto(-80, -90) t.goto(-63, -100) t.pendown() t.goto(-190, -200) t.goto(-200, -110) t.end_fill() #右腿 t.penup() t.fillcolor("gold") t.begin_fill() t.goto(100,-90) t.pendown() t.goto(140, -200) t.goto(183,-75) t.end_fill() # 羽毛 t.penup() t.fillcolor("gold") t.begin_fill() t.goto(-205, 180) t.pendown() t.setheading(-90) t.circle(-200, 40) t.goto(-220, 90) t.setheading(60) t.circle(-20, 180) t.setheading(250) t.circle(-200, 40) t.setheading(20) t.circle(-270, 20) t.circle(-20, 120) t.circle(-200, 20) t.goto(-195, -155) #左翅膀 t.penup() t.goto(-205, 180) t.pendown() t.goto(-230, 230) t.setheading(90) t.circle(30,145) t.circle(350, 30) t.circle(220, 78) t.goto(-195, -155) t.end_fill() #右翅膀 t.penup() t.goto(150, 170) t.fillcolor("gold") t.begin_fill() t.pendown() t.goto(220, 240) t.setheading(20) t.circle(-30, 80) t.setheading(-90) t.circle(-200, 50) t.setheading(40) t.circle(-150, 35) t.circle(-20, 110) t.circle(-200, 45) t.setheading(10) t.circle(-200, 10) t.circle(-15, 140) t.circle(-200, 23) t.end_fill() t.setheading(60) t.circle(250, 70) # 腳 t.penup() t.goto(-190, -200) t.pendown() t.goto(-240, -205) t.penup() t.goto(-190, -200) t.pendown() t.goto(-160, -203) t.penup() t.goto(140, -200) t.pendown() t.goto(180, -204) t.mainloop() t.down()
由於畫得熟練了,就拿小豬佩奇再一次試手
小豬佩奇
附上程式碼
import turtle as t t.pensize(4) t.hideturtle() t.colormode(255) t.color((255, 155, 192), "pink") t.setup(840, 500) t.speed(20) # 鼻子 t.pu() t.goto(-100, 100) t.pd() t.seth(-30) t.begin_fill() a = 0.4 for i in range(120): if 0 <= i < 30 or 60 <= i < 90: a = a + 0.08 t.lt(3) # 向左轉3度 t.fd(a) # 向前走a的步長 else: a = a - 0.08 t.lt(3) t.fd(a) t.end_fill() t.pu() t.seth(90) t.fd(25) t.seth(0) t.fd(10) t.pd() t.pencolor(255, 155, 192) t.seth(10) t.begin_fill() t.circle(5) t.color(160, 82, 45) t.end_fill() t.pu() t.seth(0) t.fd(20) t.pd() t.pencolor(255, 155, 192) t.seth(10) t.begin_fill() t.circle(5) t.color(160, 82, 45) t.end_fill() # 頭 t.color((255, 155, 192), "pink") t.pu() t.seth(90) t.fd(41) t.seth(0) t.fd(0) t.pd() t.begin_fill() t.seth(180) t.circle(300, -30) t.circle(100, -60) t.circle(80, -100) t.circle(150, -20) t.circle(60, -95) t.seth(161) t.circle(-300, 15) t.pu() t.goto(-100, 100) t.pd() t.seth(-30) a = 0.4 for i in range(60): if 0 <= i < 30 or 60 <= i < 90: a = a + 0.08 t.lt(3) # 向左轉3度 t.fd(a) # 向前走a的步長 else: a = a - 0.08 t.lt(3) t.fd(a) t.end_fill() # 耳朵 t.color((255, 155, 192), "pink") t.pu() t.seth(90) t.fd(-7) t.seth(0) t.fd(70) t.pd() t.begin_fill() t.seth(100) t.circle(-50, 50) t.circle(-10, 120) t.circle(-50, 54) t.end_fill() t.pu() t.seth(90) t.fd(-12) t.seth(0) t.fd(30) t.pd() t.begin_fill() t.seth(100) t.circle(-50, 50) t.circle(-10, 120) t.circle(-50, 56) t.end_fill() # 眼睛 t.color((255, 155, 192), "white") t.pu() t.seth(90) t.fd(-20) t.seth(0) t.fd(-95) t.pd() t.begin_fill() t.circle(15) t.end_fill() t.color("black") t.pu() t.seth(90) t.fd(12) t.seth(0) t.fd(-3) t.pd() t.begin_fill() t.circle(3) t.end_fill() t.color((255, 155, 192), "white") t.pu() t.seth(90) t.fd(-25) t.seth(0) t.fd(40) t.pd() t.begin_fill() t.circle(15) t.end_fill() t.color("black") t.pu() t.seth(90) t.fd(12) t.seth(0) t.fd(-3) t.pd() t.begin_fill() t.circle(3) t.end_fill() # 腮 t.color((255, 155, 192)) t.pu() t.seth(90) t.fd(-95) t.seth(0) t.fd(65) t.pd() t.begin_fill() t.circle(30) t.end_fill() # 嘴 t.color(239, 69, 19) t.pu() t.seth(90) t.fd(15) t.seth(0) t.fd(-100) t.pd() t.seth(-80) t.circle(30, 40) t.circle(40, 80) # 身體 t.color("red", (255, 99, 71)) t.pu() t.seth(90) t.fd(-20) t.seth(0) t.fd(-78) t.pd() t.begin_fill() t.seth(-130) t.circle(100, 10) t.circle(300, 30) t.seth(0) t.fd(230) t.seth(90) t.circle(300, 30) t.circle(100, 3) t.color((255, 155, 192), (255, 100, 100)) t.seth(-135) t.circle(-80, 63) t.circle(-150, 24) t.end_fill() # 手 t.color((255, 155, 192)) t.pu() t.seth(90) t.fd(-40) t.seth(0) t.fd(-27) t.pd() t.seth(-160) t.circle(300, 15) t.pu() t.seth(90) t.fd(15) t.seth(0) t.fd(0) t.pd() t.seth(-10) t.circle(-20, 90) t.pu() t.seth(90) t.fd(30) t.seth(0) t.fd(237) t.pd() t.seth(-20) t.circle(-300, 15) t.pu() t.seth(90) t.fd(20) t.seth(0) t.fd(0) t.pd() t.seth(-170) t.circle(20, 90) # 腳 t.pensize(10) t.color((240, 128, 128)) t.pu() t.seth(90) t.fd(-75) t.seth(0) t.fd(-180) t.pd() t.seth(-90) t.fd(40) t.seth(-180) t.color("black") t.pensize(15) t.fd(20) t.pensize(10) t.color((240, 128, 128)) t.pu() t.seth(90) t.fd(40) t.seth(0) t.fd(90) t.pd() t.seth(-90) t.fd(40) t.seth(-180) t.color("black") t.pensize(15) t.fd(20) # 尾巴 t.pensize(4) t.color((255, 155, 192)) t.pu() t.seth(90) t.fd(70) t.seth(0) t.fd(95) t.pd() t.seth(0) t.circle(70, 20) t.circle(10, 330) t.circle(70, 30) t.exitonclick()
本次的實驗總結
- 實驗所學習的函式:
(1)畫筆運動命令:
turtle. forward(distance):向當前畫筆方向移動distance畫素長
turtle. backward(distance):向當前畫筆相反方向移動distance畫素長度
turtle. right(degree):順時針移動degree"
turtle. left(degree):逆時針移動degree"
turtle. pendown():移動時繪製圖形,預設時也為繪製
turtle. goto(x,y):將畫筆移動到座標為x,y的位置
turtle. penup[b:移動時不繪製圖形,提起筆,用於另起一個地方繪製時用
turtle. speed(speed):畫筆繪製的速度範圍[0,10]整數
turtle. circle():畫圓,半徑為正(負),表示圓心在畫筆的左邊(右邊)畫圓
setx():將當前x軸移動到指定位置
sety():將當前y軸移動到指定位置
setheading (angle):設定當前朝向為angle角度
home():設定當前畫筆位置為原點,朝向東。
(2)畫筆控制命令:
turtle. pensize(width):繪製圖形時的寬度
turtle. pencolor():畫筆顏色
turtle:fillcolor( colorstring ):繪製圖形的填充顏色
turtle color(color),color 2):同時設定pencolor=color 1,fillColor=color2
turtle,filling():返回當前是否在填充狀態
turtle. begin fill():準備開始填充圖形
turtle. end fill():填充完成;
turtle. hideturtle ():隱藏箭頭顯示;
turtle. showturtle ():與 hideturtle ()函式對應
(3)全域性控制命令
turtle. clear()清空turtle視窗,但是turtle的位置和狀態不會改變
turtle. reset()清空視窗,重置turtle狀態為起始狀態
turtle. undo()撤銷上一個turtle動作
turtle. isvisible()返回當前turtle是否可見,
stamp()複製當前圖形
實驗分析
- 實驗分析:
本實驗使用了python 的庫turtle,。 - 設計:
開始看到用python可以實現畫畫, 便想要自己實踐一下。看到微信上的表情包很可愛,便想通過python 實現繪畫表情包。 - 實現過程:
我先在網上搜索了Python的turtle庫的用法。瞭解到它有turtle.fillcolor, turtle.circle()等用法,便開始著手開畫。 - 問題:
- 找點很困難。
每一次在電腦上找點是一件困難的事,我便先手動畫下這圖案,然後再在電腦上去找點。找點的時候也是找不到自己想要的點,後來通過查詢發現原點在正中央。 - 線條很單一。
python畫圖只有直線和圓這兩個圖案,如何畫出橢圓和其它的圓滑的線條呢?通過網上查詢,我發現可以使用多個不同半徑的圓連在一起來實現畫出橢圓,在直線的末端使用圓便可以畫出光滑的曲線。 - 填充色彩會出線條。
當我用不同顏色的時候,有時候便會出現顏色填充很溢位的現象。我通過兩個多小時的實驗,終於弄明白了,當有圓存在的時候,便是填充圓的圓心那一半的顏色。如果自己的圖案沒有封閉,顏色填充變會溢位。解決辦法便是將始末的點都設為一個。
- 找點很困難。
通過我的艱難努力,終於實現了第一個倒立的鳥的簡化版。想要使我的python繪畫技術熟練,我又畫了一個小豬佩奇。
python 課程的知識點總結
課程體會和建議
因為上學期在婁老師的課上,我曾對python有一定的瞭解,這次的選修課讓我對它有更加深切的感悟。老師拓展了我們的知識面,不僅讓我們學習python,還讓我們瞭解了正則表示式;老師的課上實踐對我們幫助也很大,給我們當場用python演示算術運算。要真切瞭解一門語言,不僅要了解它的語法,更要在實踐中應用才能更加得心應手;老師在也讓我們瞭解了很多python的實際應用,比如監聽技術,資料視覺化等,使我們學習的python有了更多的用武之地。王老師的課極大地激發了我學習python 的興趣,python 的多樣化應用也讓我對它更為著迷。
課程建議: 老師可以把上課的ppt發放到群裡,讓沒有聽懂的同學下課後可以繼續看看,加深對python 的理解。
最後,謝謝老師深博的知識對我影響和老師對我學習中的問題耐心的回答!!!