螢幕#
介紹#
螢幕方法控制 VEX AIR 無人機控制器如何在螢幕上顯示文字、數字、圖形和影像,以及如何回應螢幕按鍵。
以下是可用方法清單:
遊標列印 – 顯示文字並管理列印遊標。
print– 在目前遊標位置列印文字、數字或變數值。set_cursor– 將遊標移到指定的行和列。next_row– 將遊標移到下一行的第 1 列。clear_row– 清除一行文字。get_row– 傳回要列印文字的行。get_column– 傳回要列印文字的欄位。
XY 列印 – 根據像素座標列印文字。
print_at– 在指定的 x 和 y 座標處列印文字、數字或變數值。set_origin– 設定用於基於座標的列印和繪圖的原點。
修改器 – 變更螢幕設定、顏色和繪圖行為。
clear_screen– 清除控制器螢幕上的所有繪圖、文字和影像。wait_for_render– 將螢幕繪製切換為稍後渲染行為。set_font– 設定列印文字的字型。set_pen_width– 設定繪製的線條和形狀輪廓的粗細。set_pen_color– 設定文字、像素、線條和形狀輪廓的顏色。set_text_fill_color– 設定列印文字後面的填滿顏色。set_fill_color– 設定繪製形狀的填滿顏色。
繪圖-在螢幕上新增圖形和影像。
draw_pixel– 在指定的 x 和 y 位置繪製一個像素。draw_line– 在兩點之間繪製一條線。draw_rectangle– 使用目前畫筆和填滿色彩繪製矩形。draw_circle– 使用目前畫筆和填滿色彩繪製一個圓形。show_file– 繪製上傳的影像。
觸控-偵測並回應螢幕按壓。
pressing– 返回螢幕目前是否被按下。x_position– 傳回上次畫面所按下的 x 座標。y_position– 傳回最後一次畫面按下的 y 座標。
回調函數-在螢幕被按下或釋放時運行的函數。
遊標列印#
print#
print 在控制器畫面上的目前遊標位置和字型處列印文字、數字或變數值。
用法:
controller.screen.print(text)
參數 |
描述 |
|---|---|
|
若要列印在螢幕上的文字、數字或變數值。 |
|
可選。列印文字之間的分隔符號。預設值為 |
|
可選。附加到最後列印文字之後的字串。預設值為 |
|
可選。浮點數列印時保留的小數位數,以整數表示。預設值為 |
# Display a message at the starting cursor position.
controller.screen.print("Time to fly!")

# Display the first 2 decimals of pi
controller.screen.print(3.1415, precision = 2)

set_cursor#
set_cursor 将光标移动到遥控器屏幕上指定的行和列。下一次调用 print 时,将从该位置开始打印。行间距和列间距会根据所选字体进行调整。使用默认的中等大小等宽字体时,屏幕最多可清晰显示 18 行和 53 列。超出此范围的文本可能会被截断或难以阅读。
用法:
controller.screen.set_cursor(row, col)
參數 |
描述 |
|---|---|
|
要將遊標移到的行。 |
|
要將遊標移到的列。 |
# Display text starting at Row 7 Column 25.
controller.screen.set_cursor(row=7, col=25)
controller.screen.print("Row 7, Column 25")

next_row#
next_row 將遊標移到控制器畫面上下一行的第 1 列。
用法:
controller.screen.next_row()
參數 |
描述 |
|---|---|
此方法沒有參數。 |
# Display two lines of text.
controller.screen.print("Line 1")
controller.screen.next_row()
controller.screen.print("Line 2")

clear_row#
clear_row 清除控制器畫面上的一行文字。
用法:controller.screen.clear_row(row, color)
範圍 |
描述 |
|---|---|
|
可選。要清除的行。預設值為目前遊標所在行。 |
|
可选。应用于已清除行的颜色。如果未指定颜色,则使用
|
# Clear only the second row of text
controller.screen.print("This text stays")
controller.screen.next_row()
controller.screen.print("This text disappears")
wait(3, SECONDS)
controller.screen.clear_row(2)
# Turn the 5th row green.
controller.screen.clear_row(5, GREEN)
get_row#
get_row 傳回要列印文字的目前行,以整數形式傳回。
用法:
controller.screen.get_row()
參數 |
描述 |
|---|---|
此方法沒有參數。 |
# Display the cursor's current row.
controller.screen.set_cursor(row=7, col=25)
controller.screen.print(controller.screen.get_row())

