屏幕#
介绍#
屏幕方法控制 IQ(第二代)大脑如何在其内置屏幕上显示文本、数字和图形。
大脑的绘图分辨率为 160 x 108 像素。
本页面使用 brain 作为示例 Brain 名称。请根据需要将其替换为您自己配置的名称。
以下是所有方法的列表:
光标打印 — 放置并打印文本。
print— 在当前光标位置打印文本、数字或变量值。set_cursor— 将光标移动到指定的行和列。next_row— 将光标移动到下一行的第 1 列。clear_row— 清除一行文本。row— 返回要打印文本的行。column— 返回要打印文本的列。print_at— 在指定的 x 和 y 坐标处打印文本、数字或变量值。get_string_width— 使用当前字体返回字符串的像素宽度。get_string_height— 使用当前字体返回字符串的像素高度。
修改器——更改屏幕设置、颜色和绘制行为。
clear_screen— 清除屏幕上的所有绘图和文本。set_font— 设置打印文本的字体。set_pen_width— 设置绘制的线条和形状轮廓的粗细。set_pen_color— 设置文本、像素、线条和形状轮廓的颜色。set_fill_color— 设置绘制形状和打印文本背景的填充颜色。set_origin— 设置用于基于坐标的打印和绘图的原点。
绘制 — 在屏幕上添加图形。
draw_pixel— 在坐标处绘制一个像素。draw_line— 在两点之间画一条线。draw_rectangle— 绘制一个矩形。draw_circle— 画一个圆。set_clip_region— 限制图形和文本的显示位置。render— 仅在调用时更新大脑的屏幕,显示文本和图形。
光标打印#
print#
print 在 Brain 屏幕上的当前 光标位置 和 字体 处打印文本、数字或变量值。
Usage:
brain.screen.print(text, precision, opaque)
参数 |
描述 |
|---|---|
|
要在屏幕上打印的文本、数字或变量值。 |
|
可选。显示数字时要显示的小数位数。必须以关键字参数的形式指定,例如: |
|
可选 |
# 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 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.
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 将光标移动到 Brain 屏幕上下一行的第 1 列。
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 清除大脑屏幕上的一行文本。
Usage:
brain.screen.clear_row(row, color)
参数 |
描述 |
|---|---|
排 |
可选。要清除的行。默认值为当前光标所在行。 |
颜色 |
(可选)要应用于已清除行的颜色。选项包括:
|
# 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 条返回将以整数形式打印文本的行。
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 返回文本将以整数形式打印的列。
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 在 Brain 屏幕上,以当前设置的 font 和 origin 为参数,在指定的 x 和 y 坐标处(以像素为单位)打印文本、数字或变量值。此方法忽略当前光标位置。
Usage:
brain.screen.print_at(text, x, y, precision, opaque)
参数 |
描述 |
|---|---|
|
要打印的文本、数字或变量值。 |
|
要打印的 x 坐标作为关键字参数,例如: |
|
要打印的 y 坐标作为关键字参数,例如: |
|
可选。显示数字时要显示的小数位数。必须以关键字参数的形式指定,例如: |
|
可选 |
# 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 返回字符串的宽度(以像素为单位),以整数形式表示,就像它在 Brain 屏幕上显示的那样。宽度会根据字符串的长度和当前字体而变化。
Usage:
brain.screen.get_string_width(string)
参数 |
描述 |
|---|---|
|
要测量的字符串。 |
get_string_height#
get_string_height 返回字符串的高度(以像素为单位),以整数形式表示,就像它在 Brain 屏幕上显示的那样。高度会根据字符串的长度和当前字体而变化。
Usage:
brain.screen.get_string_height(string)
参数 |
描述 |
|---|---|
|
要测量的字符串。 |
修改器#
clear_screen#
clear_screen 清除大脑屏幕上的所有图形和文本。
Usage:
brain.screen.clear_screen(color)
参数 |
描述 |
|---|---|
|
可选。设置屏幕颜色。选项包括:
|
# 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 设置用于在 Brain 屏幕上显示文本的字体。此字体将应用于所有使用 print 或 print_at 打印的文本。项目开始时的默认字体为 MONO20。
Usage:
brain.screen.set_font(fontname)
范围 |
描述 |
|---|---|
|
将字体设置为以下选项之一:
|
|
|
|
|---|---|---|
|
|
|
|
|
|
|
# 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 设置绘制线条和形状轮廓的粗细。
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 设置文本、像素、线条和形状轮廓的颜色。
Usage:
brain.screen.set_pen_color(color)
范围 |
描述 |
|---|---|
|
(可选)要使用的笔和字体颜色。选项包括:
Color.YELLOW_GREEN |
# 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 设置绘制形状时使用的填充颜色。
Usage:
brain.screen.set_fill_color(color)
范围 |
描述 |
|---|---|
|
(可选)要使用的填充颜色。选项包括:
|
# Draw a yellow circle
brain.screen.set_fill_color(Color.YELLOW)
brain.screen.draw_circle(50, 50, 20)

