Python繪圖,聖誕樹,花,愛心 | Turtle篇

Python家庭發表於2020-02-14

1.畫聖誕樹

import turtle
screen = turtle.Screen()
screen.setup(800,600)
circle = turtle.Turtle()
circle.shape('circle')
circle.color('red')
circle.speed('fastest')
circle.up()
square = turtle.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, 17):
    y = 30*i
    for j in range(i-k):
        x = 30*j
        square.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 += 2
    if 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(17,20):
    y = 30*i
    for j in range(3):
        x = 30*j
        square.goto(x,-y+280)
        square.stamp()
        square.goto(-x,-y+280)
        square.stamp()
turtle.exitonclick()

2.畫櫻花

import turtle as T
import random
import time
​
# 畫櫻花的軀幹(60,t)
def Tree(branch, t):
    time.sleep(0.0005)
    if branch > 3:
        if 8 <= branch <= 12:
            if random.randint(0, 2) == 0:
                t.color('snow')  # 白
            else:
                t.color('lightcoral')  # 淡珊瑚色
            t.pensize(branch / 3)
        elif branch < 8:
            if random.randint(0, 1) == 0:
                t.color('snow')
            else:
                t.color('lightcoral')  # 淡珊瑚色
            t.pensize(branch / 2)
        else:
            t.color('sienna')  # 赭(zhě)色
            t.pensize(branch / 10)  # 6
        t.forward(branch)
        a = 1.5 * random.random()
        t.right(20 * a)
        b = 1.5 * random.random()
        Tree(branch - 10 * b, t)
        t.left(40 * a)
        Tree(branch - 10 * b, t)
        t.right(20 * a)
        t.up()
        t.backward(branch)
        t.down()
​
# 掉落的花瓣
def Petal(m, t):
    for i in range(m):
        a = 200 - 400 * random.random()
        b = 10 - 20 * random.random()
        t.up()
        t.forward(b)
        t.left(90)
        t.forward(a)
        t.down()
        t.color('lightcoral')  # 淡珊瑚色
        t.circle(1)
        t.up()
        t.backward(a)
        t.right(90)
        t.backward(b)
​
# 繪圖區域
t = T.Turtle()
# 畫布大小
w = T.Screen()
t.hideturtle()  # 隱藏畫筆
t.getscreen().tracer(5, 0)
w.screensize(bg='white')  # wheat小麥
t.left(90)
t.up()
t.backward(150)
t.down()
t.color('sienna')
​
# 畫櫻花的軀幹
Tree(60, t)
# 掉落的花瓣
Petal(200, t)
w.exitonclick()

3.畫一箭穿心

from turtle import*
color ("black","red")
pensize(5)
begin_fill()
penup()
goto(50,50)
pendown()
right(45)
goto(100,0)
left(90)
fd(120)
circle(50,225)
penup()
goto(0,0)
pendown()
left(135)
fd(120)
circle(50,225)
seth(90)
circle(50,225)
fd(121)
end_fill()
left(56)
penup()
goto(-210,40)
pendown()
goto(0,80)
penup()
goto(160,110)
pendown()
goto(320,140)
done()

4.畫愛心

# -*- coding:utf-8 -*- 
import turtle
import time
​
# 畫心形圓弧
​
def hart_arc():
    for i in range(200):
        turtle.right(1)
        turtle.forward(2)
​
def move_pen_position(x, y):
​
    turtle.hideturtle()     # 隱藏畫筆(先)
​
    turtle.up()     # 提筆
​
    turtle.goto(x, y)    # 移動畫筆到指定起始座標(視窗中心為0,0)
​
    turtle.down()   # 下筆
​
    turtle.showturtle()     # 顯示畫筆
​
# 初始化
​
turtle.setup(width=800, height=500)     # 視窗(畫布)大小
​
turtle.color('red', 'pink')     # 畫筆顏色
​
turtle.pensize(2)       # 畫筆粗細
​
turtle.speed(0.5)     # 描繪速度
​
# 初始化畫筆起始座標
​
move_pen_position(x=0,y=-180)   # 移動畫筆位置
​
turtle.left(140)    # 向左旋轉140度
​
turtle.begin_fill()     # 標記背景填充位置
​
# 畫心形直線( 左下方 )
​
turtle.forward(224)    # 向前移動畫筆,長度為224
​
# 畫愛心圓弧
​
hart_arc()      # 左側圓弧
​
turtle.left(120)    # 調整畫筆角度
​
hart_arc()      # 右側圓弧
​
# 畫心形直線( 右下方 )
​
turtle.forward(224)
​
turtle.end_fill()       # 標記背景填充結束位置
  
# 點選視窗關閉程式
​
window = turtle.Screen()
​
window.exitonclick()

5.畫玫瑰花

'''
Created on Nov 18, 2017
@author: QiZhao
'''
import turtle
 
# 設定初始位置
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)
# time.sleep(3)

6.畫太極

#引入turtle函式庫
​
from turtle import * 
​
#定義畫半個太極圖的函式,第一個引數radius是大圓的半徑,
​
#color1,color2分別是兩種填充顏色,對應圖形中的黑白填充
​
def draw(radius, color1,color2):
​
    #設定畫筆粗細
    width(3)
​
    #設定畫筆顏色和填充顏色
    color("black",color1)
​
    #準備開始填充圖形
    begin_fill()
​
    #首先畫一個半徑為radius/2,弧度為180的半圓,畫的是紅線所示半圓
    circle(radius/2,180)
​
    #畫一個半徑為radius,弧度為180的半圓,畫的是黃線所示半圓
    circle(radius,180)
​
    #將畫筆方向旋轉180度
    left(180)
​
    #畫一個半徑為radius/2,弧度為180的半圓,此時半徑值為負,
​
    #圓心在畫筆的右邊,畫的是綠線所示半圓
    circle(-radius/2,180)
​
    #結束填充
    end_fill()
​
    #畫筆向左旋轉90度,正好指向畫板上方
​
    left(90)
​
    #抬起畫筆,再運動時不會留下痕跡
    up()
​
    #向前移動radius*0.35,這樣小圓邊線距離大圓邊線上下各radius*0.35,
​
    #小圓的半徑就為radius*0.15
    forward(radius*0.35)
​
    #畫筆向右旋轉90度,指向畫板右側
    right(90)
​
    #放下畫筆
    down()
​
    color(color2,color2)
​
    #開始畫內嵌小圓,如藍線所示
    begin_fill()
​
    circle(radius*0.15)
​
    end_fill()
​
    #旋轉畫筆90度,指向畫板上方
    left(90)
​
    up()
​
    #後退radius*0.35
    backward(radius*0.35)
​
    down()
​
    #旋轉畫筆90度,指向畫板左方
    left(90)
​
#定義主函式
def main():
​
    #設定視窗或者畫板大小
    setup(500,500)
​
    #繪製黑色一半,白色內圓
    draw(200,"black","white")
​
    #繪製白色一半,黑色內圓
    draw(200,"white","black")
​
    #隱藏畫筆
    ht()
​
main()

 

 

秒變大牛,就是這麼簡單~



每週每日,分享Python實戰程式碼,入門資料,進階資料,基礎語法,爬蟲,資料分析,web網站,機器學習,深度學習等等。


​微信群(關注「Python家庭」一起輕鬆學Python吧)

​QQ ②群(983031854

相關文章