螢幕#

介紹#

screen 類別衍生自 brain 基底類別。它控制 IQ(第一代)大腦如何在其內建螢幕上顯示文字和數值。

IQ(第一代)大腦螢幕最多支援 5 行 21 列文字。

VEX IQ(第一代)大腦,帶有編號網格疊加層(第 1-21 列和第 1-5 行)。紅色疊加線突顯網格結構,箭頭分別標示第 1 列、第 21 列、第 1 行、第 2 行和第 5 行。第一行頂部顯示數字 1 到 21。

使用權#

可以透過以下方式存取畫面 screen 類:

Brain.Screen

筆記#

  • The screen object is provided by the Brain. It is not constructed directly.

  • 項目使用單一全域 Brain 對象,因此螢幕函數使用 Brain.Screen呼叫。

例子#

/* This constructor is required when using VS Code.
A Brain is generated automatically at the start of
VEXcode projects. */

// Create the Brain
brain Brain = brain();

// Print text to the Brain screen
Brain.Screen.print("Hello!");

成員功能#

screen 類別包含以下成員函數:

  • print — 將格式化的文字、數字或布林值列印到 Brain 畫面。

  • setCursor — 將文字遊標移到指定的行和列。

  • newLine — 清除目前行的剩餘內容並將遊標移至下一行。

  • clearLine — 清除目前行的剩餘部分或清除指定的行。

  • row — 傳回要列印文字的行。

  • column — 傳回要列印文字的列。

  • getStringHeight — 使用目前字體傳回字串的像素高度。

  • getStringWidth — 使用目前字體傳回字串的像素寬度。

  • printAt — 在 x, y 像素位置列印格式化文字(可選不透明)。

  • clearScreen — 清除整個畫面(可選擇清除為指定顏色)。

  • setFont — 設定用於螢幕列印的字型。

  • setPenWidth — 設定繪製的線條和形狀輪廓的粗細。

  • setPenColor — 設定文字、像素、線條和形狀輪廓的顏色。

  • setFillColor — 設定繪製形狀和列印文字背景的填滿顏色。

  • setOrigin — 設定螢幕座標的原點。

  • setClipRegion — 將螢幕輸出限制在長方形區域內。

  • drawPixel — 在座標處繪製一個像素。

  • drawLine — 在兩點之間畫一條線。

  • drawRectangle — 在 x, y 位置繪製一個指定大小的矩形。

  • drawCircle — 在 x, y 位置繪製一個具有指定半徑的圓。

  • render — 啟用雙緩衝或將後緩衝區渲染到螢幕。

在呼叫任何 screen 成員函數之前,必須建立一個 brain 實例,如下所示:

// Create the Brain
brain Brain = brain();

print#

在 IQ(第一代)大腦螢幕上顯示文字、數字或布林值。

Available Functions

1 — 使用字串字面量或唯讀 C 風格字串列印格式化文字。

void print(
    const char* format,
    ... );

2 — 使用可修改的字元陣列列印格式化文字。

void print(
    char* format,
    ... );

Parameters

範圍

類型

描述

format

const char*

以字串字面量或唯讀 C 風格字串形式提供的格式字串(例如, “Score: %d”)。

format

char*

以可修改字元數組形式提供的格式字串。

…

插入到格式字串中的一個或多個附加價值,以逗號分隔。

Return Values

此函數不傳回值。

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

setCursor#

將遊標移到 IQ(第一代)Brain 螢幕上的特定行和列。下一次 print 呼叫將從該位置開始列印。

Available Functions
void setCursor(
  int32_t row, 
  int32_t col );

Parameters

範圍

類型

描述

row

int32_t

要將遊標移到的行。

col

int32_t

要將遊標移到的列。

Return Values

此函數不傳回值。

Notes
  • 影響使用 print 時後續文字輸出的起始位置。

  • 行和列基於目前設定的螢幕字體。

  • 呼叫 setCursor 只會影響後續文字輸出的位置。

