Python#

VEXcode VR Python API 参考文档解释了每个命令、方法和函数的作用、使用的输入、返回值以及如何在项目中使用它们。

当您想在将命令添加到项目之前了解该命令、检查它接受或返回的值,或者比较类别中的相关命令时,请使用此参考。

VEXcode VR Python 项目使用 Python 命令。而 Blocks 项目则使用 Blocks API 部分。

VEXcode VR 中的 Python 命令可能取决于您使用的 Playground 和机器人。某些机器人包含一些独特的、并非所有 Playground 都提供的方法。

VR中的游乐场和机器人#

使用 Playgrounds 部分查看每个 Playground 中使用的机器人,并查找 Playground 特定的设置详情、场地信息和挑战背景。

使用 Robots 部分查找具有独特方法的机器人的 Python 页面。机器人页面还会显示每个机器人在哪些 Playground 中使用,以便您了解项目中何时可以使用机器人特有的方法。

如何阅读方法条目#

大多数 Python 条目都包含以下部分:

  • 命令名称 - 命令、方法或函数的正式名称。

  • 描述 - 解释该命令的作用以及何时使用该命令。

  • 用法 - 显示用于在代码中编写命令的语法。

  • 参数 - 列出命令接受的输入,并解释每个输入的作用。

  • 返回值 - 说明命令返回的内容(如果有返回值)。

  • 示例代码 - 展示了在项目中使用该命令的一种方法。

常用 Python API 元素#

元素

它的含义

方法

A command called on an object such as drivetrain.rotation(…).

功能

A command called directly, such as wait(…) or print(…).

范围

传递给方法或函数的值,用于控制其执行的操作。

可选参数

可以省略的参数,以便使用默认行为。

返回值

命令返回的值,例如数字、文本或布尔值。

Python基础词汇#

学期

它的含义

多变的

A name that stores a value, such as angle = 90.

细绳

Text inside quotes, such as “Hello”.

整数

A positive or negative number, such as 90.

漂浮

A number with a decimal, such as 25.5.

布尔值

A value that is either True or False.

争论

A value passed into a method or function call, such as DEGREES in drivetrain.rotation(DEGREES).

示例方法输入#

旋转#

Rotation is how much the robot has turned, measured in degrees. At the beginning of a project, the rotation value is set to 0 degrees. rotation returns the robot’s current rotation.

向右转会增加旋转角度,向左转会减少旋转角度。例如,向右转两圈将得到 720 度的旋转角度。

Usage:
drivetrain.rotation(units)

参数

描述

units

Optional. The rotation unit: DEGREES (default).

def main():
    # Display the rotation after turning
    drivetrain.turn_for(RIGHT, 450, DEGREES)
    brain.print("Rotation: ")
    brain.print(drivetrain.rotation(DEGREES))

# VR threads - Do not delete
vr_thread(main)