屏幕#

介绍#

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

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

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

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

以下是所有方法的列表:

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

  • 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 specific x- and y-coordinates.

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

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

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

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

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

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

  • setClipRegion — Restricts where drawings and text can appear.

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

  • pressing — Returns whether the screen is being pressed.

  • xPosition — Returns the x-coordinate of the last screen event.

  • yPosition — 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 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!");
}

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

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

  // Display text starting at Row 3 Column 12
  Brain.Screen.setCursor(3, 12);
  Brain.Screen.print("Row 3, Column 12");
}

V5 Brain 的屏幕截图,显示屏幕上打印着“第 3 行第 12 列”。

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");
}

V5 大脑的屏幕截图,显示“第 1 行”文本打印在“第 2 行”上方。

clearLine#

clearLine clears a row of drawings and text on the Brain’s screen. By default, this will clear the row that the cursor is currently on.

Default Usage:
Brain.Screen.clearLine();

Overload Usages:
Brain.Screen.clearLine(row); Brain.Screen.clearLine(row, color);

参数

描述

row

要清除的行。

color

Sets the background color of the cleared row. 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()

参数

描述

此方法没有参数。

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

  // Display the cursor's current row
  Brain.Screen.setCursor(3, 12);
  Brain.Screen.print(Brain.Screen.row());
}

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

column#

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

Usage:
Brain.Screen.column()

参数

描述

此方法没有参数。

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

  // Display the cursor's current column
  Brain.Screen.setCursor(5, 15);
  Brain.Screen.print(Brain.Screen.column());
}

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

printAt#

printAt displays text on the Brain’s screen at specified x- and y-coordinates (in pixels) with the current font and origin.

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(220, 120, "Center");
}

V5 大脑的屏幕截图,显示屏幕中央的文字“center”。

getStringWidth#

getStringWidth returns the width of a string as an integer in pixels 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.getStringWidth(string);

参数

描述

string

待测量的绳子。

getStringHeight#

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

参数

描述

string

待测量的绳子。

变异体#

clearScreen#

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

Default Usage:
Brain.Screen.clearScreen();

Overload Usages:
Brain.Screen.clearScreen(color);

参数

描述

color

Sets the background 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();

  // Clear screen after 2 seconds
  Brain.Screen.print("VEXcode");
  wait(2, seconds);
  Brain.Screen.clearScreen();
}

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

  // Clear screen to blue after 2 seconds
  Brain.Screen.print("VEXcode");
  wait(2, seconds);
  Brain.Screen.clearScreen(blue);
}

V5 主控芯片蓝屏截图。

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(font);

范围

描述

font

Sets the font to one of the following:

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

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

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

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

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

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

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

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

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

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

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

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

  // Set the font type to mono40
  Brain.Screen.setFont(mono40);
  Brain.Screen.print("VEX");
}

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

setPenWidth#

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

Usage:
Brain.Screen.setPenWidth(width);

范围

描述

width

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

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

  // Draw a rectangle with a pen width of 10
  Brain.Screen.setPenWidth(10);
  Brain.Screen.drawRectangle(50, 50, 130, 60);
}

V5 大脑的屏幕截图,显示一个带有粗边框的矩形。

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 a rectangle with orange borders
  Brain.Screen.setPenColor(orange);
  Brain.Screen.drawRectangle(50, 50, 130, 60);
}

V5 大脑的屏幕截图,显示一个带有橙色边框的矩形。

setFillColor#

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

Usage:
Brain.Screen.setFillColor(color);

范围

描述

color

Optional. Sets the fill 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 purple rectangle
  Brain.Screen.setFillColor(purple);
  Brain.Screen.drawRectangle(50, 50, 130, 60);
}

V5 大脑的屏幕截图,显示一个填充紫色的矩形。

setOrigin#

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

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

范围

描述

x

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

y

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

#

drawPixel#

drawPixel draws a pixel at the specified x- and y-coordinates with the current pen color.

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

范围

描述

x

像素绘制位置的 x 坐标,取值范围为 0 到 480 之间的整数。

y

像素绘制位置的 y 坐标,取值范围为 0 到 240 之间的整数。

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

  // Draw the pixels marking the corners of a square
  Brain.Screen.drawPixel(250, 100);
  Brain.Screen.drawPixel(275, 100);
  Brain.Screen.drawPixel(250, 125);
  Brain.Screen.drawPixel(275, 125);
}

V5 大脑的屏幕截图,显示了构成正方形各个角点的像素组合。

drawLine#

drawLine draws a line from the first specified screen coordinates (x1, y1) to the second specified screen coordinates (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 到 480 的整数。

y1

直线的起始 y 坐标,取值范围为 0 到 240 之间的整数。

x2

直线的终点 x 坐标,取值范围为 0 到 480 之间的整数。

y2

直线的终点 y 坐标,取值范围为 0 到 240 之间的整数。

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

  // Draw a line from the top left to bottom right of the screen
  Brain.Screen.drawLine(0, 0, 479, 239);
}

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

drawRectangle#

drawRectangle draws a rectangle with its top-left corner at the specified x- and y-coordinates 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.

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

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

范围

描述

x

矩形左上角的 x 坐标,取值范围为 0 到 480 之间的整数。

y

矩形左上角的 y 坐标,取值范围为 0 到 240 的整数。

width

