屏幕#

介绍#

VEX IQ(第二代)大脑屏幕提供了多种方法来显示信息、绘制形状和控制屏幕的视觉元素。

对于以下示例,构造的 Brain 包括对 Screen 方法的访问权限,并且在本 API 文档的所有后续示例中引用这些方法时都将使用它。

以下是所有方法的列表:

Cursor – 文本定位和显示的方法

  • print – Prints text at the current cursor position.

  • setCursor – Sets the cursor to a specific row and column.

  • newLine – Moves the cursor to column 1 of the next row.

  • clearLine – Clears a row of text.

  • row – Returns the current cursor row.

  • column – Returns the current cursor column.

  • printAt – Prints text at a specific x and y location.

  • setClipRegion – Restricts where drawings and text can appear.

  • getStringWidth – Returns the width of a string.

  • getStringHeight – Returns the height of a string.

Mutators – 设置屏幕属性的方法

  • clearScreen – Clears the screen of all drawings and text.

  • setFont – Sets the font for printed text.

  • setPenWidth – Sets the thickness for drawn shapes and lines.

  • setPenColor – Sets the color for outlines and text.

  • setFillColor – Sets the fill color for shapes and backgrounds.

  • setOrigin – Sets a new origin for printing and drawing.

Draw – 绘制形状和图形的方法

  • drawPixel – Draws a pixel at a specific x and y position.

  • drawLine – Draws a line between two points.

  • drawRectangle – Draws a rectangle.

  • drawCircle – Draws a circle.

  • 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(value);

参数

描述

value

要在屏幕上显示的文本。使用 C++ 字符串格式 打印变量。

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Display a message at the starting cursor
  Brain.Screen.print("Hello, Robot!");
}

VEX IQ Brain 屏幕截图,第一行显示白色文字“Hello, Robot!”。

setCursor#

setCursor 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, setCursor positions the cursor based on row and column size, not font style. The font size can be adjusted using setFont.

Usage:
Brain.Screen.setCursor(row, col);

参数

描述

row

游标所在行。

col

光标所在的列。

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Repeatedly print the Brain timer at top-left
  while (true) {
    Brain.Screen.print("%d", Brain.Timer.time(seconds));
    Brain.Screen.newLine();
    wait(1, seconds);
    Brain.Screen.clearScreen();
    Brain.Screen.setCursor(1, 1);
  }
}

IQ Brain 屏幕截图左上角显示白色文字“23.19”。

newLine#

newLine moves the cursor to column 1 on the next row on the Brain’s screen.

Usage:
Brain.Screen.newLine();

参数

描述

该方法没有参数。

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Display two lines of text
  Brain.Screen.print("Line 1");
  Brain.Screen.newLine();
  Brain.Screen.print("Line 2");
}

IQ Brain 屏幕的截图,左上角以白色文字显示“第 1 行”,其正下方显示“第 2 行”。

clearLine#

clearLine clears a row of drawings and text on the Brain’s screen.

Usage:
Brain.Screen.clearLine(row, color);

参数

描述

row

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

color

Optional. Sets the pen color. Options include:

  • black
  • blue
  • blue_green
  • blue_violet
  • cyan
  • green
  • orange
  • purple
  • red
  • red_orange
  • red_violet
  • violet
  • white
  • yellow
  • yellow_green
  • yellow_orange
You can also specify a custom color.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Display text on two rows
  Brain.Screen.print("This text stays");
  Brain.Screen.newLine();
  Brain.Screen.print("This disappears");

  // Wait 3 seconds before clearing row 2
  wait(3, seconds);
  Brain.Screen.clearLine(2);
}

row#

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

Usage:
Brain.Screen.row()

参数

描述

该方法没有参数。

column#

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

Usage:
Brain.Screen.column()

参数

描述

该方法没有参数。

printAt#

printAt displays text on the Brain’s screen at a specified x and y-coordinate (in pixels) with the current font and origin. This method disregards the current cursor position.

Usage:
Brain.Screen.printAt(x, y, value);

参数

描述

x

The x position to print at as a keyword argument, ie: x=, referenced to the screen origin.

y

The y position to print at as a keyword argument, ie: y=, referenced to the screen origin.

value

要在屏幕上打印的文本。使用 C++ 字符串格式来打印变量。

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Print the number 1 at pixel (100, 40)
  Brain.Screen.printAt(100, 40, "1");
}

IQ Brain 屏幕截图,右上角象限 (100, 40) 位置显示白色数字 1。

setClipRegion#

setClipRegion 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.setClipRegion(x, y, width, height);

范围

描述

x

剪辑区域左上角的 x 坐标,以 0 到 160 之间的整数或浮点数给出。

y

剪辑区域左上角的 y 坐标,以 0 到 108 之间的整数或浮点数给出。

width

剪辑区域的宽度(以像素为单位),以 0 到 160 之间的整数或浮点数表示。

height

剪辑区域的高度(以像素为单位),以 0 到 108 之间的整数或浮点数表示。

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Restrict text and drawings to a specific region
  Brain.Screen.setClipRegion(0, 0, 120, 120);
  Brain.Screen.drawRectangle(60, 60, 100, 100, red);
  Brain.Screen.printAt(60, 60, "Cut off!");
}

