屏幕#

介绍#

屏幕类别控制 V5 大脑的触摸屏,使机器人能够显示文本、数字和图形,并响应触摸输入。

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

对于绘画而言,大脑的分辨率为 479 x 239 像素。

VEX Brain 屏幕的带标签网格图,显示了行、列、像素尺寸和坐标,并用红线勾勒出网格轮廓。

以下是所有方法的列表:

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

  • print — Prints text at the current cursor position.

  • set_cursor — Sets the cursor to a specific row and column.

  • next_row — Moves the cursor to column 1 of the next row.

  • clear_row — Clears a row of text.

  • row — Returns the current cursor row.

  • column — Returns the current cursor column.

  • print_at — Prints text at a specific x and y location.

  • get_string_width — Returns the width of a string in pixels.

  • get_string_height — Returns the height of a string in pixels.

  • clear_line — Clears a line of text.

  • new_line — Moves the cursor to a new line.

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

  • clear_screen — Clears the screen of all drawings and text.

  • set_font — Sets the font for printed text.

  • set_pen_width — Sets the thickness for drawn shapes and lines.

  • set_pen_color — Sets the color for outlines and text.

  • set_fill_color — Sets the fill color for shapes and backgrounds.

  • set_origin — Sets a new origin for printing and drawing.

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

  • draw_pixel — Draws a pixel at a specific x and y position.

  • draw_line — Draws a line between two points.

  • draw_rectangle — Draws a rectangle.

  • draw_circle — Draws a circle.

  • render — Updates the Brain’s screen with text and drawings only when called.

  • set_clip_region — Restricts where drawings and text can appear.

触摸 — 获取屏幕触摸输入。

  • pressing — Returns whether the screen is being pressed.

  • x_position — Returns the X coordinate of the last screen event.

  • y_position — Returns the Y coordinate of the last screen event.

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

  • pressed — Registers a function to be called when the screen is pressed.

  • released — Registers a function to be called when the screen is released.

For information on constructing a Brain to gain access to the screen methods, see Brain.

光标打印#

print#

print prints text on the screen using the current cursor position.

用法:

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

参数

描述

text

要打印的文本。

sep

Optional. A string to insert between values. This must be written as a keyword argument (sep=). The default is “”.

precision

Optional. The number of decimal places to display when printing simple numbers. This must be written as a keyword argument(precision=). The default is 2.

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

V5 大脑的屏幕截图,显示屏幕上打印着“Hello Screen”。

set_cursor#

set_cursor sets the cursor position. The cursor is placed at a specific row and column on the screen. 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 moves the cursor to the 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 大脑的屏幕截图,显示“第 1 行”文本打印在“第 2 行”上方。

clear_row#

clear_row(row, color) clears a row to a single color.

用法:

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 returns the current row as an integer where text will be printed.

用法:

brain.screen.row()

参数

描述

此方法没有参数。

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

V5 Brain 的屏幕截图,显示第 3 行的文本“3”。

column#

column returns the current column as an integer where text will be printed.

用法:

brain.screen.column()

参数

描述

此方法没有参数。

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

V5 Brain 的屏幕截图,显示第 10 列上的文本“10”。

get_string_width#

get_string_width(string) returns the width of a string in pixels as an integer as if it was on the Brain’s screen. The width of a string changes based on the length of the string and the size of the font.

Usage:
brain.screen.get_string_width(string)

参数

描述

string

待测量的绳子。

get_string_height#

get_string_height(string) returns the height of a string as an integer in pixels as if it was on the Brain’s screen. The height of a string changes based on the length of the string and the size of the font.

Usage:
brain.screen.get_string_height(string)

参数

描述

string

待测量的绳子。

clear_line#

clear_line clears the current row of text.

Usage: brain.screen.clear_line()

参数

描述

此方法没有参数。

new_line#

new_line moves the cursor to a new line.

用法:

brain.screen.new_line()

参数

描述

此方法没有参数。

变异体#

clear_screen#

clear_screen(color) clears the whole screen to a single color.

用法:

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 主控芯片蓝屏截图。

set_font#

set_font(fontname) sets the font type used for printing text on the screen.

用法:

brain.screen.set_font(font)

参数

描述

font

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

机器人使用 MONO 12 字体丝网印刷数字和字母。它显示的是 AZ。屏幕底部是 26 行 15 列。
FontType.MONO12

机器人用 MONO 15 字体丝网印刷数字和字母。它显示的是 AT。屏幕底部是 20 行 12 列。
FontType.MONO15

机器人使用 MONO 20 字体丝网印刷数字和字母。它显示的是 AP。屏幕底部是 16 行 9 列。
FontType.MONO20

