Python+Turtle繪制蜘蛛俠的示例代碼
蜘蛛俠(Spider-Man)即彼得·帕克(Peter Parker),是美國(guó)漫威漫畫旗下超級(jí)英雄。
由編劇斯坦·李和畫家史蒂夫·迪特科聯(lián)合創(chuàng)造,初次登場(chǎng)于《驚奇幻想》(Amazing Fantasy)第15期(1962年8月).
因?yàn)閺V受歡迎,幾個(gè)月后,便開始擁有以自己為主角的單行本漫畫。
網(wǎng)易云中關(guān)于蜘蛛俠主題曲熱評(píng)過萬的評(píng)論說到。
蜘蛛俠之所以成為最受歡迎的超級(jí)英雄之一,是因?yàn)檫@面具下的人,不分膚色、種族、性別。。。
他/她可能是你,是我,是和你一起生活的人。
任何人都能帶上這個(gè)面具,你也可以做到,如果你以前沒有想過,希望現(xiàn)在可以了~
本文主要介紹運(yùn)用python中的turtle庫控制函數(shù)繪制蜘蛛俠。
一、效果展示
在介紹代碼之前,先來看下本文的實(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庫繪制身體的不同部位。
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
本文應(yīng)用到的庫較少,只應(yīng)用了os、pygame和turtle三個(gè)庫。
os庫可以設(shè)置文件讀取的位置。
pygame庫是為了繪制過程更有趣,在繪圖過程中添加了背景音樂。
turtle庫是繪圖庫,相當(dāng)于給你一支畫筆,你可以在畫布上用數(shù)學(xué)邏輯控制的代碼完成繪圖。
2.播放音樂
接著應(yīng)用pygame庫播放背景音樂,本文的音樂是《Sunflower》。
os.chdir(r'F:\公眾號(hào)\56.蜘蛛俠') #播放音樂 print('播放音樂') pygame.mixer.init() pygame.mixer.music.load("Cope - Sunflower (Original Version).mp3") pygame.mixer.music.set_volume(0.5) pygame.mixer.music.play(1, 10)
這一部分的代碼和整體代碼是剝離的,可以選澤在最開始放上該代碼,也可以直接刪除。
如果選擇播放音樂,需要在代碼music.load函數(shù)中把你想放音樂的電腦本地存放地址填進(jìn)去。
有部分朋友對(duì)這一塊有疑問,填充格式可參考如下圖片:
3.定義畫蜘蛛俠上半身的函數(shù)
然后設(shè)置畫板的大小,并定義繪制蜘蛛俠上半身的函數(shù)。
t.title('阿黎逸陽的代碼公眾號(hào)') t.speed(10) #t.screensize(1000, 800) t.setup(startx=0, starty = 0, width=800, height = 600) def up_body(): #畫頭 t.penup() t.goto(60, 200) t.pendown() t.pensize(1) t.color('black', 'red') t.begin_fill() t.setheading(60) t.circle(60, 30) t.left(4) t.circle(40, 173) t.left(4) t.circle(60, 30) #畫脖子 t.setheading(260) t.circle(30, 29) #畫肩膀 t.setheading(220) t.forward(30) #畫手上肌肉 t.setheading(150) t.circle(30, 130) #畫胸部的內(nèi)部線 t.setheading(30) t.circle(-100, 13) t.setheading(270) t.circle(50, 40) t.setheading(255) t.circle(55, 40) t.circle(-40, 50) #畫腰部的外橫線 t.setheading(0) t.forward(-7) t.setheading(270) t.forward(18) #畫腰線 t.setheading(-30) t.forward(50) t.setheading(15) t.forward(80) t.setheading(90) t.forward(22) #重復(fù)的地方 #畫衣服內(nèi)輪廓 t.setheading(190) t.forward(20) t.setheading(103) t.circle(-160, 41) #畫手內(nèi)輪廓 t.setheading(5) t.circle(-80, 30) t.setheading(20) t.circle(30, 30) #重復(fù)的地方 #手臂上肌肉 t.setheading(70) t.circle(22, 150) t.setheading(150) t.forward(30) t.setheading(120) t.forward(15) 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 left_hand(): #畫左手臂 #畫胸部的內(nèi)部線 t.penup() t.goto(-69, 134) t.color('black', 'blue') t.pendown() t.begin_fill() t.setheading(30) t.circle(-100, 13) t.setheading(270) t.circle(50, 40) t.setheading(255) t.circle(55, 40) t.circle(-40, 50) #畫腰部的外橫線 t.setheading(0) t.forward(-8) t.setheading(90) t.circle(220, 18) t.setheading(-90) t.circle(-40, 50) t.setheading(-85) t.circle(-50, 50) t.setheading(135) t.circle(30, 40) t.setheading(95) t.circle(-50, 50) t.setheading(98) t.circle(-60, 51) t.end_fill() def right_hand(): #畫右手臂 #畫衣服內(nèi)輪廓 t.penup() t.goto(80, 39) t.color('black', 'blue') t.pendown() t.begin_fill() t.setheading(190) t.forward(20) t.setheading(103) t.circle(-160, 41) #畫手內(nèi)輪廓 t.setheading(5) t.circle(-80, 30) t.setheading(20) t.circle(30, 30) t.setheading(-20) t.circle(-55, 65) t.setheading(-30) t.circle(-50, 60) t.setheading(180) t.circle(30, 40) t.setheading(154) t.circle(-48, 60) t.setheading(164) t.circle(-50, 60) t.setheading(-90) t.circle(-40, 60) t.left(40) t.circle(150, 23) t.end_fill() def left_wrist(): #畫左手腕 t.penup() t.goto(-81, 37) t.color('black', 'red') t.pendown() t.begin_fill() t.setheading(135) t.circle(30, 40) t.setheading(-90) t.circle(-60, 30) t.setheading(-90) t.forward(20) t.setheading(-45) t.forward(12) t.circle(6, 180) t.setheading(-50) t.circle(5, 160) t.setheading(95) t.forward(10) t.setheading(135) t.forward(8) t.setheading(95) t.forward(6) t.setheading(35) t.circle(30, 10) t.left(10) t.circle(30, 27) t.end_fill() #畫手腕上的線 #橫線 #第一條橫線 t.penup() t.goto(-84, 30) t.color('black') t.pendown() t.setheading(145) t.circle(30, 36) #第二條橫線 t.penup() t.goto(-90, 22) t.color('black') t.pendown() t.setheading(185) t.circle(-30, 31) #第三條橫線 t.penup() t.goto(-83, 10) t.color('black') t.pendown() t.setheading(210) t.circle(-50, 31) #第四條橫線 t.penup() t.goto(-102, -10) t.color('black') t.pendown() t.setheading(50) t.circle(-20, 41) t.setheading(55) t.circle(-90, 8) #第一條豎線 t.penup() t.goto(-105, 24) t.color('black') t.pendown() t.setheading(-95) t.circle(100, 20) #第二條豎線 t.penup() t.goto(-87, 42) t.color('black') t.pendown() t.setheading(-110) t.forward(22) t.setheading(-63) t.circle(-50, 40) def right_wrist(): #畫右手腕 t.penup() t.goto(189, 57) t.color('black', 'red') t.pendown() t.begin_fill() t.setheading(180) t.circle(30, 40) t.setheading(-55) t.circle(-100, 10) t.circle(-20, 70) t.setheading(-90) t.forward(10) t.setheading(-0) t.forward(5) t.setheading(-85) t.forward(8) t.setheading(-20) t.circle(8, 60) t.setheading(-35) t.circle(8, 70) t.setheading(-15) t.circle(6, 70) t.setheading(60) t.circle(20, 80) t.setheading(115) t.circle(-100, 20) t.end_fill() #畫第一條橫線 t.goto(191, 45) t.color('black') t.pendown() t.setheading(215) t.circle(-30, 34) #畫第二條橫線 t.penup() t.goto(197, 29) t.color('black') t.pendown() t.setheading(215) t.circle(-30, 37) #畫第三條橫線 t.penup() t.goto(174, 11) t.color('black') t.pendown() t.setheading(-0) t.circle(-30, 27) t.setheading(20) t.circle(-20, 27) t.setheading(40) t.circle(-30, 23) #畫第一條豎線 t.penup() t.goto(178, 55) t.color('black') t.pendown() t.setheading(-70) t.circle(-200, 9) t.setheading(-82) t.circle(-100, 18) #畫第二條豎線 t.penup() t.goto(185, 55) t.color('black') t.pendown() t.setheading(-70) t.circle(-200, 8) t.setheading(-68) t.circle(-80, 25)
5.定義畫蜘蛛的函數(shù)
接著定義畫蜘蛛的函數(shù)。
def spider(): #畫蜘蛛 t.penup() t.goto(8, 146) t.color('black') t.pendown() t.begin_fill() t.setheading(-120) t.circle(40, 60) t.setheading(60) t.circle(40,60) t.end_fill() #畫蜘蛛的腳 #右邊的腳1 t.penup() t.goto(13, 129) t.color('black') t.pendown() t.setheading(30) t.forward(10) t.setheading(90) t.forward(15) #右邊的腳2 t.penup() t.goto(14, 125) t.color('black') t.pendown() t.setheading(30) t.forward(16) t.setheading(90) t.forward(17) #右邊的腳3 t.penup() t.goto(14, 124) t.color('black') t.pendown() t.setheading(-20) t.forward(16) t.setheading(-90) t.forward(17) #右邊的腳4 t.penup() t.goto(14, 120) t.color('black') t.pendown() t.setheading(-20) t.forward(10) t.setheading(-90) t.forward(15) #畫蜘蛛的腳 #左邊的腳1 t.penup() t.goto(3, 129) t.color('black') t.pendown() t.setheading(150) t.forward(10) t.setheading(90) t.forward(15) #右邊的腳2 t.penup() t.goto(2, 125) t.color('black') t.pendown() t.setheading(150) t.forward(16) t.setheading(90) t.forward(17) #右邊的腳3 t.penup() t.goto(2, 124) t.color('black') t.pendown() t.setheading(-170) t.forward(16) t.setheading(-99) t.forward(17) #右邊的腳4 t.penup() t.goto(3, 120) t.color('black') t.pendown() t.setheading(-170) t.forward(10) t.setheading(-90) t.forward(15)
6.調(diào)用函數(shù)繪制圖形
最后調(diào)用函數(shù)繪制圖形。
print('繪制上半身外輪廓') up_body() print('繪制右手') right_hand() print('繪制左手') left_hand() print('繪制左拳頭') left_wrist() print('繪制右拳頭') right_wrist() print('繪制蜘蛛') spider()
至此,在Python中實(shí)現(xiàn)蜘蛛俠的繪制邏輯已大致講解完畢。
以上就是Python+Turtle繪制蜘蛛俠的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Python Turtle繪制蜘蛛俠的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
pycharm中import導(dǎo)入包呈現(xiàn)灰色的問題及解決
這篇文章主要介紹了pycharm中import導(dǎo)入包呈現(xiàn)灰色的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05Matplotlib 生成不同大小的subplots實(shí)例
今天小編就為大家分享一篇Matplotlib 生成不同大小的subplots實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-05-05python列表返回重復(fù)數(shù)據(jù)的下標(biāo)
這篇文章主要介紹了python列表返回重復(fù)數(shù)據(jù)的下標(biāo),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02Python如何利用Har文件進(jìn)行遍歷指定字典替換提交的數(shù)據(jù)詳解
這篇文章主要給大家介紹了關(guān)于Python如何利用Har文件進(jìn)行遍歷指定字典替換提交的數(shù)據(jù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Python基礎(chǔ)之?dāng)?shù)據(jù)類型相關(guān)知識(shí)總結(jié)
眾所周知,在Python中,常用的數(shù)據(jù)類型有三種,分別是字符串、整數(shù)和浮點(diǎn)數(shù).在Python基礎(chǔ)學(xué)習(xí)的過程中,數(shù)據(jù)類型是初學(xué)者常常容易混淆的一個(gè)基礎(chǔ)知識(shí)點(diǎn),本文為大家詳細(xì)總結(jié)了三種數(shù)據(jù)類型的概念、數(shù)據(jù)類型的查詢以及更為復(fù)雜的數(shù)據(jù)轉(zhuǎn)化,需要的朋友可以參考下2021-06-06Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)實(shí)現(xiàn)優(yōu)先級(jí)隊(duì)列的方法示例
這篇文章主要介紹了Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)實(shí)現(xiàn)優(yōu)先級(jí)隊(duì)列的方法,結(jié)合實(shí)例形式分析了Python中基于給定優(yōu)先級(jí)進(jìn)行隊(duì)列元素排序的相關(guān)操作技巧,需要的朋友可以參考下2018-02-02Python獲取央視節(jié)目單的實(shí)現(xiàn)代碼
這篇文章主要介紹了Python獲取央視節(jié)目單的實(shí)現(xiàn)代碼,涉及Python頁面采集的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07python實(shí)現(xiàn)簡(jiǎn)易數(shù)碼時(shí)鐘
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡(jiǎn)易數(shù)碼時(shí)鐘,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11Object arrays cannot be loaded when
這篇文章主要介紹了Object arrays cannot be loaded when allow_pickle=False,本文給大家分享問題解決思路,需要的朋友可以參考下2022-11-11