Examples
// Print at row 3 column 7
Brain.Screen.setCursor(3, 7);
Brain.Screen.print("VEXcode!");

第一代 IQ 大腦畫面顯示居中文字「VEXcode!」。

newLine#

將遊標移到大腦螢幕上下一行的開頭。

Available Functions
void newLine();

Parameters

此函數不接受任何參數。

Return Values

此函數不傳回值。

Notes
  • 如果遊標位於句子的中間,則 newLine 將清除遊標之後的行的其餘部分,然後再移至下一行。

  • 呼叫 newLine後,遊標位於下一行的第一列。

  • 使用 setCursor 設定遊標位置和行號。

Examples
// Display two lines of text
Brain.Screen.print("First message");
Brain.Screen.newLine();
Brain.Screen.print("Second message");

第一代 IQ 大腦螢幕顯示“第一個訊息”和“第二個訊息”,分別顯示在不同的行上。

clearLine#

清除 IQ(第一代)大腦畫面上某一行的文字。

Available Functions

1 — 清除遊標位置的目前行剩餘內容。

void clearLine();

2 — 使用目前配置的 填滿顏色 清除指定的行。

void clearLine(
    int number );

3 — 使用預先定義或 自訂顏色 物件清除指定的行。

void clearLine(
    int          number,
    const color& color );

4 — 使用十六進位顏色值清除指定的行。

void clearLine(
    int         number,
    const char* color );

5 — 使用色調值清除指定的行。

void clearLine(
    int number,
    int hue );

Parameters

範圍

類型

描述

number

int

要清除的行號。行號從 1開始。

color

const color&

使用 color 对象清除行。预定义颜色包括:

  • black
  • white
  • red
  • green
  • blue
  • yellow
  • orange
  • purple
  • cyan
  • transparent
这也可以使用自定义颜色对象。

color

const char*

