---
title: Python Turtle 畫雪花 (Snow Crystal, Snowflake)
tags: Python Turtle
---
# Python turtle 畫雪花 (Snow Crystal, Snowflake)
By 蓉爸
Created: 2020-01-08
Revised: 2022-07-29
在 FB 上看到這一篇 [Python turtle畫雪花](https://liferecordbyys.blogspot.com/2020/01/python-turtle.html),好奇跟著試了一下,結果不行,經過除錯,可以執行了,成果像這樣:
![](https://i.imgur.com/4d2Dm1B.png)
## Desktop 版程式碼如下:
OS: Win7
Python: 3.7.4
```python=
import turtle as tu
#from turtle import*
tu.bgcolor("#FFF0F5")
tu.speed(0)
tu.colormode(255)
def branch():
for i in range(3):
for i in range(3):
tu.forward(10)
tu.backward(10)
tu.right(45)
tu.left(90)
tu.backward(15)
tu.left(45)
tu.right(90)
tu.forward(45)
for b in range(7):
for c in range(6):
x=-400+c*250
y=350-b*120
tu.penup() #提筆
tu.setposition(x,y) #確認座標
tu.pendown() #下筆
tu.begin_fill()
if b%2==0:
tu.color(88,76,208)
elif b%3==0:
tu.color(83,217,255)
else:
tu.color(168,216,255)
for i in range(8):
branch()
tu.left(45)
tu.end_fill()
tu.hideturtle()
done()
```
## Colab 版程式碼如下:
![](https://i.imgur.com/0LaVlOv.png)
```python=
# 安裝 ColabTurtle 模組
! pip3 install ColabTurtle
# 匯入 ColabTurtle 模組
from ColabTurtle.Turtle import *
# 呼叫 initializeTurtle() 函數,來初始化 Turtle
initializeTurtle ()
# 呼叫 initializeTurtle() 函數,來初始化 Turtle
initializeTurtle ()
# 設定背景顏色
bgcolor('black') # bgcolor ('white', 'yellow', 'orange', 'red', 'green', 'blue', 'purple', 'grey', 'black')
# 設定繪圖速度
# speed should be an integer in the interval [1,10]
speed(10)
# 設定線條寬度
width(1)
# 設定線條顏色
color('blue') # color ('white', 'yellow', 'orange', 'red', 'green', 'blue', 'purple', 'grey', 'black')
# ------------------------------------
# 定義 branch 函數
def branch():
for i in range(3):
for i in range(3):
forward(10)
backward(10)
right(45)
left(90)
backward(15)
left(45)
right(90)
forward(45)
# ------------------------------------
#
# 繪圖主程式: 開始畫雪花
#
ora_x=100
ora_y=40
offset_x=130
offset_y=60
max_b = 4 # max row
max_c = 3 # max column
for b in range(max_b):
for c in range(max_c):
#x=-400+c*250
#y=350-b*120
x=ora_x+c*offset_x
y=ora_y+b*offset_x
penup() #提筆
#setposition(x,y) #確認座標
setx(x)
sety(y)
pendown() #下筆
#begin_fill()
if b%2==0:
#color(88,76,208)
color('white')
elif b%3==0:
#color(83,217,255)
color('yellow')
else:
#color(168,216,255)
color('orange')
for i in range(8):
branch()
left(45)
#end_fill()
hideturtle()
```