1. 程式人生 > 其它 >Python使用turtle庫繪製動態滿屏愛心程式碼

Python使用turtle庫繪製動態滿屏愛心程式碼

技術標籤:作品原始碼

情人節繪製愛心最合適了,但是單單畫一個心形怎麼能夠滿足?今天我們嘗試用Python的turtle庫畫出一整個畫布的愛心,讓它們在上面動態移動。最終效果如下:

在這裡插入圖片描述

繪製愛心

畫愛心有很多種畫法,我這裡用的方法是用兩個圓形和一個正方形組成愛心。而且為了接下來能夠實現上百個愛心比較流暢地移動的效果,這裡直接用三個畫筆組成一個愛心,執行速度會更快。程式碼如下:

from turtle import *
from math import sqrt
width, height = 800, 600
screen = Screen()     # 建立視窗物件
screen.setup(
width, height) # 設定視窗的寬高 screen.delay(0) # 設定無延時繪畫 screen.bgcolor('pink') # 設定背景顏色為粉色 # 設定畫筆的統一屬性 t = Turtle(visible=False, shape='circle') t.shapesize(10, 10) t.pencolor('red') t.fillcolor('red') t.penup() # 克隆一個圓形,設定位置 circle1 = t.clone() circle1.goto(-sqrt(10*10*160)/2, 0) # 克隆第二個圓形,設定位置 circle2 =
t.clone() circle2.goto(sqrt(10*10*160)/2, 0) # 克隆一個正方形,設定位置並旋轉角度 square = t.clone() square.shape("square") square.setheading(45) square.goto(0, -sqrt(10*10*160)/2) # 顯示圖形 circle1.showturtle() circle2.showturtle() square.showturtle() done()

效果圖如下:

在這裡插入圖片描述


定義愛心類

接著,我們將這種畫法的愛心定義為一個類。這樣後面要顯示多個愛心並調整它們的位置會比較方便。將上面的程式碼稍作修改,成為下面的程式碼:

class Heart:
    def __init__(self, x, y, size):
        self.size = size    # 心形大小
        self.speed = size    # 移動速度根據大小變化
        t = Turtle(visible=False, shape='circle')
        t.shapesize(size, size)
        color = (1, 1- size/4, 1-size/4)     # 顏色修改為根據大小變化的粉色
        t.pencolor(color)
        t.fillcolor(color)
        t.penup()
        self.circle1 = t.clone()
        self.circle1.goto(x-sqrt(size*size*160)/2, y)
        self.circle2 = t.clone()
        self.circle2.goto(x+sqrt(size*size*160)/2, y)
        self.square = t.clone()
        self.square.shape("square")
        self.square.setheading(45)
        self.square.goto(x, y-sqrt(size * size * 160)/2)
        self.circle1.showturtle()
        self.circle2.showturtle()
        self.square.showturtle()

程式碼裡定義了愛心的移動速度和顏色與大小相關:愛心越大,顏色越深,移動速度越快。這樣表現出來的效果會更加豐富。

在主函式裡隨機生成25個位置和大小不同的愛心,並存入列表:

hearts = []
for i in range(25):
    heart = Heart(width/2 + randint(1, width), randint(-height/2,height/2), random()*3)
    hearts.append(heart)

讓愛心動起來

在愛心類中定義一個方法(函式)move(),用來讓愛心向左移動一個單位速度長度:

    def move(self):
        self.circle1.setx(self.circle1.xcor()-self.speed)
        self.square.setx(self.square.xcor() - self.speed)
        self.circle2.setx(self.circle2.xcor() - self.speed)

然後在主函式裡面寫一個死迴圈,讓所有愛心重複不斷的移動:

while True:
    for heart in hearts:
        heart.move()

新增完這兩處程式碼後,愛心就可以動起來了:

在這裡插入圖片描述

讓愛心回到起點

此時還有一個問題沒處理,愛心從螢幕左側出去後就沒有新的愛心了。其實我們並不需要新的愛心,只需要把從左邊出去的愛心再偷偷移動回右側起點就行。

定義一個將愛心移動到指定位置的方法(函式)moveTo(x, y)

    def moveTo(self, x, y):
        # 隱藏形狀後再移動防止看到移動軌跡
        self.circle1.hideturtle()
        self.circle2.hideturtle()
        self.square.hideturtle()
        # 移動到指定位置
        self.circle1.goto(x - sqrt(self.size * self.size * 160) / 2, y)
        self.circle2.goto(x + sqrt(self.size * self.size * 160) / 2, y)
        self.square.goto(x, y - sqrt(self.size * self.size * 160) / 2)
        # 恢復顯示
        self.circle1.showturtle()
        self.circle2.showturtle()
        self.square.showturtle()

接著在迴圈中用 if 語句判斷,當愛心位置移動出螢幕左側時,將其移動到右側隨機位置:

while True:
    for heart in hearts:
        heart.move()
        if heart.square.xcor() < -width / 2:     # 如果愛心移動出螢幕左側
            heart.moveTo(width / 2 + randint(1, width), randint(-height / 2, height / 2))   # 回到右側隨機位置

到這裡,完整的效果就完成了。不過turtle沒法實現全屏效果,愛心只能填滿畫布,有點可惜。

在這裡插入圖片描述


完整程式碼

文章內容系原創。如果你覺得這篇文章不錯,可以購買完整程式碼支援我:Python動態滿屏心形程式碼(turtle)