【python:turtle繪畫 聖誕樹】
# TangYiJia 2021/12/15
import turtle as t # as 就是取個別名,後續呼叫的 t 都是 turtle
from turtle import *
import random as r
n = 100.0
speed(1000) # 定義速度
pensize(5) # 畫筆寬度
screensize(800, 800, bg='black') # 定義背景顏色,可以自己換顏色
left(90)
forward(250) # 開始的高度
color("orange", "yellow") # 定義最上端星星的顏色,外圈是 orange ,內部是 yellow
begin_fill()
left(126)
for i in range(5): # 畫五角星
forward(n / 5)
right(144) # 五角星的角度
forward(n / 5)
left(72) # 繼續換角度
end_fill()
right(126)
def drawlight(): # 定義畫彩燈的方法
if r.randint(0, 50) == 0: # 外匯跟單gendan5.com 如果覺得彩燈太多,可以把取值範圍加大一些,對應的燈就會少一些
color('tomato') # 定義第一種顏色
circle(3) # 定義彩燈大小
elif r.randint(0, 30) == 1:
color('orange') # 定義第二種顏色
circle(4) # 定義彩燈大小
elif r.randint(0, 50) == 2:
color('blue') # 定義第三種顏色
circle(2) # 定義彩燈大小
elif r.randint(0, 30) == 3:
color('white') # 定義第四種顏色
circle(4) # 定義彩燈大小
else:
color('dark green') # 其餘的隨機數情況下畫空的樹枝
color("dark green") # 定義樹枝的顏色
backward(n * 4.8)
def tree(d, s): # 開始畫樹
if d <= 0: return
forward(s)
tree(d - 1, s * .8)
right(120)
tree(d - 3, s * .5)
drawlight() # 同時呼叫小彩燈的方法
right(120)
tree(d - 3, s * .5)
right(120)
backward(s)
tree(15, 100)
backward(50)
for i in range(200): # 迴圈畫最底端的小裝飾
a = 200 - 400 * r.random()
b = 10 - 20 * r.random()
up()
forward(b)
left(90)
forward(a)
down()
if r.randint(0, 1) == 0:
color('tomato')
else:
color('wheat')
circle(2)
up()
backward(a)
right(90)
backward(b)
def drawsnowman(n,m,a,b): # 畫雪人 (n,m) 是頭和身子交點的座標, a 是頭的大小, m 是身體的大小
t.goto(n, m)
t.pencolor("white")
t.pensize(2)
t.fillcolor("white")
t.seth(0)
t.begin_fill()
t.circle(a)
t.end_fill()
t.seth(180)
t.begin_fill()
t.circle(b)
t.end_fill()
t.pencolor("black")
t.fillcolor("black")
t.penup() # 右眼睛
t.goto(n-a/4, m+a)
t.seth(0)
t.pendown()
t.begin_fill()
t.circle(2)
t.end_fill()
t.penup() # 左眼睛
t.goto(n+a/4, m+a)
t.seth(0)
t.pendown()
t.begin_fill()
t.circle(2)
t.end_fill()
t.penup() # 畫嘴巴
t.goto(n, m+a/2)
t.seth(0)
t.pendown()
t.fd(5)
t.penup() # 畫釦子
t.pencolor("red")
t.fillcolor("red")
t.goto(n, m-b/4)
t.pendown()
t.begin_fill()
t.circle(2)
t.end_fill()
t.penup()
t.pencolor("yellow")
t.fillcolor("yellow")
t.goto(n, m-b/2)
t.pendown()
t.begin_fill()
t.circle(2)
t.end_fill()
t.penup()
t.pencolor("orange")
t.fillcolor("orange")
t.goto(n, m-(3*b)/4)
t.pendown()
t.begin_fill()
t.circle(2)
t.end_fill()
drawsnowman(-200, -200, 20, 30)
drawsnowman(-250, -200, 30, 40)
t.up()
t.goto(100, 200)
t.down()
t.color("dark red", "red") # 定義字型顏色
t.penup()
t.write(" 小唐’ s Christmas Tree", font=("Comic Sans MS", 16, "bold")) # 定義文字、位置、字型、大小
t.end_fill()
def drawsnow(): # 畫雪花
t.ht() # 隱藏筆頭, ht=hideturtle
t.pensize(2) # 定義筆頭大小
for i in range(200): # 畫多少雪花
t.pencolor("white") # 定義畫筆顏色為白色,其實就是雪花為白色
t.pu() # 提筆, pu=penup
t.setx(r.randint(-350, 350)) # 定義 x 座標,隨機從 -350 到 350 之間選擇
t.sety(r.randint(-100, 350)) # 定義 y 座標,注意雪花一般在地上不會落下,所以不會從太小的縱座軸開始
t.pd() # 落筆, pd=pendown
dens = 6 # 雪花瓣數設為 6
snowsize = r.randint(1, 10) # 定義雪花大小
for j in range(dens): # 就是 6 ,那就是畫 5 次,也就是一個雪花五角星
# t.forward(int(snowsize)) #int ()取整數
t.fd(int(snowsize))
t.backward(int(snowsize))
# t.bd(int(snowsize)) # 注意沒有 bd=backward ,但有 fd=forward ,小 bug
t.right(int(360 / dens)) # 轉動角度
drawsnow() # 呼叫畫雪花的方法
t.done() # 完成 , 否則會直接關閉
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2848283/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Python繪圖,聖誕樹,花,愛心 | Turtle篇Python繪圖
- 最美聖誕樹!用Python畫棵雪夜聖誕樹送給你Python
- 聖誕樹--pythonPython
- 使用python的turtle繪畫滑稽臉Python
- 如何用 C 語言畫一個“聖誕樹”?
- Python——畫一棵漂亮的櫻花樹(不同種櫻花+玫瑰+聖誕樹喔)Python
- 聖誕節的python豪華版聖誕樹,包括雪花彩燈文字背景Python
- 聖誕快樂: 用 GaussDB T 繪製一顆聖誕樹,兼論高斯資料庫語法相容資料庫
- 使用Python的turtle模組繪製美麗的櫻花樹Python
- 聖誕夜,讓你的程式碼都變成聖誕樹吧!
- 聖誕節,把你的 JavaScript 程式碼都裝扮成聖誕樹吧JavaScript
- python中turtle如何畫太陽花?Python
- 程式設計師的聖誕節--送她一顆聖誕樹(附原始碼)程式設計師原始碼
- 程式設計師的聖誕節–送她一顆聖誕樹(附原始碼)程式設計師原始碼
- Python繪圖Turtle庫詳解Python繪圖
- 聖誕快樂——Keras+樹莓派:用深度學習識別聖誕老人Keras樹莓派深度學習
- 用程式碼畫兩棵聖誕樹送給你【附詳細程式碼】
- 聖誕節,把網站所有的js程式碼都壓縮成聖誕樹吧。網站JS
- 【Python自動戴聖誕帽】02 戴聖誕帽及抗疫彩蛋Python
- Linux/Unix 桌面趣事:終端上的聖誕樹Linux
- Python的畫圖模組turtle使用詳解Python
- YouGov:45%的美國人更喜歡人造聖誕樹Go
- 快到聖誕節了,用python來給自己的頭像加上一頂聖誕帽Python
- Python程式設計 聖誕樹教程 (附程式碼)程式設計師的浪漫Python程式設計師
- Python如何運用turtle繪製陰陽太極圖Python
- python 使用turtle庫簡單繪圖5個列子Python繪圖
- Python基本圖形繪製--模組1:turtle庫的使用Python
- 聖誕小鹿?VScodeVSCode
- AntD 聖誕節彩蛋事件事件
- 聖誕鬧劇!阿里旗下開源專案聖誕彩蛋遭開發者狂批阿里
- 聖誕郵件營銷:如何做使用者眼中的聖誕老人?
- python turtle 使用Python
- 聖誕祝福程式官方漢化版!
- 聖誕雪花紛飛自定義ViewView
- 一個聖誕動畫的實現動畫
- 用Python能畫哪些樹?Python
- 用python的小海龜 Turtle 畫一朵好看又有趣的小花Python
- 聖誕節快到啦!Mac電腦系統如何在聖誕節讓電腦螢幕下雪?Mac