MS Small Basic第七章: Fun with Shapes(玩形)(Part5: Sound, Program, and Text Objects)

Lesson 2.4: Sound, Program, and Text Objects

In this lesson, you will learn how to:

  • Use the Program object to manage how Small Basic programs run.
  • Enable playback of sounds by using the Sound object.
  • Perform text-related functions by using the Text object.

Introduction to the Program Object

You can control how a program runs by using the Program object in Small Basic.

  1. First, you use the Directory operation to display the current directory of the program that is running.
    • To display the path of the directory, you use the TextWindow object with the WriteLine operation.
  2. Next, you delay the start of the program by five seconds by using the Delay operation and specifying the parameter for time in milliseconds.
  3. You use the End operation to force the program to stop running.

Program.Directory就是把目前正在執行的程式其路徑print出來, Small Basic的print指令是TextWindow.WriteLine. Program.Delay()告訴電腦這個program出現要停留多久的時間, 這邊設定10000毫秒, 也就是10秒鐘, 最後以Program.End將程式關閉.

Exploring the Program Object

By using the Program object, you can also retrieve information about the arguments that are passed to your program. 利用Program物件抽取使用者指定給argument的資訊

Let’s look at an example to understand how you can use the Program object to determine the count and value of arguments that are passed to a program as it runs. 以下例子是程式利用使用者指定的argument值進行計算

這裡我有點疑問, 程式執行時直接跳Else那條, 不知道少了什麼東西…

Introduction to the Sound Object

第一行GraphicsWindow.Show()只是開了一個空白視窗, 沒什麼作用, 因為這邊重點是播音樂. 音檔我選擇一段英國BBC新聞英語自學網站的MP3, 標題是 “News Words: Sanction" , 原本的URL太長, 用Google URL Shortener縮網址. 這裡的音檔可以是.wav, .wma, .mp3等常見音樂檔案的格式. 第三行至第九行就是針對音檔進行播放(Play), 暫停(Pause), 停止(Stop)等動作.

The output of this example plays, pauses, and stops the specified sound file at regular intervals.

You canplay a sound in your program by using the Play operation of the Sound object. You specify the path of the local file or the URL of the file on the network as a parameter to the Play operation. Similarly, you use the Pause operation with the path of the file as the parameter. You stop playing the sound by using the Stop operation.

In addition, observe the use of the Delay operation of the Program object in the example. The Delay operation helps you delay the execution of commands.

You can use sound files in various formats (such as .mp3, .wav, or .wma) in your Small Basic programs.

Exploring the Sound Object

PlayBellRing(), PlayChime()和PlayClick()都是Small Basic內建的音效, 直接使用即可. 微軟線上自學教材在PlayAndWiat的地方犯一個錯誤, 沒有告訴讀者PlayAndWait()的括號裡要放filepath, 也就是我上面那個BBC音檔的路徑. 好在Small Basic會告訴你少了一個參數, 並指出是在第幾行, 再利用intellisense的功能提示, 才讓我發現要括號裡要放音檔的URL或路徑.

In this example, the PlayAndWait operation plays an audio file and then waits until it finishes playing.

This program will play the sound of a bell ringing, a chime, and a click, at regular intervals.

In the previous example, you saw how to play a sound in your program by specifying the path of a sound file. The example here explains how to include built-in sounds such as a ringing bell, a chime, and a click.

After an interval of one second, the program runs the operation PlayBellRing and plays the bell sound. After another second, the PlayChime operation runs, and the chime sound plays. After one more second, the PlayClick operation runs and plays the click sound.

The PlayAndWait operation plays an audio file and waits until it finishes playing. If the particular file is paused, the operation resumes from the position where the audio playback was paused.

Introduction to the Text Object

Small Basic also offers useful operations that you can use to work with text.

For example, you may want to convert all names to uppercase letters, or you might want to search for specific information within some text.

You can use the Text object and its various functions and methods to perform operations on text in Small Basic.

For example, you can determine the length of a text string by using the GetLength operation.

Let’s learn about more operations of the Text object by reviewing an example.

