【Python Turtle合集】有趣好玩的代码当然要分享给大家啦~(皮卡丘、玫瑰花、小黄人......)
生活随笔
收集整理的這篇文章主要介紹了
【Python Turtle合集】有趣好玩的代码当然要分享给大家啦~(皮卡丘、玫瑰花、小黄人......)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
前言
🚀 作者 :“程序員梨子”
🚀 **文章簡介 **:本篇文章主要是寫了利用Turtle庫繪制四種不一樣的圖案的小程序!
🚀 **文章源碼免費獲取 : 為了感謝每一個關(guān)注我的小可愛💓每篇文章的項目源碼都是無
償分享滴💓👇👇👇👇
點這里藍色這行字體自取,需要什么源碼記得說標題名字哈!私信我也可!
🚀 歡迎小伙伴們 點贊👍、收藏?、留言💬
正文
今天給大家介紹一個好玩兒的庫,嘻嘻!
Turtle庫是Python語言中一個很流行的繪制圖像的函數(shù)庫,想象一個小烏龜,在一個橫軸為x、縱
軸為y的坐標系原點,(0,0)位置開始,它根據(jù)一組函數(shù)指令的控制,在這個平面坐標系中移動,從
而在它爬行的路徑上繪制了圖形。
1)佩奇
?代碼展示——
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.08t.lt(3) # 向左轉(zhuǎn)3度t.fd(a) # 向前走a的步長else:a = a - 0.08t.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.08t.lt(3) # 向左轉(zhuǎn)3度t.fd(a) # 向前走a的步長else:a = a - 0.08t.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()2)小黃人
?代碼展示——
import turtle as t # t = turtle.Turtle() wn = t.Screen() t.colormode(255) t.hideturtle() t.speed(0) t.penup() t.pensize(4) t.goto(100,0) t.pendown() t.left(90) t.color((0,0,0),(255,255,0)) #身體繪制上色 t.begin_fill() t.forward(200) t.circle(100,180) t.forward(200) t.circle(100,180) t.end_fill() #右眼睛繪制上色 t.pensize(12) t.penup() t.goto(-100,200) t.pendown() t.right(100) t.circle(500,23) t.pensize(3) t.penup() t.goto(0,200) t.pendown() t.seth(270) t.color("black","white") t.begin_fill() t.circle(30) t.end_fill() t.penup() t.goto(15,200) t.pendown() t.color("black","black") t.begin_fill() t.circle(15) t.end_fill() t.penup() t.goto(35,205) t.color("black","white") t.begin_fill() t.circle(5) t.end_fill() #左眼睛繪制上色 t.pensize(3) t.penup() t.goto(0,200) t.pendown() t.seth(90) t.color("black","white") t.begin_fill() t.circle(30) t.end_fill() t.penup() t.goto(-15,200) t.pendown() t.color("black","black") t.begin_fill() t.circle(15) t.end_fill() t.penup() t.goto(-35,205) t.color("black","white") t.begin_fill() t.circle(5) t.end_fill() #嘴繪制上色 t.penup() t.goto(-20,100) t.pendown() t.seth(270) t.color("black","white") t.begin_fill() t.circle(20,180) t.left(90) t.forward(40) t.end_fill() #褲子繪制上色 t.penup() t.goto(-100,0) t.pendown() t.seth(0) t.color("black","blue") t.begin_fill() t.forward(20) t.left(90) t.forward(40) t.right(90) t.forward(160) t.right(90) t.forward(40) t.left(90) t.forward(20) t.seth(270) t.penup() t.goto(-100,0) t.circle(100,180) t.end_fill() #左褲子腰帶 t.penup() t.goto(-70,20) t.pendown() t.color("black","blue") t.begin_fill() t.seth(45) t.forward(15) t.left(90) t.forward(60) t.seth(270) t.forward(15) t.left(40) t.forward(50) t.end_fill() t.left(180) t.goto(-70,30) t.dot() #右褲腰帶 t.penup() t.goto(70,20) t.pendown() t.color("black","blue") t.begin_fill() t.seth(135) t.forward(15) t.right(90) t.forward(60) t.seth(270) t.forward(15) t.right(40) t.forward(50) t.end_fill() t.left(180) t.goto(70,30) t.dot() #腳 t.penup() t.goto(4,-100) t.pendown() t.seth(270) t.color("black","black") t.begin_fill() t.forward(30) t.left(90) t.forward(40) t.seth(20) t.circle(10,180) t.circle(400,2) t.seth(90) t.forward(20) t.goto(4,-100) t.end_fill() t.penup() t.goto(-4,-100) t.pendown() t.seth(270) t.color("black","black") t.begin_fill() t.forward(30) t.right(90) t.forward(40) t.seth(20) t.circle(10,-225) t.circle(400,-3) t.seth(90) t.forward(21) t.goto(-4,-100) t.end_fill() #左手 t.penup() t.goto(-100,50) t.pendown() t.seth(225) t.color("black","yellow") t.begin_fill() t.forward(40) t.left(90) t.forward(35) t.seth(90) t.forward(50) t.end_fill() #右手 t.penup() t.goto(100,50) t.pendown() t.seth(315) t.color("black","yellow") t.begin_fill() t.forward(40) t.right(90) t.forward(36) t.seth(90) t.forward(50) t.end_fill() # t.penup() t.goto(0,-100) t.pendown() t.forward(30) # t.penup() t.goto(0,-20) t.pendown() t.color("yellow") t.begin_fill() t.seth(45) t.forward(20) t.circle(10,180) t.right(90) t.circle(10,180) t.forward(20) t.end_fill() # t.penup() t.color("black") t.goto(-100,-20) t.pendown() t.circle(30,90) t.penup() t.goto(100,-20) t.pendown() t.circle(30,-90) #頭頂 t.penup() t.goto(2,300) t.pendown() t.begin_fill() t.seth(135) t.circle(100,40) t.end_fill() t.penup() t.goto(2,300) t.pendown() t.begin_fill() t.seth(45) t.circle(100,40) t.exitonclick()3)皮卡丘
?代碼展示——
# 皮卡丘 # 基礎(chǔ)設(shè)置 t.screensize(800, 600) t.pensize(2) # 設(shè)置畫筆的大小 t.speed(10) # 設(shè)置畫筆速度為10 # 畫左偏曲線函數(shù) def radian_left(ang, dis, step, n):for i in range(n):dis += step # dis增大stept.lt(ang) # 向左轉(zhuǎn)ang度t.fd(dis) # 向前走dis的步長 def radian_right(ang, dis, step, n):for i in range(n):dis += stept.rt(ang) # 向左轉(zhuǎn)ang度t.fd(dis) # 向前走dis的步長 # 畫耳朵 def InitEars():t.color("black", "yellow")# 左耳朵曲線t.pu() # 提筆t.goto(-50, 100) # 筆頭初始位置t.pd() # 下筆t.setheading(110) # 畫筆角度t.begin_fill()radian_left(1.2, 0.4, 0.1, 40)t.setheading(270) # 畫筆角度radian_left(1.2, 0.4, 0.1, 40)t.setheading(44) # 畫筆角度t.forward(32)t.end_fill()# 右耳朵曲線t.pu() # 提筆t.goto(50, 100) # 筆頭初始位置t.pd() # 下筆t.setheading(70) # 畫筆角度t.begin_fill()radian_right(1.2, 0.4, 0.1, 40)t.setheading(270) # 畫筆角度radian_right(1.2, 0.4, 0.1, 40)t.setheading(136) # 畫筆角度t.forward(32)t.end_fill()# 耳朵黑t.begin_fill()t.fillcolor("black")t.pu() # 提筆t.goto(88, 141) # 筆頭初始位置t.pd() # 下筆t.setheading(35) # 畫筆角度radian_right(1.2, 1.6, 0.1, 16)t.setheading(270) # 畫筆角度radian_right(1.2, 0.4, 0.1, 25)t.setheading(132) # 畫筆角度t.forward(31)t.end_fill()t.begin_fill()t.fillcolor("black")t.pu() # 提筆t.goto(-88, 141) # 筆頭初始位置t.pd() # 下筆t.setheading(145) # 畫筆角度radian_left(1.2, 1.6, 0.1, 16)t.setheading(270) # 畫筆角度radian_left(1.2, 0.4, 0.1, 25)t.setheading(48) # 畫筆角度t.forward(31)t.end_fill() # 畫尾巴 def InitTail():# 尾巴t.begin_fill()t.fillcolor("yellow")t.pu() # 提筆t.goto(64, -140) # 筆頭初始位置t.pd() # 下筆t.setheading(10) # 畫筆角度t.forward(20)t.setheading(90) # 畫筆角度t.forward(20)t.setheading(10) # 畫筆角度t.forward(10)t.setheading(80) # 畫筆角度t.forward(100)t.setheading(35) # 畫筆角度t.forward(80)t.setheading(260) # 畫筆角度t.forward(100)t.setheading(205) # 畫筆角度t.forward(40)t.setheading(260) # 畫筆角度t.forward(37)t.setheading(205) # 畫筆角度t.forward(20)t.setheading(260) # 畫筆角度t.forward(25)t.setheading(175) # 畫筆角度t.forward(30)t.setheading(100) # 畫筆角度t.forward(13)t.end_fill() # 畫腳 def InitFoots():# 腳t.begin_fill()t.fillcolor("yellow")t.pensize(2)t.pu() # 提筆t.goto(-70, -200) # 筆頭初始位置t.pd() # 下筆t.setheading(225) # 畫筆角度radian_left(0.5, 1.2, 0, 12)radian_left(35, 0.6, 0, 4)radian_left(1, 1.2, 0, 18)t.setheading(160) # 畫筆角度t.forward(13)t.end_fill()t.begin_fill()t.fillcolor("yellow")t.pensize(2)t.pu() # 提筆t.goto(70, -200) # 筆頭初始位置t.pd() # 下筆t.setheading(315) # 畫筆角度radian_right(0.5, 1.2, 0, 12)radian_right(35, 0.6, 0, 4)radian_right(1, 1.2, 0, 18)t.setheading(20) # 畫筆角度t.forward(13)t.end_fill() # 畫身體 def InitBody():# 外形輪廓t.begin_fill()t.pu() # 提筆t.goto(112, 0) # 筆頭初始位置t.pd() # 下筆t.setheading(90) # 畫筆角度t.circle(112, 180)t.setheading(250) # 畫筆角度radian_left(1.6, 1.3, 0, 50)radian_left(0.8, 1.5, 0, 25)t.setheading(255) # 畫筆角度radian_left(0.4, 1.6, 0.2, 27)radian_left(2.8, 1, 0, 45)radian_right(0.9, 1.4, 0, 31)t.setheading(355) # 畫筆角度radian_right(0.9, 1.4, 0, 31)radian_left(2.8, 1, 0, 45)radian_left(0.4, 7.2, -0.2, 27)t.setheading(10) # 畫筆角度radian_left(0.8, 1.5, 0, 25)radian_left(1.6, 1.3, 0, 50)t.end_fill() def InitEyes():# 左眼睛t.begin_fill()t.fillcolor("black")t.pu() # 提筆t.goto(-46, 10) # 筆頭初始位置t.pd() # 下筆t.setheading(90) # 畫筆角度t.circle(5, 360)t.end_fill()# 右眼睛t.begin_fill()t.fillcolor("black")t.pu() # 提筆t.goto(46, 10) # 筆頭初始位置t.pd() # 下筆t.setheading(-90) # 畫筆角度t.circle(5, 360)t.end_fill() # 畫臉 def InitFace():# 臉蛋t.begin_fill()t.fillcolor("red")t.pu() # 提筆t.goto(-63, -10) # 筆頭初始位置t.pd() # 下筆t.setheading(90) # 畫筆角度t.circle(10, 360)t.end_fill()t.begin_fill()t.fillcolor("red")t.pu() # 提筆t.goto(63, -10) # 筆頭初始位置t.pd() # 下筆t.setheading(-90) # 畫筆角度t.circle(10, 360)t.end_fill()# 嘴巴t.pensize(2.2)t.pu() # 提筆t.goto(0, 0) # 筆頭初始位置t.pd() # 下筆t.setheading(235) # 畫筆角度radian_right(5, 0.8, 0, 30)t.pu() # 提筆t.goto(0, 0) # 筆頭初始位置t.pd() # 下筆t.setheading(305) # 畫筆角度radian_left(5, 0.8, 0, 30) # 畫手 def InitHands():# 左手t.pensize(2)t.pu() # 提筆t.goto(-46, -100) # 筆頭初始位置t.pd() # 下筆t.setheading(285) # 畫筆角度radian_right(0.4, 1.2, 0, 26)radian_right(5, 0.35, 0, 26)radian_right(0.3, 1.2, 0, 15)# 右手t.pu() # 提筆t.goto(46, -100) # 筆頭初始位置t.pd() # 下筆t.setheading(255) # 畫筆角度radian_left(0.4, 1.2, 0, 26)radian_left(5, 0.35, 0, 26)radian_left(0.3, 1.2, 0, 15) def CloseEyes():# 左眼睛t.pu() # 提筆t.goto(-46, 12) # 筆頭初始位置t.pd() # 下筆t.setheading(180) # 畫筆角度t.forward(10)# 右眼睛t.pu() # 提筆t.goto(46, 12) # 筆頭初始位置t.pd() # 下筆t.setheading(0) # 畫筆角度t.forward(10) # 初始化 def Init():InitEars()InitTail()InitFoots()InitBody()InitFace()InitHands()InitEyes() # 眨眼睛 def Upgarde():InitEars()InitTail()InitFoots()InitBody()InitFace()InitHands()CloseEyes() def Upgarde_Init():InitEars()InitTail()InitFoots()InitBody()InitFace()InitHands()InitEyes() def main():Init()t.tracer(False)# 眨眼睛動畫for i in range(30):if i % 2 == 0:t.reset()t.hideturtle()Upgarde()t.update()time.sleep(0.3)else:t.reset()t.hideturtle()Upgarde_Init()t.update()time.sleep(1) main() # 結(jié)束畫筆 t.done()4)玫瑰花
?
代碼展示——
import turtleturtle.speed(5)# 設(shè)置初始位置 turtle.penup() turtle.left(90) turtle.fd(200) turtle.pendown() turtle.right(90) # 花蕊 turtle.fillcolor("red") turtle.begin_fill() turtle.circle(10,180) turtle.circle(25,110) turtle.left(50) turtle.circle(60,45) turtle.circle(20,170) turtle.right(24) turtle.fd(30) turtle.left(10) turtle.circle(30,110) turtle.fd(20) turtle.left(40) turtle.circle(90,70) turtle.circle(30,150) turtle.right(30) turtle.fd(15) turtle.circle(80,90) turtle.left(15) turtle.fd(45) turtle.right(165) turtle.fd(20) turtle.left(155) turtle.circle(150,80) turtle.left(50) turtle.circle(150,90) turtle.end_fill() # 花瓣1 turtle.left(150) turtle.circle(-90,70) turtle.left(20) turtle.circle(75,105) turtle.setheading(60) turtle.circle(80,98) turtle.circle(-90,40) # 花瓣2 turtle.left(180) turtle.circle(90,40) turtle.circle(-80,98) turtle.setheading(-83) # 葉子1 turtle.fd(30) turtle.left(90) turtle.fd(25) turtle.left(45) turtle.fillcolor("green") turtle.begin_fill() turtle.circle(-80,90) turtle.right(90) turtle.circle(-80,90) turtle.end_fill() turtle.right(135) turtle.fd(60) turtle.left(180) turtle.fd(85) turtle.left(90) turtle.fd(80) # 葉子2 turtle.right(90) turtle.right(45) turtle.fillcolor("green") turtle.begin_fill() turtle.circle(80,90) turtle.left(90) turtle.circle(80,90) turtle.end_fill() turtle.left(135) turtle.fd(60) turtle.left(180) turtle.fd(60) turtle.right(90) turtle.circle(200,60)總結(jié)
效果很不錯滴,感興趣的朋友可以試試啦~
關(guān)注小編獲取更多精彩內(nèi)容!源碼記得點擊傳送門哈👇👇👇👇
記得三連哦! 如需打包好的源碼+素材免費分享滴!!傳送門
總結(jié)
以上是生活随笔為你收集整理的【Python Turtle合集】有趣好玩的代码当然要分享给大家啦~(皮卡丘、玫瑰花、小黄人......)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Opencv实战】简易版“美颜”来啦—
- 下一篇: 【Python Turtle合集】”外面