矩形的宽度,取值范围为 0 到 480 之间的整数。

height

矩形的高度,取值范围为 0 到 240 之间的整数。

color

Sets the fill 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 rectangle on the screen
  Brain.Screen.drawRectangle(50, 50, 130, 60);
}

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

drawCircle#

drawCircle draws a circle with its center at the specified x- and y-coordinates 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);

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

范围

描述

x

圆心的 x 坐标,取值范围为 0 到 480 之间的整数。

y

圆心的 y 坐标,取值范围为 0 到 240 之间的整数。

radius

圆的半径,取值范围为 0 到 240 像素的整数。

color

Sets the fill 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 on the screen
  Brain.Screen.drawCircle(240, 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.

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();
}

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 到 480 之间的整数或浮点数表示。

y

裁剪区域左上角的 y 坐标,以 0 到 240 之间的整数或浮点数表示。

width

裁剪区域的宽度(以像素为单位),取值范围为 0 到 480 的整数或浮点数。

height

裁剪区域的高度(以像素为单位),取值范围为 0 到 240 的整数或浮点数。

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!");
}

机器人屏幕上显示一个被切掉的红色矩形,矩形周围有一圈细细的白边,矩形上方显示文字“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.

Usage:
Brain.Screen.pressing()

参数

描述

此方法没有参数。

int main() {
  // Change the screen's color after it's pressed
  while (Brain.Screen.pressing() == false) {
    wait(5, msec);
  }

  Brain.Screen.setFillColor(green);
  Brain.Screen.drawRectangle(0, 0, 479, 239);
}

int main() {
  // Display different messages after the screen is pressed
  while (!Brain.Screen.pressing()) {
    wait(5, msec);
  }
  Brain.Screen.print("First message!");
  Brain.Screen.newLine();

  // Lift finger to press the screen again
  while (Brain.Screen.pressing()) {
    wait(5, msec);
  }
  while (Brain.Screen.pressing() == false) {
    wait(5, msec);
  }
  Brain.Screen.print("Second message!");
  Brain.Screen.newLine();
}

xPosition#

xPosition returns the x-coordinate of the last screen event (press or release) as an integer between 0 and 480.

Usage:
Brain.Screen.xPosition()

参数

描述

此方法没有参数。

int main() {
  // Display a circle where the screen is pressed
  while (Brain.Screen.pressing() == false) {
    wait(5, msec);
  }

  Brain.Screen.drawCircle(Brain.Screen.xPosition(), Brain.Screen.yPosition(), 20, white);
}

yPosition#

yPosition returns the y-coordinate of the last screen event (press or release) as an integer between 0 and 240.

Usage:
Brain.Screen.yPosition()

参数

描述

此方法没有参数。

int main() {
  // Display a circle where the screen is pressed
  while (Brain.Screen.pressing() == false) {
    wait(5, msec);
  }

  Brain.Screen.drawCircle(Brain.Screen.xPosition(), Brain.Screen.yPosition(), 20, white);
}

回调函数#

pressed#

pressed registers a function to be called when the Brain’s Screen is pressed.

Default Usage:
Brain.Screen.pressed(callback)

Overload Usages:
Brain.Screen.pressed(callback, arg)

参数

描述

callback

预先定义的回调函数,当轴值发生变化时会自动调用。该函数必须符合所需的回调函数签名。有关更多信息,请参阅回调函数

arg

可选。用户定义的一个值,在调用回调函数时传递给它。这允许回调函数访问在函数外部定义的数据。

void screenPressed() {
  Brain.Screen.print("screen pressed");
}

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

  // Call screenPressed whenever the Brain screen is pressed
  Brain.Screen.pressed(screenPressed);
}

// arg keeps track of how many times the screen is pressed
void onScreenPressed(void* arg) {
  // Convert arg back to an int pointer
  int* count = (int*)arg;
  (*count)++;

  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.print("Pressed %d times", *count);
}

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

  // Variable to track screen presses
  int pressCount = 0;

  // Register the callback and pass the address of pressCount
  // so the callback can update it each time the screen is pressed
  Brain.Screen.pressed(onScreenPressed, &pressCount);

  // Keep the project running so the callback can be triggered
  while (true) {
    wait(20, msec);
  }
}

released#

released registers a function to be called when the screen is released (touch removed).

Default Usage:
Brain.Screen.released(callback)

Overload Usages:
Brain.Screen.released(callback, arg)

参数

描述

callback

预先定义的回调函数,当轴值发生变化时会自动调用。该函数必须符合所需的回调函数签名。有关更多信息,请参阅回调函数

arg

可选。用户定义的一个值,在调用回调函数时传递给它。这允许回调函数访问在函数外部定义的数据。

void screenReleased() {
  Brain.Screen.print("screen released");
}

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

  // Call screenReleased whenever the Brain screen is released
  Brain.Screen.released(screenReleased);
}

// arg keeps track of how many times the screen is released
void onScreenReleased(void* arg) {
  // Convert arg back to an int pointer
  int* count = (int*)arg;
  (*count)++;  // Increase the release count

  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.print("Released %d times", *count);
}

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

  // Variable to track screen releases
  int releaseCount = 0;

  // Register the callback and pass the address of releaseCount
  // so the callback can update it each time the screen is released
  Brain.Screen.released(onScreenReleased, &releaseCount);
}