get_column#
get_column 傳回要列印文字的目前列,以整數形式傳回。
用法:
controller.screen.get_column()
參數 |
描述 |
|---|---|
此方法沒有參數。 |
# Display the cursor's current column.
controller.screen.set_cursor(row=7, col=25)
controller.screen.print(controller.screen.get_column())

XY列印#
print_at#
print_at 使用当前的 字体 和 原点,在遥控器屏幕上指定的像素坐标 (x, y) 位置打印文本、数字或变量值。x 设置文本起始位置距离屏幕左侧的距离,y 设置文字底部所在的位置。此方法不受当前光标位置的影响。
用法:
controller.screen.print_at(text, x, y)
範圍 |
描述 |
|---|---|
|
若要列印在螢幕上的文字、數字或變數值。 |
|
文字的水平位置,以 0 到 640 像素之間的整數表示。 0 為左;640 為右。 |
|
文字的垂直位置,以 0 到 480 像素之間的整數表示。 0 表示頂部;480 表示底部。 |
# Display a message in the middle of the screen.
controller.screen.print_at("Hello, drone!", x=320, y=240)

set_origin#
set_origin 設定控制器螢幕上用於繪圖和基於座標的列印的原點 (0, 0)。預設情況下,原點位於螢幕左上角。此方法可以將原點重設為螢幕座標系中的其他位置 (x, y)。
用法:
controller.screen.set_origin(x, y)
範圍 |
描述 |
|---|---|
|
設定為原點的新 x 座標,以 0 到 640 之間的整數形式給出。 |
|
設定為原點的新 y 座標,以 0 到 480 之間的整數形式給出。 |
# Set the origin to the center of the screen.
controller.screen.set_origin(x=320, y=240)
# Draw a rectangle at the new origin.
controller.screen.draw_rectangle(x=0, y=0, width=80, height=40)

修改器#
clear_screen#
clear_screen 清除控制器螢幕上的所有繪圖、文字和影像。
用法:
controller.screen.clear_screen(row, col, color)
範圍 |
描述 |
|---|---|
|
可選。清除螢幕後遊標移動到的行。預設值為 1。 |
|
可選。清除螢幕後遊標移動到的列。預設值為 1。 |
|
可选。设置屏幕颜色。如果包含
|
# Draw a circle, and clear it after 2 seconds.
controller.screen.draw_circle(x=320, y=240, radius=80)
wait(2, SECONDS)
controller.screen.clear_screen()
# Print two messages in the same spot
controller.screen.print("Hello!")
wait(3,SECONDS)
controller.screen.clear_screen(row=1, col=1)
controller.screen.print("Goodbye!")
# Set the background color of the screen to red.
controller.screen.clear_screen(color=RED)
wait_for_render#
wait_for_render 將螢幕繪製切換為稍後渲染行為。此方法運行後,後續螢幕命令將不會顯示在控制器螢幕上,直到下一個 wait_for_render 方法運行。
注意:在繪製一組文字、形狀或圖像之前使用此方法,然後再次使用此方法可一次渲染完整的更新。
用法:
controller.screen.wait_for_render()
範圍 |
描述 |
|---|---|
此方法沒有參數。 |
# Display text and images when button 7 is pressed.
controller.screen.wait_for_render()
controller.screen.print("Screen rendered!")
controller.screen.draw_circle(x=320, y=240, radius=80)
while not controller.button7.pressing():
wait(5, MSEC)
controller.screen.wait_for_render()

set_font#
set_font 設定用於在控制器螢幕上顯示文字的字體。此字體將套用於此方法運行後列印的所有文字。專案開始時的預設字體為 MONO20。
用法:
controller.screen.set_font(fontname)
範圍 |
描述 |
|---|---|
|
将字体设置为以下选项之一:
|
|
|
|
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
# Display text using a larger font.
controller.screen.set_font(MONO40)
controller.screen.print("VEX")

set_pen_width#
set_pen_width 設定繪製線條和形狀輪廓的粗細。
用法:
controller.screen.set_pen_width(width)
範圍 |
描述 |
|---|---|
|
筆的寬度,以像素為單位的整數表示。 |
# Draw a rectangle with a pen width of 10.
controller.screen.set_pen_width(10)
controller.screen.draw_rectangle(x=200, y=200, width=200, height=80)