机器人用MONO 30字体丝网印刷数字和字母。它显示的是AK。屏幕底部是11行6列。
FontType.MONO30

机器人用MONO 40字体丝网印刷数字和字母。它显示的是AK。屏幕底部是8行5列。
FontType.MONO40

机器人使用 MONO 60 字体丝网印刷数字和字母,显示的是 1 到 6。屏幕底部有 3 行。
FontType.MONO60

机器人用 PROP 20 字体丝网印刷数字和字母。它显示的是 AS。屏幕底部是 8 行 9 列。
FontType.PROP20

机器人用 PROP 30 字体丝网印刷了数字和字母。它显示的是 AM。屏幕底部是 15 行 6 列。
FontType.PROP30

机器人用 PROP 40 字体丝网印刷了数字和字母。它显示了 AM。屏幕底部是 15 行 6 列。
FontType.PROP40

机器人用 PROP 60 字体丝网印刷数字和字母,显示的是 1 到 7。屏幕底部是 7 行 3 列。
FontType.PROP60

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

V5 大脑的屏幕截图,显示“VEX”字样的字体比默认字体更大。

set_pen_width#

set_pen_width(width) sets the pen width used for drawing lines, rectangles, and circles.

用法:

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 大脑的屏幕截图,显示一个带有粗边框的矩形。

set_pen_color#

set_pen_color sets the pen color used for drawing lines, rectangles, and circles.

用法:

brain.screen.set_pen_color(color)

参数

描述

color

A valid color option:

  • 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 大脑的屏幕截图,显示一个带有橙色边框的矩形。

set_fill_color#

set_fill_color sets the fill color used for drawing rectangles and circles.

用法:

brain.screen.set_fill_color(color)

参数

描述

color

A valid color option:

  • 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 大脑的屏幕截图,显示一个填充紫色的矩形。

set_origin#

set_origin sets the origin used for drawing graphics on the Brain’s screen. Drawing functions consider the top left corner of the Brain’s screen as the origin. This function can move the origin to an alternate position such as the center of the Brain’s screen.

用法:

brain.screen.set_origin(x, y)

参数

描述

x

原点相对于左上角的 x 坐标,取值范围为 0 到 480 的整数。

y

原点相对于左上角的 y 坐标,取值范围为 0 到 240 的整数。

#

draw_pixel#

draw_pixel draws a pixel using the current pen color.

用法:

brain.screen.draw_pixel(x, y)

参数

描述

x

相对于屏幕原点的像素的 x 坐标。

y

相对于屏幕原点的 y 坐标,用于绘制像素。

# 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 大脑的屏幕截图,显示了构成正方形各个角点的像素组合。

draw_line#

draw_line draws a line using the current pen color.

用法:

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

参数

描述

start_x

以屏幕原点为参考的线段起始点的 x 坐标。

start_y

线段起始点相对于屏幕原点的 y 坐标。

end_x

线段末端相对于屏幕原点的 x 坐标。

end_y

线段末端相对于屏幕原点的 y 坐标。

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

屏幕中央出现一条细斜线,从左上角延伸到/_static/img/screen/draw-line.png。

draw_rectangle#

draw_rectangle draws a rectangle on the screen using the current pen and fill colors.

用法:

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
# Draw a rectangle on the screen
brain.screen.draw_rectangle(50, 50, 130, 60)

机器人的屏幕上显示一个带有细白边框的矩形。

draw_circle#

draw_circle(x, y, radius, color) draws a circle using the current pen and fill colors.

用法:

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

参数

描述

x

圆心相对于屏幕原点的 x 坐标。

y

圆心相对于屏幕原点的 y 坐标。

radius

圆的半径。

color

Optional. A valid color option:

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

机器人的屏幕上显示一个中心画有细白边的圆圈。

render#

render enables double buffering for the Brain’s screen. Once called, any drawing commands (like text, shapes, or images) will no longer appear immediately for the rest of the project. Instead, updates will only be shown when render is used again. This allows for smoother and more controlled screen updates, but means nothing will be visible until render is used.

用法:

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 defines a rectangular area on the screen where all drawings and text will be confined. Any content outside this region will not be displayed.

用法:

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)

机器人屏幕上显示一个被切掉的红色矩形,矩形周围有一圈细细的白边,矩形上方显示文字“CUTOF”。

触碰#

pressing#

pressing returns whether the screen is currently being pressed (touched).

  • True - The screen is being pressed.

  • False - The screen is not being pressed.

用法:

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 returns the X coordinate of the last screen event, press or release as an integer.

用法:

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 returns the Y coordinate of the last screen event, press or release as an integer.

用法:

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 registers a function to be called when the Brain’s Screen is 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 registers a function to be called when the screen is released (touch removed).

用法:

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)