屏幕#

简介#

屏幕方法控制 VEX AIM 编码机器人如何在屏幕上显示文本、数字和图形,以及如何响应屏幕按键。

以下是所有方法的列表:

触摸——检测并响应屏幕按压。

  • [pressing] (#pressing) –返回当前是否正在按下屏幕。

  • [x_position] (#x-position) –返回最后一次屏幕按压的x坐标。

  • [y_position] (#y-position) –返回上一次屏幕按下的y坐标。

光标打印——使用行/列系统显示文本。

  • [print] (#print) –在当前光标位置打印文本、数字或变量值。

  • [set_cursor] (#set-cursor) –将光标移动到特定的行和列。

  • [next_row] (#next-row) –将光标移动到下一行的第1列。

  • [clear_row] (#clear-row) –清除一行文本。

  • [get_row] (#get-row) –返回当前光标行。

  • [get_column] (#get-column) –返回当前光标列。

XY 打印 – 在特定的屏幕坐标处显示文本。

  • [print_at] (#print-at) –在特定的x和y坐标处打印文本、数字或变量值。

  • [set_origin] (#set-origin) –设置用于基于坐标的打印和绘图的原点。

Mutators – Clear the screen or update visual settings.

  • [clear_screen] (#clear-screen) –清除所有图形和文本的屏幕。

  • [set_font] (#set-font) –设置打印文本的字体。

  • [set_pen_width] (#set-pen-width) –设置绘制的线条和形状轮廓的粗细。

  • [set_pen_color] (#set-pen-color) –设置文本、像素、线条和形状轮廓的颜色。

  • [set_fill_color] (#set-fill-color) –设置绘制形状和打印文本背景的填充颜色。

绘图——在屏幕上添加图形和图像。

  • [draw_pixel] (#draw-pixel) –在特定的x和y位置绘制像素。

  • [draw_line] (#draw-line) –在两点之间绘制一条线。

  • [draw_rectangle] (#draw-rectangle) –绘制矩形。

  • [draw_circle] (#draw-circle) –画一个圆圈。

  • [show_file] (#show-file) –显示上传的图像。

  • [set_clip_region] (#set-clip-region) –限制图形和文本的显示位置。

Callbacks – Run functions when the screen is pressed or released.

  • [pressed] (#pressed) –注册在按下屏幕时要调用的函数。

  • [released] (#released) –注册要在释放屏幕时调用的函数。

触碰#

pressing#

pressing 返回一个布尔值,指示当前是否正在按下机器人的屏幕。

  • True –正在按下屏幕。

  • False –未按下屏幕。

Usage:
robot.screen.pressing()

参数

描述

该方法没有参数。

# Turn LEDs white only while the screen is pressed.
while True:
    if robot.screen.pressing():
        robot.led.on(ALL_LEDS, WHITE)
    else:
        robot.led.off(ALL_LEDS)

    wait(50, MSEC)

# Display different messages after the screen is pressed
while not robot.screen.pressing():
    wait(5, MSEC)
robot.screen.print("First message!")
robot.screen.next_row()
# Lift finger to press the screen again
while robot.screen.pressing():
    wait(5, MSEC)
while not robot.screen.pressing():
    wait(5, MSEC)
robot.screen.print("Second message!")
robot.screen.next_row()

x_position#

x_position以从0 (左)到240 (右)的整数形式返回最后一次屏幕按压的x坐标。

用法:
robot.screen.x_position()

参数

描述

该方法没有参数。

# Display the x-coordinate of where
# the screen is pressed
while True:
    if robot.screen.pressing():
        robot.screen.clear_screen()
        robot.screen.print(robot.screen.x_position())
    wait (50, MSEC)

y_position#

y_position 返回上次屏幕按的 y坐标 ,从0 (顶部) 到240 (底部)。

Usage:
robot.screen.y_position()

参数

描述

该方法没有参数。

# Display the y-coordinate of where
# the screen is pressed
while True:
    if robot.screen.pressing():
        robot.screen.clear_screen()
        robot.screen.print(robot.screen.y_position())
    wait (50, MSEC)

光标打印#

print#

print 在机器人屏幕上的当前[光标位置] (#set-cursor)和[字体] (#set-font)处打印文本、数字或变量值。

用法:
robot.screen.print(text)

参数

描述

text

要在屏幕上显示的文本、数字或变量值。

# Display a message at the starting cursor position
robot.screen.print("Hello, Robot!")

机器人屏幕的截图,顶部用白色字体打印着“你好,机器人!”。

set_cursor#

set_cursor 将文本光标移动到机器人屏幕上的指定行和列。下一次 print 调用将从该位置开始打印。能容纳的行数和列数取决于所选字体。使用默认的等宽中号字体时,屏幕最多可以清晰显示 8 行 13 列。超出此范围的文本可能会被截断或难以阅读。

等宽字体中的字符宽度均相同,因此文本布局一致。相比之下,比例字体的字符宽度各异,有些字母会占据更多空间。然而,无论使用哪种类型,set_cursor 都会根据行和列的尺寸来定位光标,而非字体样式。字体大小可以通过 set_font 进行调整。

用法:
robot.screen.set_cursor(row, column)

参数

描述

row

要将光标移动到的行。

column

要将光标移动到的列。

# Display text starting at Row 3 Column 2
robot.screen.set_cursor(3, 2)
robot.screen.print("Row 3, Column 2")

机器人屏幕的截图,屏幕中心上方用白色字体打印着“第 3 行,第 2 列”,从第 3 行第 2 列开始。

next_row#

next_row 将光标移动到机器人屏幕上下一行的第1列。

用法:
robot.screen.next_row()

参数

描述

该方法没有参数。

# Display two lines of text
robot.screen.print("Line 1")
robot.screen.next_row()
robot.screen.print("Line 2")

机器人屏幕的截图,文字为白色。屏幕顶部左对齐显示“第 1 行”,下方紧邻“第 2 行”。

clear_row#

clear_row 清除机器人屏幕上的一行文本。

用法:
robot.screen.clear_row(row, color)

参数

描述

row

可选。要清除的行号,以整数形式表示。默认值为当前光标所在行。

color

可选。应用于被清除行的颜色。选项包括:

  • BLACK
  • BLUE
  • CYAN
  • GREEN
  • ORANGE
  • PURPLE
  • RED
  • TRANSPARENT
  • WHITE
  • YELLOW
你也可以指定一种自定义颜色

# Display text on two rows
robot.screen.print("This text stays")
robot.screen.next_row()
robot.screen.print("This text disappears")

# Wait 3 seconds before clearing only the second row
wait(3, SECONDS)
robot.screen.clear_row()

# Turn the 5th row green
robot.screen.clear_row(5, GREEN)

机器人屏幕的截图,屏幕中央从左到右有一条绿色横条。

get_row#

get_row 返回文本将打印为整数的当前行。

用法:
robot.screen.get_row()

参数

描述

该方法没有参数。

# Set cursor to (3,2) and print row number
robot.screen.set_cursor(3, 2)
robot.screen.print(robot.screen.get_row())

机器人屏幕的SA截图,左上象限中心附近,第3行第2列,印有白色数字3。

get_column#

get_column 返回文本将打印为整数的当前列。

用法:
robot.screen.get_column()

参数

描述

该方法没有参数。

# Set cursor to (3,2) and print column number
robot.screen.set_cursor(3, 2)
robot.screen.print(robot.screen.get_column())

机器人屏幕的截图,左上象限中心附近,第 3 行第 2 列,印有一个白色的数字 2。

XY打印#

set_origin#

set_origin 设置用于在机器人屏幕上绘制和基于坐标的打印的原点。默认情况下,原点是屏幕的左上角。

Usage:
robot.screen.set_origin(x, y)

参数

描述

x

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

y

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

# Set the origin to the center of the screen
robot.screen.set_origin(120, 120)

# Draw a rectangle at the new origin
robot.screen.draw_rectangle(0, 0, 80, 40)

机器人屏幕的截图,左下角绘制了一个白色矩形。矩形的左上角是屏幕中心。

Mutator#

clear_screen#

clear_screen 将清除机器人屏幕上的所有绘图和文本。默认情况下,它还将光标位置重置为第1行第1列。

Usage:
robot.screen.clear_screen(color)

参数

描述

color

可选。设置屏幕颜色。选项包括:

  • BLACK
  • BLUE
  • CYAN
  • GREEN
  • ORANGE
  • PURPLE
  • RED
  • TRANSPARENT
  • WHITE
  • YELLOW
你也可以指定一种自定义颜色

# Draw a circle, and clear it after 2 seconds
robot.screen.draw_circle(120, 120, 60)
wait(2, SECONDS)
robot.screen.clear_screen()

# Set the background color of the screen to red
robot.screen.clear_screen(RED)

机器人屏幕的截图,显示整个屏幕呈鲜红色。

set_font#

set_font 设置用于在机器人屏幕上显示文本的字体。此字体将适用于所有使用[print] (#print)或[print_at] (#print-at)打印的文本。项目开始时的默认字体是 MONO24

Usage:
robot.screen.set_font(fontname)

参数

描述

fontname

将字体设置为以下字体之一

  • MONO12
  • MONO15
  • MONO20
  • MONO24
  • MONO30
  • MONO40
  • MONO60
  • PROP20
  • PROP30
  • PROP40
  • PROP60
PROP60这些选项如下所示。

! [机器人屏幕的屏幕截图,显示以Mono 12字体打印的数字和字母样本。字母表占据1行文本。底部横跨26个字符, 15行。] (/_static/img/fonts/mono12.png)
MONO12

! [与前一个Mono 15字体相同的图像。字母A-T占据1行文本。底部横跨20个字符, 12行。] (/_static/img/fonts/mono15.png)
MONO15

! [与上一张相同的图像,使用Mono 20 字体。字母A-P占据1行文本。底部横跨16个字符和9行。] (/_static/img/fonts/mono20.png)
MONO20

! [与上一张相同的图像,采用Mono 24 字体。字母A-M占据1行文本。底部横跨13个字符, 8行。] (/_static/img/fonts/mono24.png)
MONO24

! [与上一张图片相同,使用Mono 30 字体。字母A-K占据1行文本。底部横跨11个字符和6行。] (/_static/img/fonts/mono30.png)
MONO30

! [与上一张图片相同,使用Mono 40 字体。字母A-H占据1行文本。底部横跨8个字符, 5行。] (/_static/img/fonts/mono40.png)
MONO40

! [与上一张图片相同,数字以Mono 60 字体打印。数字1-6占据1行文本。底部显示3行。] (/_static/img/fonts/mono60.png)
MONO60

! [与上一张相同的图片,使用Prop 20 字体。字母A-s占据1行文本。底部横跨20个字符和9行。] (/_static/img/fonts/prop20.png)
PROP20

! [与上一张图片相同,使用Prop 30 字体。字母A-M占据1行文本。底部横跨15个字符和6行。] (/_static/img/fonts/prop30.png)
PROP30

! [与上一张图片相同,使用Prop 40 字体。字母A-J占据1行文本。底部横跨11个字符, 5行。] (/_static/img/fonts/prop40.png)
PROP40

! [与上一张图片相同,数字以Prop 60 字体打印。数字1-7占据1行文本。底部为7乘3。] (/_static/img/fonts/prop60.png)
PROP60

# Display text using a larger font
robot.screen.set_font(MONO40)
robot.screen.print("VEX")

机器人屏幕的截图显示,左上角以 Mono 40 字体显示 VEX。

set_pen_width#

set_pen_width 设置绘制的线条和形状轮廓的厚度。

Usage:
robot.screen.set_pen_width(width)

参数

描述

width

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

# Draw a rectangle with a pen width of 10
robot.screen.set_pen_width(10)
robot.screen.draw_rectangle(50, 50, 130, 60)

机器人屏幕的截图,上半部分画着一个粗白矩形。

set_pen_color#

set_pen_color 设置文本、像素、线条和形状轮廓的颜色。

Usage:
robot.screen.set_pen_color(color)

参数

描述

color

可选。要使用的笔和字体颜色。选项包括:

  • BLACK
  • BLUE
  • CYAN
  • GREEN
  • ORANGE
  • PURPLE
  • RED
  • TRANSPARENT
  • WHITE
  • YELLOW
你也可以指定一种自定义颜色

# Draw a rectangle with a red pen
robot.screen.set_pen_color(RED)
robot.screen.draw_rectangle(50, 50, 130, 60)

机器人屏幕的截图,上半部分画着一个细边框的红色矩形。

set_fill_color#

set_fill_color 设置绘制形状和打印文本使用背景时使用的填充颜色。

Usage:
robot.screen.set_fill_color(color)

参数

描述

color

可选。要使用的填充色。选项包括:

  • BLACK
  • BLUE
  • CYAN
  • GREEN
  • ORANGE
  • PURPLE
  • RED
  • TRANSPARENT
  • WHITE
  • YELLOW
你也可以指定一种自定义颜色

# Draw two orange rectangles
robot.screen.set_fill_color(ORANGE)
robot.screen.draw_rectangle(50, 50, 100, 60)
robot.screen.draw_rectangle(50, 130, 100, 60)

机器人屏幕的截图,屏幕左侧中央有两个平行的橙色矩形,带有细细的白色轮廓。

# Display text with a purple background
robot.screen.set_fill_color(PURPLE)
robot.screen.print("Highlight")

机器人屏幕的截图显示“高亮显示”,白色文字周围有紫色高亮圈,从左上角开始。

绘制#

draw_pixel#

draw_pixel 使用当前[笔颜色] (#set-pen-color)在所选x和y坐标处绘制一个像素。

Usage:
robot.screen.draw_pixel(x, y)

参数

描述

x

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

y

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

# Draw a pixel at the center of the screen
robot.screen.draw_pixel(120, 120)

机器人屏幕的截图,中心有一个白色像素。

draw_line#

draw_line 使用当前的笔宽笔颜色,从第一个屏幕坐标 (x1, y1) 到第二个屏幕坐标 (x2, y2) 绘制一条直线。

除非已使用 set_origin 设置了不同的原点,否则 x 和 y 坐标使用默认原点 (0, 0)。

用法:
robot.screen.draw_line(x1, y1, x2, y2)

参数

描述

x1

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

y1

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

x2

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

y2

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

# Draw a line from the top left to bottom right of the screen
robot.screen.draw_line(0, 0, 240, 240)

机器人屏幕的截图,从左上角到右下角画了一条细细的白色对角线。

draw_rectangle#

draw_rectangle 绘制一个矩形,其左上角位于选定的 (x, y) 坐标,大小由给定的宽度和高度决定,所有单位均为像素。矩形的轮廓使用当前的笔宽笔颜色绘制。内部使用当前的填充色填充,除非使用了可选的 color 参数。

除非已使用 set_origin 设置了不同的原点,否则 x 和 y 坐标使用默认原点 (0, 0)。

用法:
robot.screen.draw_rectangle(x, y, width, height, color)

参数

描述

x

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

y

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

width

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

height

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

color

可选。矩形的填充色。选项包括:

  • BLACK
  • BLUE
  • CYAN
  • GREEN
  • ORANGE
  • PURPLE
  • RED
  • TRANSPARENT
  • WHITE
  • YELLOW
你也可以指定一种自定义颜色

# Draw a red rectangle on the screen
robot.screen.draw_rectangle(50, 50, 130, 60, RED)

机器人屏幕的截图,屏幕上半部分居中显示一个亮红色矩形,矩形边缘有一条细细的白色边框。

draw_circle#

draw_circle 绘制一个圆形,其圆心位于选定的 (x, y) 坐标,大小由给定的半径决定,所有单位均为像素。圆形的轮廓使用当前的笔宽笔颜色绘制。内部使用当前的填充色填充,除非使用了可选的 color 参数。

除非已使用 set_origin 设置了不同的原点,否则 x 和 y 坐标使用默认原点 (0, 0)。

Usage:
robot.screen.draw_circle(x, y, radius, color)

参数

描述

x

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

y

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

radius

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

color

可选。圆形的填充色。选项包括:

  • BLACK
  • BLUE
  • CYAN
  • GREEN
  • ORANGE
  • PURPLE
  • RED
  • TRANSPARENT
  • WHITE
  • YELLOW
你也可以指定一种自定义颜色

# Draw a green circle on the screen
robot.screen.draw_circle(120, 120, 40, GREEN)

机器人屏幕的截图,中心有一个亮绿色圆圈,圆圈边缘有细细的白色边框。

show_file#

show_file 在机器人屏幕上显示自定义上传的图像,其位置通过 xycenter 参数基于图像的参考点来设置。

Usage:
robot.screen.show_file(file, x, y, center)

参数

描述

file

要使用的自定义图像,从 IMAGE1IMAGE10。图像编号与AIM控制面板中显示的编号相匹配。

x

图像的水平偏移量,以像素为单位的整数形式指定。正值表示向右移动;负值表示向左移动。

y

图像的垂直偏移量,以像素为单位的整数形式指定。正值表示向下移动;负值表示向上移动。

center

可选。如果 center=True,则使用图像的中心坐标(120, 120)放置图像。默认情况下(center=False) ,使用图像的左上角并相对于当前[原点] (#set-origin )定位。

# Display uploaded Image 1 in the top left corner
robot.screen.show_file(IMAGE1, 0, 0)

# Show the same image on both sides of the screen
# Image size is 120 x 120
robot.screen.show_file(IMAGE1, 65, 0, center=True)
robot.screen.show_file(IMAGE1, -65, 0, center=True)

set_clip_region#

set_clip_region 在屏幕上定义了一个矩形区域,所有图形和文本都将被限制。此区域以外的任何内容都不会显示。

用法:
robot.screen.set_clip_region(x, y, width, height)

参数

描述

x

剪辑区域左上角的x坐标,以0到240之间的小数(float)给出。

y

剪辑区域左上角的y坐标,以0到240之间的小数(float) 给出。

width

以像素为单位,裁剪区域的宽度,给定为从 0 到 240 的小数(float)

height

以像素为单位,剪辑区域的高度,以0到240之间的小数(float)表示。

# Restrict text and drawings to a specific region
robot.screen.set_clip_region(0, 0, 120, 120)
robot.screen.draw_rectangle(60, 60, 100, 100, RED)
robot.screen.print_at("Cut off!", x=60, y=60)

机器人屏幕的截图,显示了适合裁剪区域的文本和图像。在左上象限,白色文字显示“Cut o”,其正下方有一个红色方块。

回调函数#

pressed#

pressed 注册在按下机器人屏幕时要调用的方法。

Usage:
robot.screen.pressed(callback, arg)

参数

描述

callback

一个预先定义好的函数,当按下控制器屏幕时执行。

arg

可选。包含要传递给回调函数的参数的元组。有关更多信息,请参阅使用带参数的函数

# Set the LEDs to green when the screen is pressed.
def screen_touched():
    robot.led.on(ALL_LEDS, GREEN)

robot.screen.pressed(screen_touched)

released#

released 注册在释放机器人屏幕时要调用的方法。

Usage:
robot.screen.released(callback, arg)

参数

描述

callback

一个预先定义好的函数,当按下控制器屏幕时执行。

arg

可选。包含要传递给回调函数的参数的元组。有关更多信息,请参阅使用带参数的函数

# Set the LEDs to blue when the screen is released.
def screen_released():
    robot.led.on(ALL_LEDS, BLUE)

robot.screen.released(screen_released)