屏幕#

介绍#

屏幕方法控制 V5 大脑如何在触摸屏上显示文本、数字和图形,以及如何响应触摸输入。

默认情况下,打印到大脑的字体是等宽小号,它有 12 行 48 列。

大脑的绘图分辨率为 480 x 240 像素。

V5 大脑屏幕,红色网格线显示了总共 12 行 48 列的布局。像素尺寸为 479 宽 x 239 高。左上角起始于 0,0 像素,位于第 1 行第 1 列;右下角结束于 479, 239 像素,位于第 12 行第 列。

以下是所有方法的列表:

光标打印 — 放置并打印文本。

  • print — 在当前光标位置打印文本、数字或变量值。

  • set_cursor — 将光标移动到指定的行和列。

  • next_row — 将光标移动到下一行的第 1 列。

  • clear_row — 清除一行文本。

  • row — 返回要打印文本的行。

  • column — 返回要打印文本的列。

  • print_at — 在指定的 x 和 y 坐标处打印文本、数字或变量值。

  • get_string_width — 使用当前字体返回字符串的像素宽度。

  • get_string_height — 使用当前字体返回字符串的像素高度。

  • clear_line — 清除一行文本。

  • new_line — 将光标移到新行。

修改器——更改屏幕设置、颜色和绘制行为。

  • clear_screen — 清除屏幕上的所有绘图和文本。

  • set_font — 设置打印文本的字体。

  • set_pen_width — 设置绘制的线条和形状轮廓的粗细。

  • set_pen_color — 设置文本、像素、线条和形状轮廓的颜色。

  • set_fill_color — 设置绘制形状和打印文本背景的填充颜色。

  • set_origin — 设置用于基于坐标的打印和绘图的原点。

绘制 — 在屏幕上添加图形。

获取器 — 检查触摸状态和触摸位置。

  • pressing — 返回屏幕是否被按下。

  • x_position — 返回最后一次屏幕按下的 x 坐标。

  • y_position — 返回最后一次屏幕按下的 y 坐标。

回调函数——响应屏幕上的触摸事件。

  • pressed — 注册一个在屏幕被按下时要调用的函数。

  • released — 注册一个在屏幕释放时要调用的函数。

有关构建 Brain 以访问 screen 方法的信息,请参阅 Brain。

光标打印#

print#

print 在 Brain 屏幕上以当前光标位置和字体打印文本、数字或变量值。

用法:

brain.screen.print(text, sep, precision)

参数

描述

text

要打印的文本、数字或变量值。

sep

可选。要在值之间插入的字符串。必须将其写成关键字参数(sep=)。默认值为 “”。

precision

可选。打印简单数字时显示的小数位数。必须以关键字参数的形式指定(precision=)。默认值为 2。

# Print on the Brain's screen
brain.screen.print("Hello, Robot!")

V5 大脑的屏幕截图,显示屏幕左上角以白色文字打印着“Hello Robot”。

set_cursor#

set_cursor moves the cursor to a specific row and column on the Brain screen. The next print call will start printing at that location. How many rows and columns can comfortably fit depends on the selected font. With the default monospaced medium font, up to 8 rows and 13 columns can be displayed clearly. Text placed beyond this range may be cut off or become difficult to read.

Monospaced fonts have characters that are all the same width, making text placement consistent. In contrast, proportional fonts vary in character width, so some letters take up more space than others. However, regardless of which type is used, set_cursor positions the cursor based on row and column size, not font style. The font size can be adjusted using the set_font.

用法:

brain.screen.set_cursor(row, column)

参数

描述

row

要将光标移动到的行。

column

要将光标移动到的列。

# Display text starting at Row 3 Column 12
brain.screen.set_cursor(3, 12)
brain.screen.print("Row 3, Column 12")

V5 Brain 的屏幕截图,显示屏幕上方靠近中心位置打印的第 3 行第 12 列。

next_row#

next_row 将光标移动到下一行。

用法:

brain.screen.next_row()

参数

描述

此方法没有参数。

# Print two lines of text on the Brain's screen
brain.screen.print("Line 1")
brain.screen.next_row()
brain.screen.print("Line 2")

V5 Brain 屏幕的截图,显示左上角有两行白色文字。上面一行是“第 1 行”,紧挨着它的第二行是“第 2 行”。

clear_row#

clear_row 清除大脑屏幕上的一行文本。

用法:

brain.screen.clear_row(row, color)

参数

描述

row

可选。要清除的行。默认值为当前光标所在行。

color

可选。有效的颜色、十六进制值或网页字符串。

# Only keep the text on row 1
brain.screen.print("This text stays")
brain.screen.next_row
brain.screen.print("This text disappears")
wait(3, SECONDS)

brain.screen.clear_row()

row#

row 条返回将以整数形式打印文本的行。

用法:

brain.screen.row()

