屏幕#
介绍#
VEX IQ(第二代)大脑屏幕提供了多种方法来显示信息、绘制形状和控制屏幕的视觉元素。
对于以下示例,构造的 Brain 包括对 Screen 方法的访问权限,并且在本 API 文档的所有后续示例中引用这些方法时都将使用它。
以下是所有方法的列表:
光标打印——放置并打印文本。
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_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.set_clip_region
– Restricts where drawings and text can appear.render
– Updates the Brain’s screen with text and drawings only when called.
光标打印#
print#
print
displays text on the Brain’s screen at the current cursor position and font.
Usage:
brain.screen.print(text, precision, opaque)
参数 |
描述 |
---|---|
|
要在屏幕上显示的文本、数字或变量值。 |
|
Optional. The number of decimal places to display when displaying numbers. This must be written as a keyword argument, ie: |
|
Optional.
|
# Display a message at the starting
# cursor position
brain.screen.print("Hello, Robot!")
# Display a percentage with 4 decimal
# places and a blue background
brain.screen.set_fill_color(Color.BLUE)
brain.screen.print((42/100), precision = 4, opaque = True)
set_cursor#
set_cursor
sets the cursor at a specific row and column on the Brain’s screen. How many rows and columns can comfortably fit depends on the selected font.
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 set_font
.
Usage:
brain.screen.set_cursor(row, column)
参数 |
描述 |
---|---|
|
游标所在行。 |
|
光标所在的列。 |
# Repeatedly print the Brain's timer at
# the top left of the screen
while True:
brain.screen.print(brain.timer.time(SECONDS))
brain.screen.next_row()
wait(1, SECONDS)
brain.screen.clear_screen()
brain.screen.set_cursor(1, 1)
next_row#
next_row
moves the cursor to column 1 on the next row on the Brain’s screen.
Usage:
brain.screen.next_row()
参数 |
描述 |
---|---|
该方法没有参数。 |
# Display two lines of text
brain.screen.print("Line 1")
brain.screen.next_row()
brain.screen.print("Line 2")
clear_row#
clear_row
clears a row of drawings and text on the Brain’s screen.
Usage:
brain.screen.clear_row(row, color)
参数 |
描述 |
---|---|
排 |
可选。要清除的行。默认值为当前光标所在行。 |
颜色 |
Optional. The color to apply to the cleared row. Options include:
|
# Display text on two rows
brain.screen.print("This text stays")
brain.screen.next_row()
brain.screen.print("This disappears")
# Wait 3 seconds before clearing
# only the second row
wait(3, SECONDS)
brain.screen.clear_row()
# Turn the 5th row green
brain.screen.clear_row(5, Color.GREEN)
row#
row
returns the current row where text will be printed as an integer.
Usage:
brain.screen.row()
参数 |
描述 |
---|---|
该方法没有参数。 |
# Set cursor to (3,2) and print
# row number
brain.screen.set_cursor(3, 2)
brain.screen.print(brain.screen.row())
column#
column
returns the current column where text will be printed as an integer.
Usage:
brain.screen.column()
参数 |
描述 |
---|---|
该方法没有参数。 |
# Set cursor to (3,2) and print
# column number
brain.screen.set_cursor(3, 2)
brain.screen.print(brain.screen.column())
print_at#
print_at
displays text on the Brain’s screen at a specified x and y-coordinate (in pixels) with the currently set font and set origin. This method disregards the current cursor position.
Usage:
brain.screen.print_at(text, x, y, precision, opaque)
参数 |
描述 |
---|---|
|
要打印的文本。 |
|
The x position to print at as a keyword argument, ie: |
|
The y position to print at as a keyword argument, ie: |
|
Optional. The number of decimal places to display when displaying numbers. This must be written as a keyword argument, ie: |
|
Optional.
|
# Print the number 1 on the Brain's screen at position (100, 40)
brain.screen.print_at(1, x=100, y=40)
# Display 1/3 with 3 decimal places and a blue background
brain.screen.set_fill_color(Color.BLUE)
brain.screen.print_at((1/3), x=100, y=40, precision=3, opaque=True)
get_string_width#
get_string_width
returns the width of a string in pixels, as it would appear on the Brain’s screen. The width varies depending on the string’s length and the current font.
Usage:
brain.screen.get_string_width(string)
参数 |
描述 |
---|---|
|
要测量的字符串。 |
# Display the width of a string in pixels
brain.screen.print("String width ")
brain.screen.print(brain.screen.get_string_width("Hello, Robot!"))
get_string_height#
get_string_height
returns the height of a string in pixels, as it would appear on the Brain’s screen. The width varies depending on the string’s length and the current font.
Usage:
brain.screen.get_string_height(string)
参数 |
描述 |
---|---|
细绳 |
要测量的字符串。 |
# Display the height of a string in pixels
brain.screen.print("String height ")
brain.screen.print(brain.screen.get_string_height("Hello, Robot!"))
修改器#
clear_screen#
clear_screen
clears all drawings and text from the Brain’s screen.
Usage:
brain.screen.clear_screen(color)
参数 |
描述 |
---|---|
颜色 |
Optional. Sets the screen color. Options include:
|
# Draw a circle and clear it after
# 2 seconds
brain.screen.draw_circle(80, 50, 20)
wait(2, SECONDS)
brain.screen.clear_screen()
# Turn the screen red
brain.screen.clear_screen(Color.RED)
set_font#
set_font
sets the font used for displaying text on the Brain’s screen. This font will apply to all text printed with print
or print_at
. The default font at the start of a project is MONO20
.
Usage:
brain.screen.set_font(fontname)
范围 |
描述 |
---|---|
|
Sets the font to one of the following:
|
|
|
|
---|---|---|
|
|
|
|
|
|
|
# Display two different fonts on
# two separate lines
brain.screen.set_font(FontType.MONO20)
brain.screen.print("Mono Medium")
brain.screen.next_row()
brain.screen.set_font(FontType.PROP20)
brain.screen.print("Prop Medium")
set_pen_width#
set_pen_width
sets the pen width used for drawing lines and shapes.
Usage:
brain.screen.set_pen_width(width)
范围 |
描述 |
---|---|
|
笔的宽度,以像素为单位的整数,范围是 0 到 32。 |
# Draw two circles with
# different pen widths
brain.screen.draw_circle(40, 70, 20)
brain.screen.set_pen_width(5)
brain.screen.draw_circle(100, 70, 20)
set_pen_color#
set_pen_color
sets the pen color used for drawing lines, shapes, and text.
Usage:
brain.screen.set_pen_color(color)
范围 |
描述 |
---|---|
|
Optional. Sets the pen color. Options include:
|
# Draw two rectangles with
# different colors
brain.screen.draw_rectangle(100, 50, 10, 20)
brain.screen.set_pen_color(Color.BLUE)
brain.screen.draw_rectangle(50, 50, 10, 20)
set_fill_color#
set_fill_color
method sets the fill color used when shapes are drawn.
Usage:
brain.screen.set_fill_color(color)
范围 |
描述 |
---|---|
|
Optional. Sets the fill color. Options include:
|
# Draw a yellow circle
brain.screen.set_fill_color(Color.YELLOW)
brain.screen.draw_circle(50, 50, 20)
set_origin#
set_origin
sets the origin (0,0) used for drawing or printing on the Brain’s screen. By default, drawing or printing methods consider the top left corner of the screen as the origin. This method can reset the origin to an alternate (x, y) screen coordinate location.
Usage:
brain.screen.set_origin(x, y)
范围 |
描述 |
---|---|
|
设置为原点的新 x 坐标,以 0 至 160 之间的整数形式给出。 |
|
设置作为原点的新 y 坐标,以 0 至 108 之间的整数形式给出。 |
# Draw a line with the origin set to (50, 50)
brain.screen.set_origin(50, 50)
brain.screen.draw_line(1, 1, 159, 107)
画#
draw_pixel#
draw_pixel
draws a pixel at the specified (x, y) screen coordinate in the current pen color. It uses the current pen color set by set_pen_color
.
Usage:
brain.screen.draw_pixel(x, y)
范围 |
描述 |
---|---|
|
将绘制像素的 x 坐标,以 0 至 160 之间的整数表示。 |
|
将绘制像素的 y 坐标,以 0 至 108 之间的整数表示。 |
# Draw one pixel at the center
# of the screen
brain.screen.draw_pixel(80, 50)
draw_line#
draw_line
draws a line from the first specified screen coordinate (x1, y1)
to the second specified screen coordinate (x2, y2)
. It uses the current the pen width set by set_pen_width
and pen color set by set_pen_color
.
The x and y-coordinates use the default origin of (0, 0) unless a different origin has been set using set_origin
.
Usage:
brain.screen.draw_line(x1, y1, x2, y2)
范围 |
描述 |
---|---|
|
线的起始 x 坐标,以 0 至 160 之间的整数表示。 |
|
线的起始 y 坐标,以 0 至 108 之间的整数表示。 |
|
线的结束 x 坐标,以 0 至 160 之间的整数表示。 |
|
线的结束 y 坐标,以 0 至 108 之间的整数表示。 |
# Draw a line from the top left to
# bottom right of the screen
brain.screen.draw_line(0, 0, 159, 107)
draw_rectangle#
draw_rectangle
draws a rectangle with its top-left corner at the specified (x, y)
coordinate and a size determined by the given width and height, all measured in pixels. The rectangle’s outline is drawn using the current pen width set by set_pen_width
and the pen color set by set_pen_color
. The interior is filled with the color set by set_fill_color
.
The x and y-coordinates use the default origin of (0,0) unless a different origin has been set using set_origin
.
Usage:
brain.screen.draw_rectangle(x, y, width, height, color)
范围 |
描述 |
---|---|
|
矩形左上角的 x 坐标,以 0 至 160 之间的整数表示。 |
|
矩形左上角的 y 坐标,以 0 至 108 之间的整数表示。 |
|
矩形的宽度,以 0 到 160 之间的整数表示。 |
|
矩形的高度,以 0 到 108 之间的整数表示。 |
|
Optional. The fill color of the rectangle. Options include:
|
# Draw a red rectangle on the screen
brain.screen.draw_rectangle(25, 25, 100, 50, Color.RED)
画圆#
draw_circle
draws a circle with its center at the specified (x, y)
coordinate and a size determined by the given radius, all measured in pixels. The circle’s outline is drawn using the current pen width set by set_pen_width
and the pen color set by set_pen_color
. The interior is filled with the color set by set_fill_color
.
The x and y-coordinates use the default origin of (0,0) unless a different origin has been set using set_origin
.
Usage:
brain.screen.draw_circle(x, y, radius, color)
范围 |
描述 |
---|---|
|
圆心的 x 坐标,以 0 至 160 之间的整数表示。 |
|
圆心的 y 坐标,以 0 至 160 之间的整数表示。 |
|
圆的半径,以 0 到 108 像素之间的整数表示。 |
|
Optional. The fill color of the circle. Options include:
|
# Draw a green circle on the screen
brain.screen.draw_circle(80, 50, 20, Color.GREEN)
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.
Usage:
brain.screen.set_clip_region(x, y, width, height)
范围 |
描述 |
---|---|
|
剪辑区域左上角的 x 坐标,以 0 到 160 之间的整数或浮点数给出。 |
|
剪辑区域左上角的 y 坐标,以 0 到 108 之间的整数或浮点数给出。 |
|
剪辑区域的宽度(以像素为单位),以 0 到 160 之间的整数或浮点数表示。 |
|
剪辑区域的高度(以像素为单位),以 0 到 108 之间的整数或浮点数表示。 |
# 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)
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.
Usage:
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()