有個小地方要注意, 就是Text.Append(text1, text2), text1就是FirstName, text2就是LastName, Append只能有兩個argument, 第一個是FirstName, 第二個是 " " + LastName, 引號中間空一格的作用是在LastName前面製造一格空白, 才不會FirstName和LastName黏在一起, 所以 " " + LastName合起來算一個argument. 請注意我在第七行最後引號內的驚嘆號前也空一格.

Sponge是海綿, Sponge Bob就是海綿寶寶. 還記得有次看百萬小學堂, 某年級英文題, 題目是: 海綿寶寶叫Sponge ___ . 選項給幾個英文名字. 真有夠他 ___ 的 ___ ___,這關英文屁事啊.

When you work in Small Basic, you can perform operations on text by typing a statement that contains the Text object.

In this example:

  1. First, you ask for the user’s first name and last name by using the Write and Read operations of the TextWindow object.
  2. You then append the last name to the first name by using the Append operation of the Text object.
  3. Next, you convert the appended text into uppercase letters by using the ConvertToUpperCase operation of the Text object.
    • You must specify the name of the variable that is holding the assigned text as a parameter. Similarly, you can also use the ConvertToLowerCase operation to convert the text to lowercase letters.

More on the Text Object

Let’s look at another example to understand more operations of the Text object.

IsSubText(text, subText) 獲得一個給定子文本是否為大文本的子集

  • text : 將在其中搜索子文本的較大文本
  • subText : 待搜索的子文本
  • 返回 : 如果在給定文本中發現子文本則為 True

這樣看不是很好懂, 直接看微軟教材提供的實例:

其實這個例子主要在讓讀者瞭解IsSubText(text, subText)的功能, 因為程式裡的有效email地址只用 “." 和 “@" 限制, 也就是只要使用者輸入的文字包含 “." 和 “@" 就會承認是有效地址, 但用屁股想也知道這樣bug很大, 以下例子是隨便打一串包含 “." 和 “@" 的文字, 但常識告訴我們這絕對不是有效email地址:

Operations of the Text Object

GetSubText(text, start, length) 從給定文本中獲得子文本

  • text : 將在其中產生子文本的文本
  • start : 指定從何處開始
  • length : 指定子文本的長度
  • 返回 : 被請求的子文本

StartsWith(text, subText) 獲得一個給定文本是否是以給定子文本開始

  • text : 在其中進行搜索的較大文本
  • subText : 待搜索的子文本
  • 返回 : 如果在給定文本的開始發現子文本則為 True

EndsWith(text, subText) 獲得一個給定文本是否以給定子文本結束

  • text : 在其中進行搜索的較大文本
  • subText : The sub-text to search for
  • 返回 : 如果在給定文本的最後發現子文本則為 True

GetCharacter(characterCode) 根據 Unicode 字元碼,獲得相應的字元,這些字元可以在正常文本中使用

  • characterCode : 要求字元的字元代碼(基於 Unicode)
  • 返回 : 和特定的代碼相對應的 Unicode 字元

GetCharacterCode(character) 提供一個 Unicode 字元,獲得對應的字元代碼

  • Text : GetCharacterCode(character)
  • character : 其代碼被請求的字元
  • 返回 : 和特定的字元相對應的 Unicode 代碼

GetIndexOf(text, subText) 找到子文本出現在特定文本中的位置

  • text : 在其中進行搜索的文本
  • subText : 待搜索的文本
  • 返回 : 子文本在特定文本中的位置。如果文本未出現,則返回 0
廣告

About leeoxygen

吱吱喳喳,嚕嚕啦啦,嗯嗯啊啊.
本篇發表於 我的Small Basic學習記錄 並標籤為 , 。將永久鏈結加入書籤。

One Response to MS Small Basic第七章: Fun with Shapes(玩形)(Part5: Sound, Program, and Text Objects)

  1. 京都自由行 說道:

    I’m no longer sure the place you are getting your info,
    but great topic. I must spend a while finding
    out more or figuring out more. Thank you for excellent information
    I was in search of this info for my mission.

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s