参数

描述

此方法没有参数。

# Display the cursor's current row
brain.screen.set_cursor(3, 2)
brain.screen.print(brain.screen.row())

V5 Brain 屏幕的屏幕截图,显示屏幕左上角第 3 行第 2 列位置的白色文字“3”。

column#

column 返回文本将以整数形式打印的列。

用法:

brain.screen.column()

参数

描述

此方法没有参数。

# Display the cursor's current column
brain.screen.set_cursor(3, 15)
brain.screen.print(brain.screen.column())

V5 Brain 屏幕的屏幕截图,显示屏幕左上角第 3 行第 15 列位置的白色文字“15”。

get_string_width#

get_string_width 返回字符串的宽度(以像素为单位),以整数形式表示,使用当前的 font。

Usage:
brain.screen.get_string_width(string)

参数

描述

string

待测量的绳子。

get_string_height#

get_string_height 返回字符串的高度(以像素为单位),使用当前的 font。

Usage:
brain.screen.get_string_height(string)

参数

描述

string

待测量的绳子。

clear_line#

clear_line 清除当前行的文本。

Usage: brain.screen.clear_line()

参数

描述

此方法没有参数。

new_line#

new_line 将光标移动到 Brain 屏幕上下一行的第 1 列。

用法:

brain.screen.new_line()

参数

描述

此方法没有参数。

变异体#

clear_screen#

clear_screen 清除大脑屏幕上的所有图形和文本。

用法:

brain.screen.clear_screen(color)

参数

描述

color

Optional. A valid color option:

  • Color.RED
  • Color.GREEN
  • Color.BLUE
  • Color.YELLOW
  • Color.ORANGE
  • Color.PURPLE
  • Color.CYAN
  • Color.BLACK (Default)
  • Color.WHITE

# Print VEXcode on the screen
brain.screen.print("VEXcode")
wait(2, SECONDS)
# Clear screen to black
brain.screen.clear_screen()

# Print VEXcode on the screen
brain.screen.print("VEXcode")

# Clear screen to blue using predefined color
brain.screen.clear_screen(Color.BLUE)

V5 Brain 屏幕的截图,其中整个可打印区域呈亮蓝色。

set_font#

set_font 设置用于在大脑屏幕上显示文本的字体。此字体将应用于此方法运行后打印的所有文本。

用法:

brain.screen.set_font(font)

参数

描述

font

以下是一个有效的字体选项。

V5 Brain 屏幕的截图,左上角以 MONO 12 字体打印着白色的数字和字母。其中一行显示 AZ,大约占屏幕宽度的四分之一。左下角显示 80 列和 20 行。
FontType.MONO12

与上一张图相同,但字体为 Mono 15。图中一行显示“AZ”,几乎占据了屏幕一半的宽度。左下角显示“68 列 16 行”。
FontType.MONO15

与上一张图相同,但字体为 Mono 20。图中一行显示“AZ”,占据了屏幕宽度的近三分之二。左下角显示“48 列 12 行”。
FontType.MONO20

与上一张图相同,但字体为 Mono 30。图中一行显示“AZ”,几乎占据了整个屏幕宽度。左下角显示“32 列 8 行”。
FontType.MONO30

与上一张图片相同,但字体为 Mono 40。图中一行显示 AX,横跨屏幕宽度。左下角显示“24 列 6 行”。
FontType.MONO40

与上一张图片相同,但字体为 Mono 60。图片中一行显示 AP,横跨屏幕宽度。底部显示“16 列 4 行”。
FontType.MONO60

与上一张图片相同,但字体为 Prop 20。图中一行显示 AZ,占据了屏幕宽度的近三分之二。左下角显示“48 列 12 行”。
FontType.PROP20

与上一张图片相同,但字体为 Prop 30。图中一行显示 AZ,几乎占据了整个屏幕宽度。左下角显示“32 列 8 行”。
FontType.PROP30

与上一张图片相同,但字体为 Prop 40。图中一行显示 AU,横跨屏幕宽度。左下角显示 24 列 6 行。
FontType.PROP40

与上一张图片相同,但使用 Prop 60 字体。它显示一行 AN,横跨屏幕宽度。底部显示 15 列和 4 行。
FontType.PROP60

# Set the font type to MONO40
brain.screen.set_font(FontType.MONO40)
brain.screen.print("VEX")

V5 Brain 屏幕的截图显示,左上角有白色的 VEX 字样,字体比默认字体大。

set_pen_width#

set_pen_width 设置绘制线条和形状轮廓的粗细。

用法:

brain.screen.set_pen_width(value)

参数

描述

value

笔的宽度,以像素为单位,范围从 0 到 32。

# Draw a rectangle with a pen width of 10
brain.screen.set_pen_width(10)
brain.screen.draw_rectangle(50, 50, 130, 60)

