Python+Turtle繪制表白比心圖案
一、效果展示
在介紹代碼之前,先來看下本文的實(shí)現(xiàn)效果。

可以參考下面步驟把Python文件轉(zhuǎn)化成exe,發(fā)給未安裝Python的他/她。
Pinstaller(Python打包為exe文件)
之前自己把 Python 文件打包成 exe 的時(shí)候,折騰了很久,本文將詳細(xì)地講述如何快速生成在不安裝 Python 的電腦上也能執(zhí)行的文件
1. 在 prompt 中運(yùn)行 pip install pyinstaller , 安裝 pyinstaller 庫

2. 在 prompt 中運(yùn)行 where pyinstaller

3. 找到待打包文件存放的路徑
把要打包的文件放到找到的路徑
C:\Users\Administrator\Anaconda3\Scripts 中 (我的路徑是這個(gè),你就按照第二步的路徑)
4. 調(diào)用 cmd 窗口
把待打包文件放在
C:\Users\Administrator\Anaconda3 \Scripts 目錄下,在該文件夾中按shift+鼠標(biāo)右鍵 , 點(diǎn)擊 在此處打開命令窗口 調(diào)用 cmd
5. 在 cmd 中輸入 pyinstaller -F 文件名
例子:打包 Python 繪制皮卡丘的視頻,在cmd中輸入 pyinstaller -F pkq_1.py
即可生成普通圖標(biāo)的exe可執(zhí)行文件。
6. 生成 exe 文件
可以在路徑
C:\Users\Administrator\Anaconda3\Scripts 下的 dist 文件夾中找到打包好的exe文件(即不用安裝 Python 也可以運(yùn)行的文件)。
這樣生成的文件圖標(biāo)是標(biāo)準(zhǔn)固定格式,如果想生成特定特定形狀的圖標(biāo)需要用第7點(diǎn)中的語句。
7. 生成自定義形狀的圖標(biāo),在cmd中輸入:pyinstaller -i ico路徑 -F xxxxx.py
例子: 打包 Python 繪制皮卡丘視頻的py文件,在cmd中輸入 (注: 我把ico圖標(biāo)和待打包文件放到一個(gè)文件夾下了, 所以直接輸入了ico的名字)
pyinstaller?-i??pikaqiu2.ico?-F?pkq_1.py
生成圖標(biāo)是皮卡丘形狀的exe文件。
二、代碼詳解
Python繪制比心圖的原理是:應(yīng)用turtle庫控制函數(shù)繪制不同曲線構(gòu)成比心圖。
1 導(dǎo)入庫
首先導(dǎo)入本文需要加載的庫,如果你有些庫還沒有安裝,導(dǎo)致運(yùn)行代碼時(shí)報(bào)錯(cuò),可以在Anaconda Prompt中用pip方法安裝。
# -*- coding: UTF-8 -*- ''' 代碼用途 :畫比心手 作者 :阿黎逸陽 博客 : https://blog.csdn.net/qq_32532663/article/details/106176609 ''' import os import pygame import turtle as t from time import sleep
本文應(yīng)用到的庫較少,只應(yīng)用了os、pygame、turtle和time四個(gè)庫。
- os庫可以設(shè)置文件讀取的位置。
- pygame庫是為了繪制過程更有趣,在繪圖過程中添加了背景音樂。
- turtle庫是繪圖庫,相當(dāng)于給你一支畫筆,你可以在畫布上用數(shù)學(xué)邏輯控制的代碼完成繪圖。
- time庫可以設(shè)置程序休眠的時(shí)間,達(dá)到動(dòng)態(tài)畫心的效果。
2 播放音樂
接著應(yīng)用pygame庫播放背景音樂,本文的音樂是《趙海洋 - 《瞬間的永恒》夜色鋼琴曲》。
#播放音樂
print('播放音樂')
pygame.mixer.init()
pygame.mixer.music.load(r"F:\公眾號(hào)\520\趙海洋 - 《瞬間的永恒》夜色鋼琴曲.mp3")
pygame.mixer.music.set_volume(0.5)
pygame.mixer.music.play(1, 10)
這一部分的代碼和整體代碼是剝離的,可以選澤在最開始放上該代碼,也可以直接刪除。如果選擇播放音樂,需要在代碼music.load函數(shù)中把你想放音樂的電腦本地存放地址填進(jìn)去。有部分朋友對(duì)這一塊有疑問,填充格式可參考如下圖片:

