2012年2月23日 星期四

如何改裝MIT Scratch-新增程式積木

當玩過一陣子Scratch後,有寫過其他程式語言經驗的人,會覺得Scratch這個小玩具有點綁手綁腳,看到官網論壇發表的一堆改裝版(Scratch Mod),一定有衝動想擴充程式積木或其他功能,但是看到陌生的Smalltalk語法後卻又不知從何著手,Jimmy's papa在這裡教大家改裝Scratch也順便幫自己備忘

這是Jimmy's papa打算新增的程式積木,功能是讓程式等待十秒

寄件者 scratchlab01
目標是加到Control分類底下
寄件者 scratchlab01

首先打開SystemBrowser,依下列順序點選各項
>NScratch-Objects>ScriptableScratchMorph>block specs>blockSpecks
寄件者 scratchlab01
可以看到ScriptableScratchMorph類別在blockSpecs這裡定義所屬的每個程式積木的詳細規格,blockSpecs將會回傳一個多維陣列,內含多個分類命名字串(category name)及各分類所屬的程式積木規格說明陣列(Block specificaton Arrays)

程式積木規格說明格式如下
<block spec string> <block type> <selector> [optional initial argument values]
以下是各細項說明
block spec string : 程式積木規格描述字串
block type : 程式積木型態,如K代表鍵盤事件帽, c代表C型包夾命令等等...
selector : 程式積木所對應的選擇子/函數
optional initial argument values:參數預設值

我們可以到下列的程式碼找到('wait %n secs' t wait:elapsed:from: 1)這一行,複製並且插入下一行
寄件者 scratchlab01
將它改成('wait 10 secs' t wait:elapsed:from: 10)
寄件者 scratchlab01

將分類切換一下,可發現屬於Control分類的黃色程式積木多了一個
寄件者 scratchlab01

底下貼出blockSpecs程式碼供大家參考,並附上程式積木型態(block type)的中文說明,試著改改看,可以看到積木外觀型態的改變
blockSpecs
    "Answer a collection of block specifications for the blocks that are common to all objects.  are interspersed with category names (Strings). A block  is an Array of the form: (<block spec string> <block type> <selector> [optional initial argument values]).

    Explanation of flags:
        -    no flags 無
        b    boolean reporter 布林值回報
        c    c-shaped block containing a sequence of commands (always special form) C形包夾命令
        r    reporter 回報
        s    special form command with its own evaluation rule 特殊格式命令
        t    timed command, like wait or glide 定時命令
        E    message event hat 訊息事件帽
        K    key event hat 鍵盤事件帽
        M    mouse-click event hat 滑鼠點擊事件帽
        S    start event hat 起始事件帽
        W    when <condition> hat (obsolete)"

    | blocks |
    blocks := #(
        'control'
            ('when %m clicked'                S    -)
            ('when %k key pressed'            K    -)
            ('when %m clicked'                M    -)
            -
            ('wait %n secs'                    t    wait:elapsed:from: 1)
            -
            ('forever'                        c    doForever)
            ('repeat %n'                        c    doRepeat 10)
            -
            ('broadcast %e'                    -    broadcast:)
            ('broadcast %e and wait'            s    doBroadcastAndWait)
            ('when I receive %e'            E    -)
            -
            ('forever if %b'                    c    doForeverIf)
            ('if %b'                            c    doIf)
            ('if %b'                            c    doIfElse)
            ('wait until %b'                    s    doWaitUntil)
            ('repeat until %b'                c    doUntil)
            -
            ('stop script'                    s    doReturn)
            ('stop all'                        -    stopAll)
        'operators'
            ('%n + %n'                        r    + - -)
            ('%n - %n'                        r    - - -)
            ('%n * %n'                        r    * - -)
            ('%n / %n'                        r    / - -)
            -
            ('pick random %n to %n'        r    randomFrom:to: 1 10)
            -
            ('%s < %s'                        b    < '' '')
            ('%s = %s'                        b    = '' '')
            ('%s > %s'                        b    > '' '')
            -
            ('%b and %b'                    b    &)
            ('%b or %b'                        b    |)
            ('not %b'                        b    not)
            -
            ('join %s %s'                    r    concatenate:with: 'hello ' 'world')
            ('letter %n of %s'                r    letter:of: 1 'world')
            ('length of %s'                    r    stringLength: 'world')
            -
            ('%n mod %n'                    r    \\ - -)
            ('round %n'                        r    rounded -)
            -
            ('%f of %n'                        r    computeFunction:of: 'sqrt' 10)
        'sound'
            ('play sound %S'                -    playSound:)
            ('play sound %S until done'        s    doPlaySoundAndWait)
            ('stop all sounds'                -    stopAllSounds)
            -
            ('play drum %D for %n beats'    t     drum:duration:elapsed:from: 48 0.2)
            ('rest for %n beats'                t     rest:elapsed:from: 0.2)
            -
            ('play note %N for %n beats'    t    noteOn:duration:elapsed:from: 60 0.5)
            ('set instrument to %I'            -     midiInstrument: 1)
            -
            ('change volume by %n'        -     changeVolumeBy: -10)
            ('set volume to %n%'            -     setVolumeTo: 100)
            ('volume'                        r     volume)
            -
            ('change tempo by %n'            -     changeTempoBy: 20)
            ('set tempo to %n bpm'            -     setTempoTo: 60)
            ('tempo'                            r     tempo)
        'motor'
            ('motor on for %n secs'            t    motorOnFor:elapsed:from: 1)
            ('motor on'                        -    allMotorsOn)
            ('motor off'                        -    allMotorsOff)
            ('motor power %n'                -    startMotorPower: 100)
            ('motor direction %W'            -    setMotorDirection: 'this way')
        'variables'
            ('show variable %v'                -    showVariable:)
            ('hide variable %v'                -    hideVariable:)
        'list'
            ('add %s to %L'                    -    append:toList: 'thing')
            -
            ('delete %y of %L'                -    deleteLine:ofList: 1)
            ('insert %s at %i of %L'            -    insert:at:ofList: 'thing' 1)
            ('replace item %i of %L with %s'        -    setLine:ofList:to: 1 'list' 'thing')
            -
            ('item %i of %L'                    r    getLine:ofList: 1)
            ('length of %L'                    r    lineCountOfList:)
            ('%L contains %s'                b    list:contains: 'list' 'thing')
    ).

    ^ blocks, self obsoleteBlockSpecs

沒有留言:

張貼留言