轉變#

介紹#

Switch 模組幫助學生從基於模組的程式設計過渡到基於文字的程式設計。借助 Switch 模組,學生可以直接在模組專案中輸入 Python 程式碼——編寫命令、條件、值或函數定義——同時仍可使用模組建立專案的其他部分。單一模組也可以轉換為對應的 Python 命令。

若要了解如何輸入 Python 命令、方法和運算符,請參閱 VEXcode AIR Python API。

由於 Switch 程式碼區塊包含 Python 程式碼,因此拼字、標點符號和縮排必須正確,專案才能按預期運行。

以下是所有 Switch 模組的清單:

切換模組 — 為 Blocks 專案新增 Python 程式碼。

  • 堆疊 — 提供一個空間來鍵入與堆疊區塊依序運行的 Python 指令。

  • * * C块* * —提供用于键入Python控制代码的空格,例如 if、 while或 for 语句。

  • C Expandable — 提供一個空間來鍵入具有多個分支的 Python 控製程式碼,例如 if, elif或 else。

  • 布林值 — 提供一個空間來鍵入傳回 True 或 False的 Python 程式碼。

  • Reporter — 提供一個空間來鍵入傳回值的 Python 程式碼。

  • Hat — 提供一個空間來定義 Python 函數。

堆疊#

堆疊 區塊提供了一個空間,用於鍵入與其他堆疊區塊按順序運行的 Python 命令。

堆疊塊#
[] :: custom-switch

堆疊 區塊可以包含一行或多行 Python 程式碼。

啟動時,使用 Stack 方塊起飛,等待片刻,然後降落。#
當開始時
[Take off, wait 1 second, then land.]
[drone.take_off(500)] :: custom-switch
等待 [1] 秒
降落 ▶

C 區#

The C Block provides a space to type Python control code, such as if, while, or for statements. Blocks placed inside the C Block run as part of that Python control structure.

C 區#
if [] :: custom-switch
結束

可在C 程式碼區塊中使用的 Python 控制碼包括:

啟動時,使用 C 塊跟隨控制器移動,直到按下按鈕 7。#
當開始時
[Move with the controller until button 7 is pressed.]
起飛至 [500] [mm v] ▶
if [while not controller.button7.pressing():] :: custom-switch
以控制器移動
結束
降落 ▶

C 可擴展#

C Expandable 區塊提供了一個空間,用於輸入具有多個分支的 Python 控製程式碼,例如 if elif或 else可以透過選擇加號箭頭來新增其他分支。

C 可擴展塊#
if [] :: custom-switch-expand
結束

可在C Expandable程式碼區塊中使用的Python控製程式碼包括:

啟動時,使用 C 可擴充模組向上攀爬、向下攀爬或懸停。#
當開始時
[Climb up or down with buttons 5 and 6.]
起飛至 [500] [mm v] ▶
永遠
if [if controller.button5.pressing():] :: custom-switch-expand
爬升 [上升 v]
else [elif controller.button6.pressing():] :: custom-switch-expand
爬升 [下降 v]
else [else:] :: custom-switch-expand
懸停
結束
結束

布林值#

布林值 區塊提供了一個空間,用於輸入傳回 True 或 False的 Python 程式碼。它可以放置在具有六邊形輸入的區塊內。

布爾塊#
<[] :: custom-switch>

布林值區塊可以包含Python條件。

帶有 Python 條件的布林塊#
<[controller.button7.pressing()] :: custom-switch>

布林區塊也可以使用邏輯運算子:

帶有 Python 邏輯運算子的布林區塊#
<[controller.button5.pressing() and controller.button7.pressing()] :: custom-switch>

布林區塊可以使用比較運算子來比較值:

帶有 Python 比較運算子的布林塊#
<[drone.inertial.get_heading()> 100] :: custom-switch>
啟動時,使用布林區塊重複執行,直到按下按鈕 7。#
當開始時
[Move with the controller until button 7 is pressed.]
起飛至 [500] [mm v] ▶
重複直到 <[controller.button7.pressing()] :: custom-switch>
以控制器移動
結束
降落 ▶

記者#

Reporter 程式碼區塊提供了一個用於編寫傳回值的 Python 程式碼的空間。它可以放置在具有循環輸入的程式碼區塊內。

記者欄#
([] :: custom-switch)

Reporter 程式碼區塊可以包含傳回值的 Python 程式碼。

包含傳回值的 Python 程式碼的報告器區塊#
([drone.get_altitude()] :: custom-switch)

數學運算子可以在Reporter模組中使用:

  • 加法:+

  • 減法:-

  • 除法: /

  • 乘法:*

帶有 Python 新增功能的 Reporter 區塊#
([drone.get_altitude() + 45] :: custom-switch)
啟動後,在無人機爬升 3 秒後列印其高度。#
當開始時
[Display the altitude after climbing for 3 seconds.]
起飛至 [500] [mm v] ▶
爬升 [上升 v]
等待 [3] 秒
在螢幕上列印 ([drone.get_altitude()] :: custom-switch) ▶
降落 ▶

帽子#

帽子 程式碼區塊提供了一個定義 Python 函數的空間。附加在 帽子 下的程式碼區塊將成為該函數的主體。

帽塊#
[] :: hat custom-switch

在 Hat 程式碼區塊中輸入 Python 函數定義。參數放在圓括號內。如果函數不使用參數,則使用空圓括號。

帶有 Python 函數定義的帽子模組#
[def function_name(parameter):] :: hat custom-switch
啟動時,使用 Hat 區塊來定義和呼叫函數。#
[def take_off_500():] :: hat custom-switch
[Define a custom function using a Hat block.]
起飛至 [500] [mm v] ▶

當開始時
[take_off_500()] :: custom-switch