當前位置: 華文星空 > 體育

用turtle庫畫「冰墩墩」和奧運五環

2023-02-01體育

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

< style data-emotion-css="19xugg7"> .css-19xugg7{position:absolute;width:100%;bottom:0;background-image:linear-gradient(to bottom,transparent,#ffffff 50px);} < style data-emotion-css="12cv0pi"> .css-12cv0pi{box-sizing:border-box;margin:0;min-width:0;height:100px;-webkit-box-pack:center;-webkit-justify-content:center;-ms-flex-pack:center;justify-content:center;display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;position:absolute;width:100%;bottom:0;background-image:linear-gradient(to bottom,transparent,#ffffff 50px);}
< style data-emotion-css="1pr2waf"> .css-1pr2waf{font-size:15px;color:#09408e;}
編輯於 2023-02-01 20:49 IP 屬地浙江
< style data-emotion-css="ch8ocw"> .css-ch8ocw{position:relative;display:inline-block;height:30px;padding:0 12px;font-size:14px;line-height:30px;color:#1772F6;vertical-align:top;border-radius:100px;background:rgba(23,114,246,0.1);}.css-ch8ocw:hover{background-color:rgba(23,114,246,0.15);}
< style data-emotion-css="1xlfegr"> .css-1xlfegr{background:transparent;box-shadow:none;} < style data-emotion-css="1gomreu"> .css-1gomreu{position:relative;display:inline-block;}
turtle畫圖