当前位置: 华文星空 > 体育

用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画图