IQ Brain 屏幕的截图,屏幕中央附近有一段白色文字写着“Cut of”,其正下方有一个实心红色方块,显示了剪辑区域中打印的内容。

getStringWidth#

getStringWidth 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.getStringWidth(string);

参数

描述

string

要测量的字符串。

getStringHeight#

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.getStringHeight(string);

参数

描述

string

要测量的字符串。

修改器#

clearScreen#

clearScreen clears all drawings and text from the Brain’s screen.

Usage:
Brain.Screen.clearScreen();

参数

描述

color

Optional. Sets the pen color. Options include:

  • black
  • blue
  • blue_green
  • blue_violet
  • cyan
  • green
  • orange
  • purple
  • red
  • red_orange
  • red_violet
  • violet
  • white
  • yellow
  • yellow_green
  • yellow_orange
You can also specify a custom color.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Draw a circle and clear it after 2 seconds
  Brain.Screen.drawCircle(80, 50, 20);
  wait(2, seconds);
  Brain.Screen.clearScreen();
}

setFont#

setFont sets the font used for displaying text on the Brain’s screen. This font will apply to all text printed with print or printAt. The default font at the start of a project is mono20.

Usage:
Brain.Screen.setFont(type);

范围

描述

type

Sets the font to one of the following:

  • mono12
  • mono15
  • mono20
  • mono30
  • mono40
  • mono60
  • prop20
  • prop30
  • prop40
  • prop60
These options are shown below.

VEX IQ Brain 屏幕截图,显示一系列 Mono 12 字体的数字和字母。字母 AZ 位于一行,横跨屏幕宽度。左下角指示屏幕宽度为 26 个字符,共 9 行。
mono12

与上一张截图相同,但字体改为 Mono 15。字母 AT 位于一行,横跨屏幕宽度。左下角指示宽度为 20 个字符,行数为 7 行。
mono15

与上一张截图相同,但字体改为 Mono 20。字母 AP 位于一行,横跨屏幕宽度。左下角指示横向 16 个字符,共 5 行。
mono20

与上一张截图相同,现在使用 Mono 30 字体。数字 1 到 9 以及一个额外的 0 位于同一行,横跨屏幕宽度。左下角指示 3 行。
mono30

与上一张截图相同,现在使用 Mono 40 字体。数字 1 到 8 位于第二行,横跨屏幕宽度。
mono40

与上一张截图相同,但字体改为 Mono 60。屏幕上显示大号 MN 60 字体,几乎占据了屏幕上半部分。
mono60

与上一张截图相同,现在使用 Prop 20 字体。字母 AW 位于一行,横跨屏幕宽度。左下角指示横向 26 个字符,共 5 行。
prop20

与上一张截图相同,现在使用 Prop 30 字体。数字 1-9 在一行中重复两次,横跨屏幕宽度。左下角指示横向 18 个字符,共 3 行。
prop30

与上一张截图相同,现在使用 Prop 40 字体。屏幕第一行显示“Prop 40”,第二行显示 14 个字符和 2 行。
prop40

与上一张截图相同,现在使用 Prop 60 字体。屏幕上以大字体显示“PROP 60”,几乎占据了屏幕上半部分。
prop60

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Display two different fonts on two lines
  Brain.Screen.setFont(mono20);
  Brain.Screen.print("Mono Medium");
  Brain.Screen.newLine();
  Brain.Screen.setFont(prop20);
  Brain.Screen.print("Prop Medium");
}

IQ Brain 屏幕截图,左上角有两行白色文字。第一行是 Mono Medium,紧随其后的第二行字体较小,是 Prop Medium。

setPenWidth#

setPenWidth sets the pen width used for drawing lines and shapes.

Usage:
Brain.Screen.setPenWidth(value);

范围

描述

value

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

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Draw two circles with different pen widths
  Brain.Screen.drawCircle(40, 70, 20);
  Brain.Screen.setPenWidth(5);
  Brain.Screen.drawCircle(100, 70, 20);
}

IQ Brain 屏幕的截图,屏幕下半部分印有两个白色圆圈。左边的圆圈比右边的圆圈边框更窄。

setPenColor#

setPenColor sets the pen color used for drawing lines, shapes, and text.

Usage:
Brain.Screen.setPenColor(color);

范围

描述

color

Optional. Sets the pen color. Options include:

  • black
  • blue
  • blue_green
  • blue_violet
  • cyan
  • green
  • orange
  • purple
  • red
  • red_orange
  • red_violet
  • violet
  • white
  • yellow
  • yellow_green
  • yellow_orange
You can also specify a custom color.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Draw two rectangles with different colors
  Brain.Screen.drawRectangle(100, 50, 10, 20);
  Brain.Screen.setPenColor(blue);
  Brain.Screen.drawRectangle(50, 50, 10, 20);
}

IQ Brain 屏幕的截图,屏幕下半部分画有两个小矩形。左边的矩形是蓝色的,右边的矩形是白色的。

