屏幕#

介绍#

The Screen methods control how the V5 Brain shows text, numbers, and graphics on its touchscreen, and how it responds to touch input.

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

For drawing, the Brain’s resolution is 480 x 240 pixels.

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

以下是所有方法的列表:

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

  • print — Prints text, numbers, or variable values at the current cursor position.

  • set_cursor — Moves 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 row where text will be printed.

  • column — Returns the column where text will be printed.

  • print_at — Prints text, numbers, or variable values at a specific x and y coordinate.

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

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

  • 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 of drawn lines and shape outlines.

  • set_pen_color — Sets the color of text, pixels, lines, and shape outlines.

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

  • set_origin — Sets the origin used for coordinate-based printing and drawing.

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

Getters — Check touch state and touch position.

  • pressing — Returns whether the screen is being pressed.

  • x_position — Returns the x-coordinate of the last screen press.

  • y_position — Returns the y-coordinate of the last screen press.

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

  • 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, numbers, or variable values on the Brain screen at the current cursor position and font.

用法:

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

参数

描述

text

The text, number, or variable value to print.

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

The row to move the cursor to.

column

The column to move the cursor to.

# 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 Brain 屏幕的截图,显示左上角有两行白色文字。上面一行是“第 1 行”,紧挨着它的第二行是“第 2 行”。

clear_row#

clear_row clears a single row of text on the Brain screen.

用法:

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

用法:

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

用法:

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 returns the width of a string in pixels as an integer, using the current font.

Usage:
brain.screen.get_string_width(string)

参数

描述

string

待测量的绳子。

get_string_height#

get_string_height returns the height of a string in pixels as an integer, using the current 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 column 1 on the next row on the Brain screen.

用法:

brain.screen.new_line()

参数

描述

此方法没有参数。

变异体#

clear_screen#

clear_screen clears all drawings and text from the Brain 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 sets the font used for displaying text on the Brain screen. This font will apply to all text printed after this method runs.

用法:

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 sets the thickness of drawn lines and shape outlines.

用法:

brain.screen.set_pen_width(value)

参数

描述

value

The pen width, in pixels, from 0 to 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 sets the color of text, pixels, lines, and shape outlines.

用法:

brain.screen.set_pen_color(color)

参数

描述

color

The pen and font color to use. Options include:

  • 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 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 sets the fill color used when shapes are drawn.

用法:

brain.screen.set_fill_color(color)

参数

描述

color

The fill color to use. Options include:

  • 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 purple rectangle
brain.screen.set_fill_color(Color.PURPLE)
brain.screen.draw_rectangle(50, 130, 100, 60)

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

set_origin#

set_origin sets the origin used for drawing and coordinate-based printing on the Brain screen. By default, the origin is the top-left corner of the screen.

用法:

brain.screen.set_origin(x, y)

参数

描述

x

The x-coordinate to set as the origin, from 0 to 479.

y

The y-coordinate to set as the origin, from 0 to 239.

#

draw_pixel#

draw_pixel draws one pixel at the selected x and y coordinate using the current pen color.

用法:

brain.screen.draw_pixel(x, y)

参数

描述

x

The x-coordinate to draw the pixel, from 0 to 479, referenced to the screen origin.

y

The y-coordinate to draw the pixel, from 0 to 239, referenced to the screen origin.

# 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 draws a line from the first screen coordinate to the second screen coordinate using the current pen width and pen color.

用法:

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

参数

描述

start_x

The x-coordinate in pixels of the start of the line, from 0 to 479, referenced to the screen origin.

start_y

The y-coordinate in pixels of the start of the line, from 0 to 239, referenced to the screen origin.

end_x

The x-coordinate in pixels of the end of the line, from 0 to 479, referenced to the screen origin.

end_y

The y-coordinate in pixels of the end of the line, from 0 to 239, referenced to the screen origin.

# 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 draws a rectangle using the current pen width, pen color, and fill color, unless the optional color parameter is used.

用法:

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 draws a circle using the current pen width, pen color, and fill color, unless the optional color parameter is used.

用法:

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

参数

描述

x

The x-coordinate in pixels of the center of the circle, from 0 to 479, referenced to the screen origin.

y

The y-coordinate in pixels of the center of the circle, from 0 to 239, referenced to the screen origin.

radius

The radius of the circle, in pixels.

color

Optional. The fill color to use. Options include:

  • 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 circle on the screen
brain.screen.draw_circle(120, 120, 40)

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

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)

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

触碰#

pressing#

pressing returns a Boolean indicating whether the Brain screen is currently being pressed.

  • 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 press as an integer from 0 (left) to 479 (right).

用法:

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 press as an integer from 0 (top) to 239 (bottom).

用法:

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

Optional. A tuple containing arguments to pass to the callback function. See Using Functions with Parameters for more information.

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

Optional. A tuple containing arguments to pass to the callback function. See Using Functions with Parameters for more information.

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

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