控制#

介绍#

VEXcode GO 中的控制块通过处理循环、条件、延迟和停止执行来管理项目流程。

Below is a list of all blocks:

  • wait — Pauses execution for a specific duration.

  • wait until — Pauses execution until a specified condition is met.

  • repeat — Repeats enclosed blocks a specific number of times.

  • forever — Repeats enclosed blocks indefinitely.

  • repeat until — Repeats enclosed blocks until a condition is met.

  • while — Repeats enclosed blocks while a condition remains true.

  • if — Runs enclosed blocks if a condition is true.

  • if / else — Runs one set of blocks if a condition is true, otherwise runs another.

  • if / else if / else — Runs different sets of blocks depending on multiple conditions.

  • break — Exits a repeat, forever, repeat until, or while loop immediately.

  • stop project — Ends the execution of the project.

等待#

wait 块在移动到下一个块之前会暂停一段时间。

等待堆栈块#
等待 (1) 秒

参数

描述

时间

等待的时间量,以秒为单位的正整数或小数。

例子

aria-description 写在这里#
当开始 :: hat events
[Build Used: Code Base 2.0]
[Drive forward then stop.]
驱动 [向前 v]
等待 (2) 秒
驱动停止

等到#

等待直到块暂停执行,直到满足指定条件后再继续执行下一个块。

等到堆栈块#
等到 <>

参数

描述

状况

进入下一个区块之前必须满足的条件。

例子

aria-description 写在这里#
当开始 :: hat events
[Build Used: Super Code Base 2.0]
[Drive forward when the LED Bumper is pressed.]
等到 <[bumper v] 按下了?>
驱动 [向前 v] [200] [毫米 v] ▶

重复#

repeat 块会按照设定的次数运行其中的块。

重复 c 块#
重复 [10]
结束

参数

描述

设置重复块运行次数的整数。

例子

aria-description 写在这里#
当开始 :: hat events
[Build Used: Code Base 2.0]
[Drive in a square pattern.]
重复 [4]
驱动 [向前 v] [200] [毫米 v] ▶
[右 v] 转 [90] 度 ▶

永远#

forever 块会不断运行其中的块,而不会停止。

永远的c块#
永久循环
结束

参数

描述

该块没有参数。

例子

aria-description 写在这里#
当开始 :: hat events
[Build Used: Code Base 2.0 - Eye Forward]
[Blink LED light in a pattern.]
永久循环
设定 [bumper v] 为 [绿色 v]
等待 [0.5] 秒
设定 [bumper v] 为 [灭 v]
等待 [0.5] 秒

重复直到#

当条件计算结果为 False 时,repeat until 块会重复执行封闭的块。

重复直到阻塞#
重复直到 <>
结束

参数

描述

状况

An expression or variable that is evaluated before each iteration. If it evaluates as:

  • False — The loop continues
  • True — The loop stops

例子

aria-description 写在这里#
当开始 :: hat events
[Build Used: Code Base 2.0 - Eye Forward]
[Blink LED light until the LED Bumper is pressed.]
重复直到 <[bumper v] 按下了?>
设定 [bumper v] 为 [绿色 v]
等待 [0.5] 秒
设定 [bumper v] 为 [灭 v]
等待 [0.5] 秒

尽管#

当条件计算为 True 时,while 块会重复执行封闭的块。

当 c 块#
当 <>
结束

参数

描述

状况

An expression or variable that is evaluated before each iteration. If it evaluates as:

  • True — The loop continues
  • False — The loop stops

例子

当开始 :: hat events
[Build Used: Code Base 2.0]
[Turn the robot around.]
当 <(底盘归位角度值) [math_less_than v] [180]>
[右 v] 转
结束
驱动停止

如果#

如果条件计算结果为 True,则 if 块执行封闭的代码块。

如果块#
如果 <> 那么
结束

参数

描述

状况

An expression or variable that is evaluated when the statement runs. If it evaluates as:

  • True — The code inside the if block executes
  • False — The block is skipped

例子

aria-description 写在这里#
当开始 :: hat events
[Build Used: Super Code Base 2.0]
[Drive forward whenever the LED Bumper is pressed.]
永久循环
如果 <[bumper v] 按下了?> 那么
驱动 [向前 v] [200] [毫米 v] ▶

如果/否则#

if / else 块根据条件计算结果为 True 还是 False 来确定运行哪个封闭的代码块。

如果则否则块#
如果 <> 那么
否则
结束

参数

描述

状况

An expression or variable that is evaluated when the statement runs. If it evaluates as:

  • True — The code inside the if block executes
  • False — The code inside the else block executes instead

例子

aria-description 写在这里#
当开始 :: hat events
[Build Used: Super Code Base 2.0]
[Drive forward if there is no an object in the way.]
永久循环
如果 <辨色仪发现一个对象?> 那么
驱动停止
否则
驱动 [向前 v] [200] [毫米 v] ▶

如果/否则 如果/否则#

if / else if / else 块结构根据条件选择运行哪个封闭的代码块:

  • if runs its block of code if the condition evaluates as True.

  • else if checks additional conditions only if all previous conditions evaluated as False. Multiple else if statements can be used.

  • else runs its block of code only if none of the previous conditions evaluated as True.

如果否则如果否则块#
如果 <> 那么
否则如果 <> 那么
否则
结束

参数

描述

状况

An expression or variable that is evaluated when the statement runs. The first condition that evaluates as:

  • True — Determines which block of code executes
  • False — If none are True, the else block of code runs

例子

aria-description 写在这里#
当开始 :: hat events
[Build Used: Super Code Base 2.0]
[Turn right for red, left for green.]
永久循环
如果 <eye detects [red v] ?> 那么
[右 v] 转
否则如果 <eye detects [green v] ?> 那么
[左 v] 转
否则
驱动停止

休息#

break 块立即退出循环。

打破堆栈块#
退出循环

参数

描述

该块没有参数。

例子

aria-description 写在这里#
当开始 :: hat events
[Build Used: Super Code Base 2.0]
[Flash the LED until the LED Bumper is pressed.]
永久循环
设定 [bumper v] 为 [绿色 v]
等待 [0.5] 秒
设定 [bumper v] 为 [红色 v]
等待 [0.5] 秒
如果 <[bumper v] 按下了?> 那么
退出循环

停止项目#

停止项目块结束正在运行的项目。

停止项目阻止#
停止程序

参数

描述

该块没有参数。

例子

aria-description 写在这里#
当开始 :: hat events
[Build Used: Super Code Base 2.0]
[Stop the project when the LED Bumper is pressed.]
永久循环
驱动 [向前 v] [100] [毫米 v] ▶
如果 <[bumper v] 按下了?> 那么
停止程序