set_origin#
set_origin 设置 Brain 屏幕上用于绘图和基于坐标的打印的原点。默认情况下,原点位于屏幕左上角。
Usage:
brain.screen.set_origin(x, y)
范围 |
描述 |
|---|---|
|
要设置为原点的新 x 坐标,取值范围为 0 到 159 之间的整数。 |
|
要设置为原点的新 y 坐标,取值范围为 0 到 107 之间的整数。 |
# 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 使用当前 笔颜色 在选定的 x 和 y 坐标处绘制一个像素。
Usage:
brain.screen.draw_pixel(x, y)
范围 |
描述 |
|---|---|
|
像素绘制位置的 x 坐标,取值范围为 0 到 159 之间的整数。 |
|
像素绘制位置的 y 坐标,取值范围为 0 到 107 之间的整数。 |
# Draw one pixel at the center
# of the screen
brain.screen.draw_pixel(80, 50)
![]()
draw_line#
draw_line 使用当前的 笔宽 和 笔颜色 从第一个屏幕坐标 (x1, y1) 到第二个屏幕坐标 (x2, y2) 绘制一条线。
x 和 y 坐标使用默认原点 (0, 0),除非使用 set_origin 设置了不同的原点。
Usage:
brain.screen.draw_line(x1, y1, x2, y2)
范围 |
描述 |
|---|---|
|
直线的起始 x 坐标,取值范围为 0 到 159 的整数。 |
|
直线的起始 y 坐标,取值范围为 0 到 107 的整数。 |
|
直线的终点 x 坐标,取值范围为 0 到 159 的整数。 |
|
直线的终点 y 坐标,取值范围为 0 到 107 的整数。 |
# 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 selected (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 and pen color. The interior is filled with the current fill color, unless the optional color parameter is used.
除非使用 set_origin 设置了不同的原点,否则 x 和 y 坐标使用默认原点 (0,0)。
Usage:
brain.screen.draw_rectangle(x, y, width, height, color)
范围 |
描述 |
|---|---|
|
矩形左上角的 x 坐标,取值范围为 0 到 159 的整数。 |
|
矩形左上角的 y 坐标,取值范围为 0 到 107 的整数。 |
|
矩形的宽度,以像素为单位。 |
|
矩形的高度,以像素为单位。 |
|
(可选)矩形的填充颜色。选项包括:
|
# Draw a red rectangle on the screen
brain.screen.draw_rectangle(25, 25, 100, 50, Color.RED)

draw_circle#
draw_circle draws a circle with its center at the selected (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 and pen color. The interior is filled with the current fill color, unless the optional color parameter is used.
除非使用 set_origin 设置了不同的原点,否则 x 和 y 坐标使用默认原点 (0,0)。
Usage:
brain.screen.draw_circle(x, y, radius, color)
范围 |
描述 |
|---|---|
|
圆心的 x 坐标,取值范围为 0 到 159 的整数。 |
|
圆心的 y 坐标,取值范围为 0 到 107 的整数。 |
|
圆的半径,以像素为单位。 |
|
可选。圆圈的填充颜色。选项包括:
|
# Draw a green circle on the screen
brain.screen.draw_circle(80, 50, 20, Color.GREEN)

set_clip_region#
set_clip_region 定义了屏幕上的一个矩形区域,所有图形和文本都将限制在该区域内。该区域之外的任何内容都不会显示。
Usage:
brain.screen.set_clip_region(x, y, width, height)
范围 |
描述 |
|---|---|
|
剪辑区域左上角的 x 坐标,以 0 到 159 之间的整数或浮点数表示。 |
|
剪辑区域左上角的 y 坐标,以 0 到 107 之间的整数或浮点数表示。 |
|
剪辑区域的宽度(以像素为单位)。 |
|
裁剪区域的高度(以像素为单位)。 |
# 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 为大脑屏幕启用双缓冲。调用后,任何绘图命令(例如文本、形状或图像)都不会立即显示在项目的其余部分。相反,更新内容只有在再次使用 render 时才会显示。这使得屏幕更新更加流畅和可控,但也意味着在 render 之前,屏幕内容将不可见。
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()









