python画圣诞树
生活随笔
收集整理的這篇文章主要介紹了
python画圣诞树
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
大家好,我是python的小白,最近剛開始上手python的海龜畫圖!
首先得先安裝海龜Turtle插件,然后給大家分享一波如何使用 Python 來畫一顆簡單易上手的圣誕
樹和平安果,由最基本的圖形(圓形和正方形)組合而成,其中在邊角的地方設置不同的顏色圓形。此外在最底部還設置了字體Merry Christmas字樣,并且帶有雪花圖案!
上圖:?
?
?上代碼:
import turtle as t import random as r screen=t.Screen()screen.screensize(bg='black') screen.setup(750,700) circle = t.Turtle () circle.shape ('circle') circle.color ('red') circle.speed ('fastest') circle.up ()square = t.Turtle () square.shape ('square') square.color ('green') square.speed ('fastest') square.up ()circle.goto(0,280) circle.stamp()k=0 for i in range(1,13):y=i*30for j in range(i-k):x=30*jsquare.goto(x, -y+280)square.stamp()square.goto(-x,-y+280)square.stamp()if i%4==0:x=30*(j+1)circle.color('red')circle.goto(-x,-y+280)circle.stamp()circle.goto(x,-y+280)circle.stamp()k+=3if i%4==3:x=30*(j+1)circle.color('yellow')circle.goto(-x,-y+280)circle.stamp()circle.goto(x,-y+280)circle.stamp()square.color('brown') for i in range(13,17):y=30*ifor j in range(3):x=30*jsquare.goto(x,-y+280)square.stamp()square.goto(-x,-y+280)square.stamp()def drawsnow (): # 定義畫雪花的方法t.ht () # 隱藏筆頭,ht=hideturtlet.pensize (2) # 定義筆頭大小for i in range (200): # 畫多少雪花t.pencolor ("white") # 定義畫筆顏色為白色,其實就是雪花為白色t.pu () # 提筆,pu=penupt.setx (r.randint (-350, 350)) # 定義x坐標,隨機從-350到350之間選擇t.sety (r.randint (-100, 350)) # 定義y坐標,注意雪花一般在地上不會落下,所以不會從太小的縱座軸開始t.pd () # 落筆,pd=pendowndens = 6 # 雪花瓣數設為6snowsize = r.randint (1, 10) # 定義雪花大小for j in range (dens): # 就是6,那就是畫5次,也就是一個雪花五角星t.fd (int (snowsize))t.backward (int (snowsize))t.right (int (360 / dens)) drawsnow () t.done()
# 平安夜 送?平安果
上圖:
上代碼:
import turtle as t screen=t.Screen() screen.screensize(bg="white") screen.setup(800,800) t.speed(3) t.pensize(3) t.goto(0,-200) //以(0,-200)為圓心畫半徑150的紅色圓 t.begin_fill() t.color("red") t.circle(radius=150) t.end_fill()t.color("brown") //畫果實的梗 t.up() t.goto(-90,0) t.pd() t.circle(180,40) t.seth(105) t.up() t.goto(-20,20) t.pd() t.circle(180,50) t.pd()t.begin_fill() //左邊的葉子 t.color("green") t.circle(180,50) t.seth(-30) t.circle(180,55) t.end_fill()t.begin_fill() //右邊的葉子 t.seth(0) t.circle(180,50) t.seth(-180) t.circle(180,50) t.end_fill()t.done()
總結
以上是生活随笔為你收集整理的python画圣诞树的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 脚本梦幻西游_GitHub
- 下一篇: 2015 kitti 数据集_KITTI