使用十六進位顏色值清除行(例如, “#000000”)。

hue

int

使用 0–359範圍內的整數色調值清除行,表示色輪上的度數。

Return Values

此函數不傳回值。

Notes
  • 清除一行不會改變目前遊標位置。

  • 使用 setCursor 設定遊標位置和行號。

  • IQ(第一代)Brain螢幕是單色顯示屏,因此指定的顏色不會顯示在螢幕上。

Examples
// 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#

返回 IQ(第一代)Brain 畫面上目前遊標所在的行位置。

Available Functions
int32_t row();

Parameters

此函數不接受任何參數。

Return Values

傳回 int32_t ,表示要列印文字的行。

行號從1開始。

Notes
  • 遊標位置基於 螢幕字體。

  • 返回值指示下一個列印輸出將顯示在哪裡。

  • 當列印文字或呼叫 setCursor 時,行值會改變。

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

column#

返回 IQ(第一代)大腦螢幕上將列印文字的列。

Available Functions
int32_t column();

Parameters

此函數不接受任何參數。

Return Values

傳回 int32_t ,表示要列印文字的列。

列編號從 1 開始。

Notes
  • 遊標位置基於 螢幕字體。

  • 返回值指示下一個列印輸出將顯示在哪裡。

  • 當列印文字或呼叫 setCursor 時,列值會發生變化。

Examples
// Display the cursor's current column
Brain.Screen.print("column: ", Brain.Screen.column());

getStringHeight#

傳回字串在 IQ(第一代)Brain 螢幕上渲染時的高度(以像素為單位)。

Available Functions
int32_t getStringHeight( 
    const char* cstr );

Parameters

範圍

類型

描述

cstr

const char*

要測量的文本,以 C 風格字串 的形式提供(例如, “Hello”)。

Return Values

傳回 int32_t ,表示字串在 Brain 螢幕上渲染時的像素高度。

Notes
  • 返回的高度取決於目前選定的螢幕 。

  • 此函數測量字串的長度,但不將其繪製到螢幕上。

  • 傳回的高度反映了字串列印時的外觀。

getStringWidth#

傳回字串在 IQ(第一代)Brain 螢幕上渲染時的寬度(以像素為單位)。

Available Functions
int32_t getStringWidth( 
    const char* cstr );

Parameters

範圍

類型

描述

cstr

const char*

要測量的文本,以 C 風格字串 的形式提供(例如, “Hello”)。

Return Values

傳回 int32_t ,表示字串在 Brain 螢幕上渲染時的像素寬度。

Notes

printAt#

在 IQ(第一代)大腦螢幕上的特定座標處列印文字、數字或布林值。

Available Functions

1 — 在指定的像素位置列印格式化的輸出。

void printAt(
    int32_t    x,
    int32_t    y,
    const char* format,
    ... );

2 — 在指定的像素位置列印格式化的輸出,並設定文字背景是不透明還是透明。

void printAt(
    int32_t    x,
    int32_t    y,
    bool       bOpaque,
    const char* format,
    ... );

Parameters

範圍

類型

描述

x

int32_t

列印位置的 x 座標,以螢幕原點為參考。

y

int32_t

列印位置的 y 座標,以螢幕原點為參考。

bOpaque

bool

Controls whether the printed text is drawn opaquely or transparently:

  • true — The background of the printed text is filled using the current fill color.
  • false — The text is drawn transparently and the background is not filled.

format

const char*

控制螢幕上列印內容的格式字串(例如, “Score: %d”)。

…

插入到格式字串中的一個或多個附加價值,以逗號分隔。

Return Values

此函數不傳回值。

Notes
  • x 和 y 座標是相對於目前螢幕原點的,可以使用 setOrigin 進行更改。

  • 列印文字使用 螢幕字體。

  • 列印文字使用 筆顏色。

  • 使用格式字串時,傳遞的值的數量和類型必須與字串中的 格式說明符 相符。

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

clearScreen#

清除大腦螢幕上的所有圖畫和文字。

Available Functions

1 — 將螢幕清空至黑色。

void clearScreen();

2 — 使用預先定義或 自訂顏色 物件清除螢幕。

void clearScreen(
    const color& color );

3 — 使用十六進位顏色值清除螢幕。

void clearScreen(
    const char* color );

4 — 使用色調值清除螢幕。

void clearScreen(
    int hue );

Parameters

範圍

類型

描述

color

const color&

使用 color 对象清除屏幕。预定义颜色包括:

  • black
  • white
  • red
  • green
  • blue
  • yellow
  • orange
  • purple
  • cyan
  • transparent
也可以使用自定义颜色对象。

color

const char*

使用以字串形式表示的十六進位顏色值清除螢幕(例如, “#000000”)。

hue

int

使用 0–359範圍內的整數色調值清除螢幕,表示色輪上的度數。

Return Values

此函數不傳回值。

Notes
  • IQ(第一代)Brain螢幕是單色顯示屏,因此指定的顏色不會顯示在螢幕上。

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


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

setFont#

設定 IQ(第一代)大腦畫面上顯示的文字所使用的字體。

這控制在使用螢幕文字功能(例如 print)時應用於後續文字輸出的字體。

Available Functions
void setFont(
  fontType font );

Parameters

範圍

類型

描述

font

fontType

將字體設置為以下選項之一:

  • mono12
  • mono15
  • mono20
  • mono30
  • mono40
  • mono60
  • prop20
  • prop30
  • prop40
  • prop60
這些選項如下所示。

Return Values

此函數不傳回值。

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

setPenWidth#

設定繪製線條和形狀輪廓的粗細。

Available Functions
void setPenWidth(
  uint32_t width );

Parameters

範圍

類型

描述

width

uint32_t

筆的寬度,以像素為單位,範圍從 0 到 32。

Return Values

此函數不傳回值。

Notes Examples
// Draw a circle with a pen width of 5
Brain.Screen.drawCircle(40, 70, 20);
Brain.Screen.setPenWidth(5);
Brain.Screen.drawCircle(100, 70, 20);

setPenColor#

設定文字、像素、線條和形狀輪廓的顏色。

Available Functions

1 — 使用預先定義或 自訂顏色 物件設定畫筆顏色。

void setPenColor(
    const color& color );

2 — 使用十六進位顏色值設定畫筆顏色。

void setPenColor(
    const char* color );

3 — 使用色調值設定畫筆顏色。

void setPenColor(
    int hue );

Parameters

範圍

類型

描述

color

const color&

使用 color 对象设置画笔颜色。预定义颜色包括:

  • black
  • white
  • red
  • green
  • blue
  • yellow
  • orange
  • purple
  • cyan
  • transparent

color

const char*

使用以字串形式表示的十六進位顏色值來設定畫筆顏色(例如, “#FF0000”)。

hue

int

使用 0–359範圍內的整數色調值設定畫筆顏色,表示色輪上的度數(例如, 0 為紅色, 120 為綠色, 240 為藍色)。

Return Values

此函數不傳回值。

Notes
  • IQ(第一代)Brain螢幕是單色顯示屏,因此指定的顏色不會顯示在螢幕上。

Examples
Brain.Screen.drawRectangle(100, 50, 10, 20);
Brain.Screen.setPenColor(blue);
Brain.Screen.drawRectangle(50, 50, 10, 20);

setFillColor#

設定繪製形狀時所使用的填滿顏色。

Available Functions

1 — 使用預先定義或 自訂顏色 物件設定填滿顏色。

void setFillColor(
    const color& color );

2 — 使用十六進位顏色值設定填滿顏色。

void setFillColor(
    const char* color );

3 — 使用色調值設定填滿顏色。

void setFillColor(
    int hue );

Parameters

範圍

類型

描述

color

const color&

使用 color 对象设置填充颜色。预定义颜色包括:

  • black
  • white
  • red
  • green
  • blue
  • yellow
  • orange
  • purple
  • cyan
  • transparent

color

const char*

使用以字串形式表示的十六進位顏色值設定填滿顏色(例如, “#00FF00”)。

hue

int

使用 0–359範圍內的整數色調值設定填滿顏色,表示色輪上的度數(例如, 0 為紅色, 120 為綠色, 240 為藍色)。

Return Values

此函數不傳回值。

Notes
  • IQ(第一代)Brain螢幕是單色顯示屏,因此指定的顏色不會顯示在螢幕上。

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

setOrigin#

設定大腦螢幕上繪製圖形的原點。

Available Functions
void setOrigin(
  int32_t x,
  int32_t y );

Parameters

範圍

類型

描述

x

int32_t

原點相對於大腦螢幕左上角的 x 座標,取值範圍為 0 到 159。

y

int32_t

原點相對於大腦螢幕左上角的 y 座標,取值範圍為 0 到 107。

Return Values

此函數不傳回值。

Notes
  • 預設原點位於螢幕左上角座標 (0, 0)。

  • 原點適用於座標函數,例如 drawLine、drawRectangle、drawCircle 和 printAt。

  • 此原點設定將一直有效,直到再次變更或項目重設為止。

setClipRegion#

在螢幕上定義一個矩形區域,所有圖形和文字都將限制在該區域內。該區域以外的任何內容都不會顯示。

Available Functions
void setClipRegion(
  int x,
  int y,
  int width,
  int height );

Parameters

範圍

類型

描述

x

int

剪輯區域左上角的 x 座標,取值範圍為 0 到 159 的整數。

y

int

剪輯區域左上角的 y 座標,取值範圍為 0 到 107 的整數。

width

int

裁切區域的寬度(以像素為單位),取值範圍為 0 到 159 的整數。

height

int

裁切區域的高度(以像素為單位),取值範圍為 0 到 107 的整數。

Return Values

此函數不傳回值。

Notes
  • 即使繪圖函數超出裁切區域,它們仍然會執行,但只有區域內的部分是可見的。

  • x 和 y 座標是相對於目前螢幕原點的,可以使用 setOrigin 進行更改。

  • 剪輯區域僅適用於目前執行緒。

  • 設定新的剪輯區域會取代目前執行緒之前設定的任何剪輯區域。

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

drawPixel#

在 IQ(第一代)Brain 畫面上,於指定的 x 和 y 像素位置繪製一個像素。

Available Functions
void drawPixel(
  int x,
  int y );

Parameters

範圍

類型

描述

x

int

像素繪製位置的 x 座標,取值範圍為 0 到 159 之間的整數。

y

int

像素繪製位置的 y 座標,取值範圍為 0 到 107 之間的整數。

Return Values

此函數不傳回值。

Notes
  • IQ(第一代)Brain螢幕是單色顯示屏,因此指定的顏色不會顯示在螢幕上。

  • x 和 y 座標是相對於目前螢幕原點的,可以使用 setOrigin 進行設定。

  • 如果指定的像素位置在螢幕邊界之外,則不會繪製該像素。

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

drawLine#

在 IQ(第一代)腦力測試畫面上,畫一條直線連接兩點。

Available Functions
void drawLine(
  int x1,
  int y1,
  int x2,
  int y2 );

Parameters

範圍

類型

描述

x1

int

直線的起始 x 座標,取值範圍為 0 到 159 的整數。

y1

int

直線的起始 y 座標,取值範圍為 0 到 107 的整數。

x2

int

直線的終點 x 座標,取值範圍為 0 到 159 的整數。

y2

int

直線的終點 y 座標,取值範圍為 0 到 107 的整數。

Return Values

此函數不傳回值。

Notes
  • IQ(第一代)Brain螢幕是單色顯示屏,因此指定的顏色不會顯示在螢幕上。

  • 線條的寬度由目前筆寬決定,該筆寬透過 setPenWidth 設定。

  • x 和 y 座標是相對於目前螢幕原點的,可以使用 setOrigin 進行設定。

  • 如果線條的任何部分超出螢幕邊界,則只繪製可見部分。

Examples
// Draw a line from the top left to 
// bottom right of the screen
Brain.Screen.drawLine(0, 0, 159, 107);

drawRectangle#

在 IQ(第一代)Brain 畫面上,於指定位置和大小繪製一個矩形。

Available Functions

1 — 使用目前配置的 填滿顏色 繪製並填滿矩形。

void drawRectangle(
    int x,
    int y,
    int width,
    int height );

2 — 使用預先定義或自訂顏色物件繪製並填滿矩形。

void drawRectangle(
    int           x,
    int           y,
    int           width,
    int           height,
    const color&  color );

3 — 使用十六進位顏色值繪製並填滿矩形。

void drawRectangle(
    int           x,
    int           y,
    int           width,
    int           height,
    const char*   color );

4 — 使用色調值繪製並填滿矩形。

void drawRectangle(
    int x,
    int y,
    int width,
    int height,
    int hue );

Parameters

範圍

類型

描述

x

int

矩形左上角的 x 座標,取值範圍為 0 到 159 的整數。

y

int

矩形左上角的 y 座標,取值範圍為 0 到 107 的整數。

width

int

矩形的寬度,取值範圍為 0 到 159 的整數。

height

int

矩形的高度,取值範圍為 0 到 107 之間的整數。

color

const color&

使用 color 对象填充矩形。预定义颜色包括:

  • black
  • white
  • red
  • green
  • blue
  • yellow
  • orange
  • purple
  • cyan
  • transparent
也可以使用自定义颜色对象。

color

const char*

使用十六進位顏色值填滿矩形(例如, “#FF0000”)。

hue

int

使用 0–359範圍內的色調值填滿矩形。

Return Values

此函數不傳回值。

Notes
  • IQ(第一代)Brain螢幕是單色顯示屏,因此指定的顏色不會顯示在螢幕上。

  • 輪廓粗細由目前筆寬決定,該筆寬透過 setPenWidth 設定。

  • x 和 y 座標是相對於目前螢幕原點的,可以使用 setOrigin 進行更改。

  • 如果矩形的任何部分位於螢幕邊界之外,則只繪製可見部分。

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

drawCircle#

在 IQ(第一代)大腦畫面上,按指定位置和半徑畫一個圓。

Available Functions

1 — 使用目前配置的 填滿顏色 繪製並填入圓形。

void drawCircle(
    int x,
    int y,
    int radius );

2 — 使用預先定義或自訂顏色物件繪製並填入圓形。

void drawCircle(
    int           x,
    int           y,
    int           radius,
    const color&  color );

3 — 使用十六進位顏色值繪製並填滿一個圓。

void drawCircle(
    int           x,
    int           y,
    int           radius,
    const char*   color );

4 — 使用色相值繪製並填滿圓形。

void drawCircle(
    int x,
    int y,
    int radius,
    int hue );

Parameters

範圍

類型

描述

x

int

圓心的 x 座標,取值範圍為 0 到 159 之間的整數。

y

int

圓心的 y 座標,取值範圍為 0 到 107 的整數。

radius

int

圓的半徑,取值範圍為 0 到 107 像素的整數。

color

const color&

使用 color 对象填充圆圈。预定义颜色包括:

  • black
  • white
  • red
  • green
  • blue
  • yellow
  • orange
  • purple
  • cyan
  • transparent
也可以使用自定义颜色对象。

color

const char*

使用十六進位顏色值填滿圓圈(例如, “#FF0000”)。

hue

int

使用 0–359範圍內的色調值填滿圓形。

Return Values

此函數不傳回值。

Notes
  • IQ(第一代)Brain螢幕是單色顯示屏,因此指定的顏色不會顯示在螢幕上。

  • 輪廓粗細由目前筆寬決定,該筆寬透過 setPenWidth 設定。

  • x 和 y 座標是相對於目前螢幕原點的,可以使用 setOrigin 進行更改。

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

render#

啟用大腦螢幕的雙緩衝功能。呼叫後,任何繪圖功能(例如文字或形狀)在專案剩餘時間內都不會立即顯示。相反,更新內容僅在再次使用 render 時才會顯示。這可以實現更流暢、更可控的螢幕更新,但意味著在 render 之前,螢幕內容將不可見。

Available Functions

1 — 啟用雙緩衝或將後緩衝區渲染到螢幕。

bool render();

2 — 將後緩衝區渲染到螢幕上,並可控制垂直同步行為和可選的調度程序執行。

bool render(
    bool bVsyncWait,
    bool bRunScheduler = true );

Parameters

範圍

類型

描述

bVsyncWait

bool

  • true — Wait for the screen’s vertical refresh before rendering to reduce visual tearing.
  • false — Do not wait for the screen’s vertical refresh before rendering.

bRunScheduler

bool

可選

  • true (預設)— 允許在等待渲染時執行後台任務。 4
  • false — 不允許在等待渲染時執行後台任務。

Return Values

傳回布林值指示緩衝區中儲存的圖形是否已成功渲染到螢幕上:

  • true — 圖形已成功渲染到螢幕上。 4
  • false — 圖形無法渲染到螢幕上
  • Notes
    • 調用 render 將需要 render 用於專案的其餘部分。

    Examples
    Brain.Screen.render();
    
    // Draw text to the back buffer
    Brain.Screen.print("Render later...");
    
    // Wait for a screen press to render drawings
    while (!Brain.Screen.pressing()) {
      wait(20, msec);
    }
    Brain.Screen.render();