转变#

介绍#

Switch 模块使学生能够直接在基于块的环境中编写 Python 代码。这种方法有助于弥合基于块的编程和基于文本的编程之间的差距。通过让用户了解块命令如何转换为 Python,Switch 模块使在熟悉的界面中学习基于文本的编程技巧变得更加容易。使用 Switch 模块可以简化高级逻辑、计算和函数调用,同时仍然使用常规模块。

#

Switch Stack 块用于执行在块的空白处输入的 Switch Python 命令。

它们的形状适合附着在其他动作块的上方或下方。

VEXcode 开关块示例,展示了开关堆栈块#
[] :: custom-switch

Switch Stack 块可用于单行或多行 Switch Python 命令。

为了使 Switch Stack 块按预期运行,必须使用正确的拼写、标点和缩进输入 Switch Python 命令。

VEXcode 开关块示例,展示了一个开关堆叠块,其中包含用于向前行驶 400 毫米的开关 Python 命令#
当开始 :: hat events
[向前移动 400 毫米]
[robot.move_for(400, 0)] :: custom-switch

C 区#

Switch C Block 块用于循环内部的块,直到满足块的空白处的条件。

它们的形状适合在其上方、下方或内部连接堆栈或其他 C 块。

VEXcode 开关块示例,显示开关 C 块#
if [] :: custom-switch
结束

一些可与 Switch C 块一起使用的 Switch Python 命令包括:

  • while

  • if

  • for

为了使 Switch C 块按预期运行,必须使用正确的拼写、标点和缩进输入 Switch Python 命令。

VEXcode 开关块示例,其中开关 c 块内有文本,打印块显示“保险杠被按下”#
当开始 :: hat events
[不断向右转直到按下屏幕。]
[右 v] 转
永久循环
if [ if robot.screen.pressing(): ] :: custom-switch
打印 [屏幕被按下了!] 在屏幕 ▶
停止移动
结束
结束

C 可扩展#

Switch C Expandable 模块用于在满足模块空白处的条件时,循环执行特定分支内的模块。您可以通过扩展模块来添加其他分支。

它们的形状适合将堆叠和其他 C 块附着在其上方、下方或内部。

VEXcode 开关块示例,展示了一个开关 C 可扩展块#
if [] :: custom-switch-expand
结束

一些可与 Switch C Expandable 块一起使用的 Switch Python 命令包括:

  • while

  • if

  • for

为了使 Switch C Expandable 块按预期运行,必须使用正确的拼写、标点和缩进输入 Switch Python 命令。

VEXcode 开关块示例,展示了一个可利用 if else python 代码扩展的 C 开关#
当开始 :: hat events
[不断向前移动,直到机器人拿到运动球。]
永久循环
if [ if robot.has_sports_ball(): ] :: custom-switch-expand
打印 [屏幕被按下了!] 在屏幕 ▶
停止移动
else [else:                                          ]
移动 [正 v]
结束
结束

布尔值#

The Switch Boolean block is used to return a condition as either True or False. These conditions are defined with Switch Python functions.

它们的形状适合放入任何具有六角形(六面)输入的块中。

VEXcode 开关布尔块#
<[] :: custom-switch>

Switch Boolean 块可用于单行 Switch Python 函数,直接返回真或假条件。

运算符可用于在单个 Switch Boolean 块中评估多个 Switch Python 函数。其中包括:

  • and

  • or

  • not

一个 VEXcode 开关块示例,其中包含一行开关 Python 函数的布尔块#
<[controller.button_up.pressing() and controller.button_down.pressing()] :: custom-switch>

Switch Boolean 块还可以将 Switch Python 函数与比较运算符结合起来,使用以下运算符来评估返回值与给定数字的比较情况:

  • Greater Than: >

  • Less Than: <

  • Equal to: =

  • Additional operators that can be used include <=, !=, and >=

使用带有比较运算符的布尔块的 VEXcode 开关块示例#
<[robot.inertial.get_heading() > 100] :: custom-switch>

为了使 Switch Boolean 块按预期运行,必须使用正确的拼写、标点和缩进输入 Switch Python 命令。

使用带比较运算符的布尔块的 VEXcode 开关块示例#
当开始 :: hat events
[不断向右转直到按下屏幕。]
[右 v] 转
永久循环
如果 <[robot.screen.pressing()] :: custom-switch> 那么
打印 [屏幕被按下了!] 在屏幕 ▶
停止移动
结束
结束

记者#

Switch Reporter 模块用于返回数值。这些条件由 Switch Python 函数定义。

它们的形状适合放入任何带有圆形输入的块中。

VEXcode 开关块示例,展示了开关报告块#
([] :: custom-switch)

Switch Reporter 块可用于直接返回数值的单行 Switch Python 函数。

数学函数可用于计算Switch Reporter块中的值。包括但不限于:

  • Addition: +

  • Subtraction: -

  • Division: /

  • Multiplication: *

VEXcode Switch Blocks 示例展示了一个带有加法运算符的 Python 代码的 Switch Reporter Block#
([robot.inertial.get_heading() + 45] :: custom-switch)

为了使 Switch Reporter 块按预期运行,必须使用正确的拼写、标点和缩进输入 Switch Python 命令。

VEXcode 开关块示例,显示打印块内的开关报告块#
当开始 :: hat events
[机器人当前航向 + 45 度将打印在屏幕上]
打印 ([robot.inertial.get_heading() + 45]  :: custom-switch) 在屏幕 ▶

帽子#

当调用该函数时,Switch Hat 块用于运行附加的块堆栈。

它们的形状适合附着在其他动作块上方。

VEXcode 开关块示例,展示了开关帽块#
[] :: hat custom-switch

附加的块堆栈是一个函数。Switch Hat 块用于定义函数的名称和任何可选参数。

Parameters should be enclosed in parentheses and followed by a colon as shown below. If a function has no parameters, use closed parentheses: ().

VEXcode Switch Blocks 示例展示了一个带有 Python 函数的 Switch Hat 块,该函数有一个参数#
[def function_name(parameter):] :: hat custom-switch

为了使 Switch Hat 块按预期运行,必须使用正确的拼写、标点和缩进输入 Switch Python 命令。

VEXcode 开关块示例,其中展示了开关帽块和前进驱动块#
[def move_400():] :: hat custom-switch
[使用 Switch Hat Blocks 定义自定义函数。]
移动 [正 v] (400) [mm v] ▶

当开始 :: hat events
[move_400()] :: custom-switch