V5 Brain 屏幕的屏幕截图,显示屏幕左上角打印着一个带有粗边框的矩形。

set_pen_color#

set_pen_color 设置文本、像素、线条和形状轮廓的颜色。

用法:

brain.screen.set_pen_color(color)

参数

描述

color

要使用的笔和字体颜色。选项包括:

  • Color.RED
  • Color.GREEN
  • Color.BLUE
  • Color.YELLOW
  • Color.ORANGE
  • Color.PURPLE
  • Color.CYAN
  • Color.TRANSPARENT
  • Color.WHITE
您还可以指定自定义颜色。

# Draw a rectangle with orange borders
brain.screen.set_pen_color(Color.ORANGE)
brain.screen.draw_rectangle(50, 50, 130, 60)

V5 Brain 屏幕的截图,显示屏幕左上角有一个橙色矩形。

set_fill_color#

set_fill_color 设置绘制形状时使用的填充颜色。

用法:

brain.screen.set_fill_color(color)

参数

描述

color

要使用的填充颜色。选项包括:

  • Color.RED
  • Color.GREEN
  • Color.BLUE
  • Color.YELLOW
  • Color.ORANGE
  • Color.PURPLE
  • Color.CYAN
  • Color.TRANSPARENT
  • Color.WHITE
您还可以指定自定义颜色。

# Draw a purple rectangle
brain.screen.set_fill_color(Color.PURPLE)
brain.screen.draw_rectangle(50, 130, 100, 60)

V5 Brain 屏幕的截图,显示屏幕左下角有一个填充紫色的矩形。

set_origin#

set_origin 设置 Brain 屏幕上用于绘图和基于坐标的打印的原点。默认情况下,原点位于屏幕左上角。

用法:

brain.screen.set_origin(x, y)

参数

描述

x

要设置的原点 x 坐标,范围从 0 到 479。

y

设置为原点的 y 坐标,范围从 0 到 239。

画#

draw_pixel#

draw_pixel 使用当前画笔颜色在选定的 x 和 y 坐标处绘制一个像素。

用法:

brain.screen.draw_pixel(x, y)

参数

描述

x

绘制像素的 x 坐标,范围从 0 到 479,以屏幕原点为参考。

y

绘制像素的 y 坐标,范围从 0 到 239,以屏幕原点为参考。

# Draw the pixels marking the corners of a square
brain.screen.draw_pixel(250, 100)
brain.screen.draw_pixel(275, 100)
brain.screen.draw_pixel(250, 125)
brain.screen.draw_pixel(275, 125)

V5 Brain 屏幕的截图,显示屏幕中心附近一个正方形的四个角由各种像素组成。

draw_line#

draw_line 使用当前笔宽和笔色,从第一个屏幕坐标到第二个屏幕坐标绘制一条线。

用法:

brain.screen.draw_line(start_x, start_y, end_x, end_y)

参数

描述

start_x

行首的 x 坐标(以像素为单位),范围从 0 到 479,参考屏幕原点。

start_y

行首的 y 坐标(以像素为单位),范围从 0 到 239,参考屏幕原点。

end_x

行尾的 x 坐标(以像素为单位),范围从 0 到 479,参考屏幕原点。

end_y

行尾的 y 坐标(以像素为单位),范围从 0 到 239,以屏幕原点为参考。

# Draw a line from the top left to bottom right of the screen
brain.screen.draw_line(0, 0, 479, 239)

V5 Brain 屏幕的截图显示,屏幕中心有一条细斜线,从左上角延伸到右下角。

draw_rectangle#

draw_rectangle 使用当前画笔宽度、画笔颜色和填充颜色绘制矩形,除非使用可选的 color 参数。

用法:

brain.screen.draw_rectangle(x, y, width, height, color)

参数

描述

x

矩形左上角的 x 坐标(像素),取值范围为 0 到 479。

y

矩形左上角的 y 坐标(像素),取值范围为 0 到 239。

width

矩形的宽度。

height

矩形的高度。

color

Optional. A valid color option:

  • Color.RED
  • Color.GREEN
  • Color.BLUE
  • Color.YELLOW
  • Color.ORANGE
  • Color.PURPLE
  • Color.CYAN
  • Color.TRANSPARENT
  • Color.WHITE
You can also specify a custom color.

# Draw a rectangle on the screen
brain.screen.draw_rectangle(50, 50, 130, 60)

V5 大脑屏幕的截图显示,左上象限打印着一个带有细白边框的矩形。

draw_circle#

draw_circle 使用当前的画笔宽度、画笔颜色和填充颜色绘制一个圆,除非使用了可选的 color 参数。

用法:

brain.screen.draw_circle(x, y, radius, color)

参数

描述

x

圆心的 x 坐标(以像素为单位),范围从 0 到 479,参考屏幕原点。

y