set_pen_color#
set_pen_color 設定文字、像素、線條和形狀輪廓的顏色。
用法:
controller.screen.set_pen_color(color)
範圍 |
描述 |
|---|---|
|
可选。设置画笔和字体颜色。可选颜色包括:
|
# Draw a rectangle with an orange outline.
controller.screen.set_pen_color(ORANGE)
controller.screen.draw_rectangle(x=200, y=150, width=240, height=150)
controller.screen.set_pen_color(WHITE)
controller.screen.print_at("VEXcode AIR", x=250, y=220)

set_text_fill_color#
set_text_fill_color 設定列印文字的填滿顏色。預設的文字填滿顏色為透明。
用法:
controller.screen.set_text_fill_color(color)
範圍 |
描述 |
|---|---|
|
可选。设置文本填充颜色。可选颜色包括:
|
# Display two colors behind text
controller.screen.set_text_fill_color(RED)
controller.screen.print("Red")
controller.screen.next_row()
controller.screen.set_text_fill_color(BLUE)
controller.screen.print("Blue")

set_fill_color#
set_fill_color 設定繪製形狀時所使用的填滿顏色。預設填滿顏色為黑色。
用法:
controller.screen.set_fill_color(color)
範圍 |
描述 |
|---|---|
|
可选。设置填充颜色。可选颜色包括:
|
# Draw an orange-filled rectangle with text on top
controller.screen.set_fill_color(ORANGE)
controller.screen.draw_rectangle(x=200, y=150, width=250, height=150)
controller.screen.print_at("VEXcode AIR", x=250, y=220)

畫#
draw_pixel#
draw_pixel 使用目前 筆顏色 在選取的 x 和 y 座標處繪製一個像素。
用法:
controller.screen.draw_pixel(x, y)
範圍 |
描述 |
|---|---|
|
將繪製像素的 x 座標,以 0 到 640 之間的整數表示。 |
|
將繪製像素的 y 座標,以 0 到 480 之間的整數表示。 |
# Draw five pixels in a diagonal.
controller.screen.draw_pixel(x=360, y=200)
controller.screen.draw_pixel(x=340, y=220)
controller.screen.draw_pixel(x=320, y=240)
controller.screen.draw_pixel(x=300, y=260)
controller.screen.draw_pixel(x=280, y=280)
![]()
draw_line#
draw_line 使用目前的 筆寬 和 筆顏色 從第一個螢幕座標 (x1, y1) 到第二個螢幕座標 (x2, y2) 繪製一條線。
x 和 y 座標使用預設原點 (0, 0),除非使用 set_origin 設定了不同的原點。
用法:
controller.screen.draw_line(x1, y1, x2, y2)
範圍 |
描述 |
|---|---|
|
線的起始 x 座標,以 0 到 640 之間的整數表示。 |
|
線的起始 y 座標,以 0 到 480 之間的整數表示。 |
|
線的結束 x 座標,以 0 到 640 之間的整數表示。 |
|
線的結束 y 座標,以 0 到 480 之間的整數表示。 |
# Make an X across the screen.
controller.screen.draw_line(x1=0, y1=0, x2=640, y2=480)
controller.screen.set_pen_color(CYAN)
controller.screen.draw_line(x1=0, y1=480, x2=640, y2=0)

draw_rectangle#
draw_rectangle 使用目前的 筆寬、筆顏色 和 填滿顏色 繪製矩形,除非使用了可選的 color 參數。
x 和 y 座標使用預設原點 (0, 0),除非使用 set_origin 設定了不同的原點。
用法:
controller.screen.draw_rectangle(x, y, width, height, color)
範圍 |
描述 |
|---|---|
|
矩形左上角的 x 座標,以 0 到 640 之間的整數表示。 |
|
矩形左上角的 y 座標,以 0 到 480 之間的整數表示。 |
|
矩形的寬度,以 0 到 640 之間的整數表示。 |
|
矩形的高度,以 0 到 480 之間的整數表示。 |
|
可选。设置矩形的填充颜色。可选颜色包括:
|
# Draw two rectangles on the screen.
controller.screen.draw_rectangle(x=0, y=0, width=620, height=460)
controller.screen.draw_rectangle(x=200, y=180, width=200, height=100, color=GREEN)

draw_circle#
draw_circle 使用目前的 筆寬、筆顏色 和 填滿顏色 繪製一個圓,除非使用了可選的 color 參數。
x 和 y 座標使用預設原點 (0, 0),除非使用 set_origin 設定了不同的原點。
用法:
controller.screen.draw_circle(x, y, radius, color)
範圍 |
描述 |
|---|---|
|
圓心的 x 座標,以 0 到 640 之間的整數表示。 |
|
圓心的 y 座標,以 0 到 480 之間的整數表示。 |
|
圓的半徑,以 0 到 480 像素之間的整數表示。 |
|
可选。设置圆形的填充颜色。可选颜色包括:
|
# Draw one green circle on the screen.
controller.screen.set_fill_color(GREEN)
controller.screen.draw_circle(x=320, y=240, radius=200, color=GREEN)

