<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                >在Scratch中,小貓是可以唱歌的,而且Scratch的聲音木塊有著豐富的功能,在這方面Python turtle略有欠缺,今天我們就來完善一下. ![Scratch聲音模塊](http://upload-images.jianshu.io/upload_images/1108512-cfdae465c49e3e70.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ## Python聲音模塊 ![Python有關聲音的庫有639種,可以完成非常復雜的功能的](http://upload-images.jianshu.io/upload_images/1108512-6474443ff821573e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) Python處理聲音的模塊很多,但是我們要實現的功能并不復雜,所以只需要用到```winsound```這個系統自帶的模塊就好了.我們要的只是實現類似scratch的功能,用不到很復雜的功能的,當然Python能夠做的比scratch多多了. ## Python turtle的代碼結構 最近一直在閱讀python turtle的源代碼對于Python源代碼的結構有所了解,針對造型的操作也重新定義了一些函數來與scratch的積木塊相對應。 在Python turtle基礎上進行擴充而不是使用Pygame、Pyglet等復雜的庫是因為Python turtle本身為了教小孩子編程設計的,在結構上更加符合小孩子的學習習慣,對交互式編程友好,而pygame和pyglet不具備這兩個特點。 ![Python Turtle的代碼結構圖](http://upload-images.jianshu.io/upload_images/1108512-24f2ff8343e5fd31.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ``` class RawTurtle(TPen, TNavigator): # 小烏龜的動畫部分 # 為何添加的函數不工作呢 """Animation part of the RawTurtle. Puts RawTurtle upon a TurtleScreen and provides tools for its animation. """ ``` 其中核心的turtle繼承自RawTurtle類,而RawTurtle繼承自TPen和TNavigator,兩個類分別控制畫筆的繪制和移動,簡單的來說TPen承載的是類似Scratch中畫筆積木類的功能,而TNavigator更多的承載了動作積木類的功能。 ![TNavigator](http://upload-images.jianshu.io/upload_images/1108512-279d6da6d352ff6e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ![TNavigator更多的承載了類似動作積木類的功能](http://upload-images.jianshu.io/upload_images/1108512-28fe1ce598005b69.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ![TNavigator](http://upload-images.jianshu.io/upload_images/1108512-a9cb8b16399f0b06.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ![Tpen](http://upload-images.jianshu.io/upload_images/1108512-d13a42df2b02f7fd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ![TPen更多的實現了畫筆分類的功能](http://upload-images.jianshu.io/upload_images/1108512-ee8cf247c7fef79f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ![TPen更多的實現了畫筆分類的功能](http://upload-images.jianshu.io/upload_images/1108512-5aae07e293da8a6d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 上面兩組功能是否很相似呢。 所以要實現Python turtle的聲音功能,只要重新寫一個聲音相關的類,然后讓```RawTurtle```繼承這個聲音類就ok了,當然還要注意一個問題,turtle所有的類都要加入到```_tg_turtle_functions```這個列表中,這個列表保存了turtle所有的方法,**用來保證在交互式命令行里,默認turtle的各種方法可以直接作為函數調用**. ![image.png](http://upload-images.jianshu.io/upload_images/1108512-48e11f8e361b641b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 讓我們開工把 ## 實現TSound類 ### Scratch積木塊對應的Python 我們一步步的來實現 ![導入```winsound```庫并生命類](http://upload-images.jianshu.io/upload_images/1108512-3a7097265f3ac3b7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 對[winsound](https://docs.python.org/2/library/winsound.html)模塊的同學,可以訪問其在官方的文檔,這里介紹播放聲音的方法: ``` winsound.PlaySound('d:\coding\maow.wav',winsound.SND_FILENAME) # 第一個參數是文件的完整路徑,第二個是播放的標志,winsound.SND_FILENAME代表的是播放的是文件 ``` 首先我們導入winsound模塊之后,然后利用這個模塊的```PlaySound```方法播放指定的聲音文件這樣就實現了 ![播放直到完畢](http://upload-images.jianshu.io/upload_images/1108512-5d2f26304bf04c91.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 停止聲音的辦法是: ``` winsound.PlaySound(None, winsound.SND_PURGE) ``` 對應的是 ![停止所有聲音模塊](http://upload-images.jianshu.io/upload_images/1108512-ce7580074670c9b9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 默認的聲音播放方式是同步的,也就是說播放聲音的時候程序啥也不能干,如下面播放5分鐘的音頻: ``` import winsound as s s.PlaySound("d:\coding\sound.wav",s.SND_FILENAME) print('Playing ended!') ``` 如果這五分鐘的音頻sound.wav沒有播放完畢,程序啥也不能干,尬不尬. 這個積木塊比較霸道,要求必須等他做完了才可以,本來就相當于你跟你女朋友打電話,一般我是邊打電話邊做事情,但是女朋友呢非得要求你啥也不能做,直到電話打完,不然就是不尊重她,不愛她,真尬.當然了少年們,跟女朋友打電話的時候一定不要做別的事情哦. 如何實現異步播放聲音呢,畢竟我們是熱愛時間的好少年. ![播放聲音](http://upload-images.jianshu.io/upload_images/1108512-0cdde800a1c30051.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 這里要用到的是同步標志```winsound.SND_ASYNC``` ![winsound.SND_ASYNC](http://upload-images.jianshu.io/upload_images/1108512-4b1ee590e3f34617.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 雖然同樣是5分鐘的音頻,可是后面的語句很明顯的執行了. ![找到了與這三個積木塊對應的winsound語句](http://upload-images.jianshu.io/upload_images/1108512-57c400b81637ccd0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ### 實現TSound類 ![image.png](http://upload-images.jianshu.io/upload_images/1108512-ee2ea4f0f929efb8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ``` import winsound as snd # 導入聲音類 class TSound(object): """Sounding part of the RawTurtle Implements sounding methods """ def __init__(self, soundfile, resizemode=_CFG["resizemode"]): # 其實Sound并不涉及畫筆的移動和繪制,resizemode不加也是可以滴 self._soundfile = soundfile def play(self, soundfile): snd.PlaySound("d:\coding\sound.wav", snd.SND_ASYNC) def playuntil(self, soundfile): snd.PlaySound("d:\coding\sound.wav", snd.SND_FILENAME) def stopall(self): snd.PlaySound(None, snd.SND_PURGE) ``` 首先導入winsound模塊并重命名為snd,然后生命TSound類,定義三個函數,實現播放,播放直到和停止所有聲音的功能,當然這也僅僅是個實現而已,要真的完善還是需要比較長的時間,只是給大家簡單的舉個例子,這個其實可以對照register_shape和shape函數來定義和完善的. ### 讓RawTurtle繼承TSound類 ![RawTurtle繼承TSound類](http://upload-images.jianshu.io/upload_images/1108512-5f03f718c53cdb15.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ``` class RawTurtle(TPen, TNavigator, TSound): # 小烏龜的動畫部分 # 為何添加的函數不工作呢 """Animation part of the RawTurtle. Puts RawTurtle upon a TurtleScreen and provides tools for its animation. """ ``` ### 將```TSound```的方法添加到Turtle的函數列表 ![添加TSound的方法到turtle的函數列表](http://upload-images.jianshu.io/upload_images/1108512-283aff74934339a5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ![然后再RawTurtle中初始化一下TSound](http://upload-images.jianshu.io/upload_images/1108512-1fa2daa8717dff26.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ![pycat](http://upload-images.jianshu.io/upload_images/1108512-90a1ae064e14c5e9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 我把修改后的文件重命名為pycat,然后導入: ![運行](http://upload-images.jianshu.io/upload_images/1108512-507480e89544e951.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 注意我用IDLE新建了一個sound.py文件然后保存在```d:\coding\yaohao```目錄下,然后導入同目錄下的```pycat```,如果你的python文件和pycat不在同一目錄,是無法導入```pycat```的 ![播放聲音](http://upload-images.jianshu.io/upload_images/1108512-ac590741128df949.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ![停止播放聲音,大功告成](http://upload-images.jianshu.io/upload_images/1108512-e87f87ab86659c2a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看