圆心的 y 坐标(以像素为单位),范围从 0 到 239,参考屏幕原点。

radius

圆的半径,以像素为单位。

color

可选。要使用的填充颜色。选项包括:

  • Color.RED
  • Color.GREEN
  • Color.BLUE
  • Color.YELLOW
  • Color.ORANGE
  • Color.PURPLE
  • Color.CYAN
  • Color.TRANSPARENT
  • Color.WHITE
您还可以指定自定义颜色。

# Draw a circle on the screen
brain.screen.draw_circle(120, 120, 40)

V5 Brain 屏幕的截图显示,中心画有一个带有细白边框的圆圈。

render#

render 为大脑屏幕启用双缓冲。调用后,任何绘图命令(例如文本、形状或图像)都不会立即显示在项目的其余部分。相反,更新内容只有在再次使用 render 时才会显示。这使得屏幕更新更加流畅和可控,但也意味着在 render 之前,屏幕内容将不可见。

用法:

brain.screen.render()

参数

描述

此方法没有参数。

# Render text after moving
brain.screen.render()
brain.screen.print("Render later...")
drivetrain.drive_for(FORWARD, 100, MM)
drivetrain.turn_for(RIGHT, 90, DEGREES)
brain.screen.render()

set_clip_region#

set_clip_region 定义了屏幕上的一个矩形区域,所有图形和文本都将限制在该区域内。该区域之外的任何内容都不会显示。

用法:

brain.screen.set_clip_region(x, y, width, height)

参数

描述

x

矩形左上角的 x 坐标,以屏幕原点为参考。

y

矩形左上角的 y 坐标,以屏幕原点为参考。

width

矩形的宽度。

height

矩形的高度。

# Restrict text and drawings to a specific region
brain.screen.set_clip_region(0, 0, 120, 120)
brain.screen.draw_rectangle(60, 60, 100, 100, Color.RED)
brain.screen.print_at("Cut off!", x=60, y=60)

V5 Brain 屏幕的屏幕截图,白色文字显示“Cut of”,其正下方有一个实心红色方块,位于屏幕左上角,显示剪辑区域内打印的内容。

触碰#

pressing#

pressing 返回一个布尔值,指示当前是否正在按下大脑屏幕。

  • True - 屏幕被按下。

  • False ​​- 屏幕未被按下。

用法:

brain.screen.pressing()

参数

描述

此方法没有参数。

# Change the screen's color after it's pressed
while not brain.screen.pressing():
    wait(5, MSEC)
brain.screen.set_fill_color(Color.GREEN)
brain.screen.draw_rectangle(0, 0, 479, 239)

# Display different messages after the screen is pressed
while not brain.screen.pressing():
    wait(5, MSEC)
brain.screen.print("First message!")
brain.screen.next_row()
# Lift finger to press the screen again
while brain.screen.pressing():
    wait(5, MSEC)
while not brain.screen.pressing():
    wait(5, MSEC)
brain.screen.print("Second message!")
brain.screen.next_row()

x_position#

x_position 返回上次屏幕按下的 x 坐标,取值范围为 0(左)到 479(右)的整数。

用法:

brain.screen.x_position()

参数

描述

此方法没有参数。

# Display a circle where the screen is pressed
while not brain.screen.pressing():
    wait(5, MSEC)
brain.screen.set_fill_color(Color.WHITE)
brain.screen.draw_circle(brain.screen.x_position(), brain.screen.y_position(), 20)

y_position#

y_position 返回上次屏幕按下的 y 坐标,数值为 0(上)到 239(下)之间的整数。

用法:

brain.screen.y_position()

参数

描述

此方法没有参数。

# Display a circle where the screen is pressed
while not brain.screen.pressing():
    wait(5, MSEC)
brain.screen.set_fill_color(Color.WHITE)
brain.screen.draw_circle(brain.screen.x_position(), brain.screen.y_position(), 20)

回调函数#

pressed#

pressed 注册一个在按下大脑屏幕时要调用的函数。

用法:

brain.screen.pressed(callback, arg)

参数

描述

callback

一个先前定义的函数,当按下大脑屏幕时执行。

arg

可选。包含要传递给回调函数的参数的元组。有关更多信息,请参阅使用带参数的函数。

def my_function():
  brain.screen.print("Screen pressed")

# Call my_function whenever the Brain screen is pressed
brain.screen.pressed(my_function)

released#

released 注册一个在屏幕释放(触摸移除)时要调用的函数。

用法:

brain.screen.released(callback, arg)

参数

描述

callback

先前定义的 函数 在大脑屏幕释放时执行。

arg

可选。包含要传递给回调函数的参数的元组。有关更多信息,请参阅使用带参数的函数。

def my_function():
  brain.screen.print("Screen released")

# Call my_function whenever the Brain screen is released
brain.screen.released(my_function)