setFillColor#

setFillColor method sets the fill color used when shapes are drawn.

Usage:
Brain.Screen.setFillColor(color);

范围

描述

color

Optional. Sets the pen color. Options include:

  • black
  • blue
  • blue_green
  • blue_violet
  • cyan
  • green
  • orange
  • purple
  • red
  • red_orange
  • red_violet
  • violet
  • white
  • yellow
  • yellow_green
  • yellow_orange
You can also specify a custom color.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Draw a yellow circle
  Brain.Screen.setFillColor(yellow);
  Brain.Screen.drawCircle(50, 50, 20);
}

IQ Brain 屏幕的截图,屏幕左半部分画着一个亮黄色圆圈,圆圈边缘有细细的白色边框。

setOrigin#

setOrigin 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.setOrigin(x, y);

范围

描述

x

设置为原点的新 x 坐标,以 0 至 160 之间的整数形式给出。

y

设置作为原点的新 y 坐标,以 0 至 108 之间的整数形式给出。

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Draw a line with the origin set to (50, 50)
  Brain.Screen.setOrigin(50, 50);
  Brain.Screen.drawLine(1, 1, 159, 107);
}

一张 IQ Brain 屏幕的截图,其中一条白线从略偏左侧的位置开始,向右下方斜延伸至屏幕右下角。

#

drawPixel#

drawPixel draws a pixel at the specified (x, y) screen coordinate with the current pen color.

Usage:
Brain.Screen.drawPixel(x, y);

范围

描述

x

将绘制像素的 x 坐标,以 0 至 160 之间的整数表示。

y

将绘制像素的 y 坐标,以 0 至 108 之间的整数表示。

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Draw one pixel at the center of the screen
  Brain.Screen.drawPixel(80, 50);
}

IQ Brain 屏幕的截图,屏幕中央有一个小白点。

drawLine#

drawLine draws a line from the first specified screen coordinate (x1, y1) to the second specified screen coordinate (x2, y2). It uses the current pen width and pen color.

The x and y-coordinates use the default origin of (0, 0) unless a different origin has been set using setOrigin.

Usage:
Brain.Screen.drawLine(x1, y1, x2, y2);

范围

描述

x1

线的起始 x 坐标,以 0 至 160 之间的整数表示。

y1

线的起始 y 坐标,以 0 至 108 之间的整数表示。

x2

线的结束 x 坐标,以 0 至 160 之间的整数表示。

y2

线的结束 y 坐标,以 0 至 108 之间的整数表示。

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Draw a line from top-left to bottom-right
  Brain.Screen.drawLine(0, 0, 159, 107);
}

IQ Brain 屏幕截图,从左上角到右下角画了一条白色对角线。

drawRectangle#

drawRectangle 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 and pen color. The interior is filled with the current fill color.

The x and y-coordinates use the default origin of (0,0) unless a different origin has been set using setOrigin.

Usage:
Brain.Screen.drawRectangle(x, y, width, height, color);

范围

描述

x

矩形左上角的 x 坐标,以 0 至 160 之间的整数表示。

y

矩形左上角的 y 坐标,以 0 至 108 之间的整数表示。

width

矩形的宽度,以 0 到 160 之间的整数表示。

height

矩形的高度,以 0 到 108 之间的整数表示。

color

Optional. Sets the pen color. Options include:

  • black
  • blue
  • blue_green
  • blue_violet
  • cyan
  • green
  • orange
  • purple
  • red
  • red_orange
  • red_violet
  • violet
  • white
  • yellow
  • yellow_green
  • yellow_orange
You can also specify a custom color.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Draw a red rectangle on the screen
  Brain.Screen.drawRectangle(25, 25, 100, 50, red);
}

IQ Brain 屏幕的截图,屏幕上几乎居中处画着一个带有细白边框的红色大矩形。

drawCircle#

drawCircle 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 and pen color. The interior is filled with the current fill color.

The x and y-coordinates use the default origin of (0,0) unless a different origin has been set using setOrigin.

Usage:
Brain.Screen.drawCircle(x, y, radius, color);

范围

描述

x

圆心的 x 坐标,以 0 至 160 之间的整数表示。

y

圆心的 y 坐标,以 0 至 160 之间的整数表示。

radius

圆的半径,以 0 到 108 像素之间的整数表示。

color

Optional. Sets the pen color. Options include:

  • black
  • blue
  • blue_green
  • blue_violet
  • cyan
  • green
  • orange
  • purple
  • red
  • red_orange
  • red_violet
  • violet
  • white
  • yellow
  • yellow_green
  • yellow_orange
You can also specify a custom color.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Draw a green circle on the screen
  Brain.Screen.drawCircle(80, 50, 20, green);
}

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

Usage:
Brain.Screen.render();

参数

描述

该方法没有参数。

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Render text after moving
  Brain.Screen.render();
  Brain.Screen.print("Render later...");
  Drivetrain.driveFor(forward, 100, mm);
  Drivetrain.turnFor(right, 90, degrees);
  Brain.Screen.render();
}