控制#

简介#

The Control blocks in VEXcode AIM manage the flow of a project by handling loops, conditions, and delaying blocks from running.

A loop is when a computer repeats the same step or group of steps over and over until it is told to stop. A condition is a rule the computer checks to decide what to do next. For example, a robot can move forward if a sensor detects an object.

以下是可用指令块的列表:

等待#

The wait stack block pauses blocks from running for a specific amount of time before moving to the next block.

等待指令段#
wait (1) seconds

参数

描述

时间

The amount of time to wait, as a positive whole number or decimal in seconds.

示例

启动后,向前移动一秒钟,然后停止所有移动。#
when started
[Move forward for one second, then stop.]
move [forward v]
wait (1) seconds
stop all movement

等到#

The wait until stack block pauses blocks from running until a specified condition is met before moving to the next block.

等到指令段#
wait until <>

参数

描述

条件

此条件必须在程序继续执行下一个指令块之前被满足。

示例

当程序启动时,向前移动直到屏幕被按下,然后停止所有运动。#
when started
[Move forward until screen pressed, then stop.]
move [forward v]
wait until <screen pressed?>
stop all movement

重复#

The repeat C block runs the blocks inside it a set number of times.

重复C型指令块#
repeat [10]
结束

参数

描述

次数

一个用于设置重复指令块运行次数的整数。

示例

when started
[Move in a square path.]
repeat [4]
move [forward v] for [50] [mm v] ▶
turn [right v] for [90] degrees ▶
结束

永久循环#

The forever C block keeps running the blocks inside it again and again without stopping.

永久循环 C 型指令块#
forever
结束

参数

描述

该指令块没有参数。

示例

启动后,以方形路径连续移动,向前移动 50 毫米,向右旋转 90 度,无限循环。#
when started
[Move in a square path forever.]
forever
move [forward v] for [50] [mm v] ▶
turn [right v] for [90] degrees ▶
结束

重复直到#

The repeat until C block runs the blocks inside it repeatedly while the specified condition is not met.

重复直到指令块#
repeat until <>
结束

参数

描述

条件

An expression or variable that is checked before each loop. If it is False, the blocks continue repeating. If it is True, the loop will stop.

示例

启动时,所有 LED 保持红色,直到按下屏幕,然后将其关闭。#
when started
[Repeat until the screen is pressed before turning off the LEDs.]
repeat until <screen pressed?>
set [lightall v] LED color to [red v]
结束
set [lightall v] LED color to [off v]

#

The while C block runs the blocks inside repeatedly while the specified condition is met.

当 C 型指令块#
while <>
结束

参数

描述

条件

An expression or variable that is checked before each loop. If it is True, the blocks continue repeating. If it is False, the loop will stop.

示例

启动后,向前移动 200 毫米,同时所有 LED 保持绿色。移动停止后,LED 熄灭。#
when started
[Keep the LEDs green while the robot is moving.]
move [forward v] for [200] [mm v] ◀ and don't wait
while <move active?>
set [lightall v] LED color to [green v]
结束
set [lightall v] LED color to [off v]

如果#

The if C block runs the blocks inside if the condition is True.

如果指令块#
if <> then
结束

参数

描述

条件

An expression or variable that is checked when the statement runs. If it is True, the blocks inside the if block will run. If it is False, the blocks are skipped over.

示例

启动后,机器人会持续检测屏幕是否被按下。如果被按下,机器人会以中等力度踢出一个物体。#
when started
[Kick when the screen is pressed.]
forever
if <screen pressed?> then
kick object [medium v]
结束
结束

如果/否则#

The if / else C block determines which set of blocks runs based on whether the condition is True or False.

如果那么否则指令块#
if <> then
else
结束

参数

描述

条件

An expression or variable that is checked when the statement runs. If it is True, the blocks inside the if block will run. If it is False, it will runs the blocks inside the else block.

示例

启动后,持续检测屏幕是否被按下。如果按下,则显示一个兴奋的期待表情;否则,显示一个无聊的期待表情。#
when started
[Show one emoji when the screen is touched and a different one when it's not.]
forever
if <screen pressed?> then
show [emoji_excited v] looking [forward v]
else
show [emoji_bored v] looking [forward v]
结束
结束

如果/否则 如果/否则#

The if / else if / else expandable C block selects which set of blocks runs based on conditions:

  • 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.

如果否则如果否则指令块#
if <> then
else if <> then
else
结束

参数

描述

条件

An expression or variable that is checked when the statement runs. The first condition that is True runs that set of blocks. If all conditions evaluate as False, the blocks inside the else block will run.

示例

当控制器的轴1位置发生变化时,向前推则向前移动,向后拉则反向移动,中立时停止。#
when Controller axis [1 v] is changed
[Move the robot forward or reverse based on the position of the joystick.]
if <(controller axis [1 v] position) [math_greater_than v] [0]> then
move [forward v]
else if <(controller axis [1 v] position) [math_less_than v] [0]> then
move [reverse v]
else
stop all movement
结束

退出循环#

The break stack block exits a loop immediately. This block can be used inside repeat, repeat until, while, and forever blocks. Break is useful when a loop needs to stop early based on something that happens during the project, like a button being pressed or a sensor detecting an object.

退出循环指令段#
break

参数

描述

该指令块没有参数。

示例

启动时,每 0.5 秒闪烁一次红色和绿色 LED,直到按下屏幕,然后停止。#
when started
[Flash LEDs until the screen is pressed.]
forever
set [lightall v] LED color to [red v]
wait [0.5] seconds
set [lightall v] LED color to [green v]
wait [0.5] seconds
if <screen pressed?> then
break
结束
结束

停止程序#

The stop project stack block ends a running project.

停止程序指令块#
stop project

参数

描述

该指令块没有参数。

示例

启动时,每 0.5 秒闪烁一次红色和绿色 LED,直到按下屏幕,然后停止项目。#
when started
[Stop the project once the screen is pressed.]
forever
set [lightall v] LED color to [red v]
wait [0.5] seconds
set [lightall v] LED color to [green v]
wait [0.5] seconds
if <screen pressed?> then
stop project
结束
结束