show_file#
show_file 在控制器螢幕上的 x 和 y 位置繪製自訂上傳的影像,並可選擇縮放、旋轉和 alpha 透明度。
用法:
controller.screen.show_file(file, x, y, scale_width, scale_height, rotation, use_alpha)
範圍 |
描述 |
|---|---|
|
要使用的用戶上傳的圖片。如果在控制台中編輯了圖片名稱,選項將會變更。 |
|
影像的水平偏移量,以像素為單位的整數形式指定。正值表示向右移動;負值表示向左移動。 |
|
影像的垂直偏移量,以像素為單位的整數形式指定。正值表示向下移動;負值表示向上移動。 |
|
可選。要縮放的影像寬度,以像素為單位的整數形式給出。 |
|
可選。要縮放的影像高度,以像素為單位的整數形式給出。 |
|
可選參數。影像旋轉的角度,取值範圍為 0 到 359 之間的整數。 |
|
可選。是否使用影像的 alpha 通道實現透明度。選項包括: |
# Display uploaded Image 1 in the top left corner.
controller.screen.show_file(IMAGE1, x=0, y=0)
# Display uploaded Image 1 scaled to the full screen.
controller.screen.show_file(IMAGE1, x=65, y=0, scale_width=640, scale_height=480)
# Display uploaded Image 1 with rotation of 180 degrees.
controller.screen.show_file(IMAGE1, x=-65, y=0, rotation=180)
# Upload an image with a transparent background.
# Display uploaded Image 1 with alpha channel enabled.
controller.screen.show_file(IMAGE1, x=0, y=0, use_alpha=True)
觸碰#
pressing#
pressing 返回一個布林值,指示機器人的螢幕目前是否被按下。
True– 螢幕被按下。False– 螢幕未按下。
用法:
controller.screen.pressing()
參數 |
描述 |
|---|---|
此方法沒有參數。 |
# Play a sound when the screen is pressed.
while True:
if controller.screen.pressing():
controller.sound.play_note("C5", 500)
wait(5, MSEC)
# Display different messages after the screen is pressed
while not controller.screen.pressing():
wait(5, MSEC)
controller.screen.print("First message!")
controller.screen.next_row()
# Lift finger to press the screen again
while controller.screen.pressing():
wait(5, MSEC)
while not controller.screen.pressing():
wait(5, MSEC)
controller.screen.print("Second message!")
controller.screen.next_row()
x_position#
x_position 返回螢幕上次被按下位置的 x 座標(以像素為單位),取值範圍為 0(左)到 640(右)。

用法:
controller.screen.x_position()
參數 |
描述 |
|---|---|
此方法沒有參數。 |
# Play a high or low note based on where screen is pressed.
while True:
if controller.screen.pressing():
if controller.screen.x_position() > 320:
controller.sound.play_note("C6", 500)
else:
controller.sound.play_note("C4", 500)
wait(5, MSEC)
y_position#
y_position 返回螢幕上次被按下位置的 y 座標(以像素為單位),取值範圍為 0(頂部)到 480(底部)的整數。

用法:
controller.screen.y_position()
# Play a high or low note based on where screen is pressed.
while True:
if controller.screen.pressing():
if controller.screen.y_position() > 240:
controller.sound.play_note("C6", 500)
else:
controller.sound.play_note("C4", 500)
wait(5, MSEC)
回呼函數#
pressed#
pressed 註冊一個在控制器畫面被按下時要呼叫的函數。
用法:
controller.screen.pressed(callback, arg)
參數 |
描述 |
|---|---|
|
一個預先定義好的函數,當按下控制器螢幕時執行。 |
|
# Play a sound when the screen is pressed.
def play_sound():
controller.sound.play_note("C5", 500)
controller.screen.pressed(play_sound)
released#
released 註冊一個在控制器畫面釋放時要呼叫的函數。
用法:
controller.screen.released(callback, arg)
參數 |
描述 |
|---|---|
|
先前定義的一個函數,當控制器螢幕釋放時執行。 |
|
# Play a sound when the screen is pressed.
def play_sound():
controller.sound.play_note("C5", 500)
controller.screen.released(play_sound)