3 畫手
然后設(shè)置畫板的大小,并畫手。
#畫圖
print('畫圖')
t.title('阿黎逸陽的代碼公眾號(hào)')
t.speed(1)
#t.screensize(1000, 800)
t.setup(startx=0, starty = 0, width=800, height = 600)
#第一根手指頭
print('畫第一跟手指頭')
t.penup()
t.goto(0, 60)
t.pendown()
t.color('black')
t.pensize(2)
t.setheading(30)
t.circle(-200, 30)
t.circle(-8, 120)
t.setheading(205)
t.circle(-300, 12)
t.left(10)
t.forward(45)
#畫第二條線
print('畫第二條線')
t.penup()
t.goto(18, 13)
t.pendown()
t.setheading(20)
t.forward(50)
#畫第三條線
print('畫第三條線')
t.penup()
t.goto(18, -15)
t.pendown()
t.setheading(20)
t.forward(50)
#畫第四條線
print('畫第四條線')
t.penup()
t.goto(22, -36)
t.pendown()
t.setheading(10)
t.circle(90, 26)
#畫第二根手指的弧線
print('畫第二根手指的弧線')
t.penup()
t.goto(55, 55)
t.pendown()
t.setheading(-20)
t.circle(-25, 85)
#畫第三根手指的弧線
print('畫第三根手指的弧線')
t.penup()
t.goto(62, 30)
t.pendown()
t.setheading(-10)
t.circle(-20, 120)
#畫第四根手指弧線
print('畫第四根手指的弧線')
t.penup()
t.goto(64, 2)
t.pendown()
t.setheading(-10)
t.circle(-20, 120)
t.right(5)
t.forward(20)
t.right(10)
t.circle(-10, 80)
t.setheading(78)
t.circle(-50, 30)
t.penup()
t.goto(57, -25)
t.pendown()
t.setheading(-50)
t.forward(10)
#手的下弧度
print('畫手的下弧線')
t.penup()
t.goto(22, -36)
t.pendown()
t.setheading(270)
t.circle(-30, 80)
t.forward(10)
t.setheading(240)
t.forward(40)
#手的上弧度
print('畫第手的上弧線')
t.penup()
t.goto(0, 60)
t.pendown()
t.setheading(190)
t.circle(150, 20)
t.setheading(225)
t.forward(10)
t.setheading(265)
t.forward(80)
t.setheading(230)
t.forward(60)
#畫大拇指
print('畫大拇指')
t.penup()
t.goto(8, 63)
t.pendown()
t.setheading(95)
t.circle(-80, 30)
t.circle(-10, 180)
t.left(20)
t.forward(20)
#畫指甲
print('畫指甲')
t.penup()
t.goto(84, 85)
t.pendown()
t.color('black', 'red')
t.begin_fill()
t.setheading(18)
t.circle(-50, 20)
t.setheading(270)
t.circle(-10, 170)
t.end_fill()
關(guān)鍵代碼詳解:
- t.pensize(width):設(shè)置畫筆的尺寸。
- t.color(color):設(shè)置畫筆的顏色。
- t.penup():抬起畫筆,一般用于另起一個(gè)地方繪圖使用。
- t.goto(x,y):畫筆去到某個(gè)位置,參數(shù)為(x,y),對(duì)應(yīng)去到的橫坐標(biāo)和縱坐標(biāo)。
- t.pendown():放下畫筆,一般和penup組合使用。
- t.left(degree):畫筆向左轉(zhuǎn)多少度,括號(hào)里表示度數(shù)。
- t.right(degree):畫筆向右轉(zhuǎn)多少度,括號(hào)里表示度數(shù)。
- t.circle(radius,extent,steps):radius指半徑,若為正,半徑在小烏龜左側(cè)radius遠(yuǎn)的地方,若為負(fù),半徑在小烏龜右側(cè)radius遠(yuǎn)的地方;extent指弧度;steps指階數(shù)。
畫手的關(guān)鍵是:通過調(diào)節(jié)circle函數(shù)中的半徑和弧度來調(diào)節(jié)曲線的弧度,從而使得手的輪廓比較流暢。
4 定義畫心的函數(shù)
接著定義畫心的函數(shù)。
def heart_bit():
#畫愛心
print('畫愛心')
t.penup()
t.goto(70, 135)
t.pendown()
t.color('black', 'red')
t.begin_fill()
t.setheading(90)
t.circle(10, 180)
t.left(30)
t.forward(30)
t.setheading(30)
t.forward(30)
t.left(38)
t.circle(12, 188)
t.end_fill()?5 定義寫名字的函數(shù)并實(shí)現(xiàn)動(dòng)態(tài)畫心
最后定義寫名字的函數(shù)并實(shí)現(xiàn)動(dòng)態(tài)畫心。
def write_name(x, y, size, ss):
t.hideturtle()
t.penup()
t.goto(x, y)
t.pendown()
t.pencolor('black')
t.write(ss, font=('Times New Roman', size, 'normal'))
while 1:
t.speed(10)
print('寫名字')
write_name(5, 125, 12, '楊紫')
write_name(5, 125, 12, '楊紫')
heart_bit()
sleep(1)
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
到此這篇關(guān)于Python+Turtle繪制表白比心圖案的文章就介紹到這了,更多相關(guān)Python Turtle比心內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Pytorch中的variable, tensor與numpy相互轉(zhuǎn)化的方法
這篇文章主要介紹了Pytorch中的variable, tensor與numpy相互轉(zhuǎn)化的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Python使用Appium實(shí)現(xiàn)自動(dòng)化操作手機(jī)入門教學(xué)
Appium作為一個(gè)開源的移動(dòng)應(yīng)用自動(dòng)化測試框架,支持多種編程語言,包括Python、Java、Ruby等,本文將詳細(xì)介紹如何使用Python和Appium來操作手機(jī),需要的可以了解下2025-10-10
numpy中np.append()函數(shù)用法小結(jié)
在numpy的函數(shù)庫中,np.append()函數(shù)是一個(gè)常用的數(shù)組操作函數(shù),它在進(jìn)行數(shù)組操作時(shí)能夠?qū)蓚€(gè)數(shù)組進(jìn)行拼接,并返回一個(gè)拼接后的新數(shù)組,下面就來介紹一下具體用法,感興趣的可以了解一下2023-11-11

