pygame.cursors
pygame module for cursor resources
create binary cursor data from simple strings
load cursor data from an XBM file
pygame object representing a cursor

PYGAME提供了对系统硬件光标的控制。PYGAME支持黑白光标(位图光标),以及系统变量光标和颜色光标。您可以使用内部函数来控制光标 pygame.mousepygame module to work with the mouse

此游标模块包含用于加载和解码各种游标格式的函数。它们允许您轻松地将游标存储在外部文件中,或直接存储为编码的Python字符串。

该模块包括几个标准游标。这个 pygame.mouse.set_cursor()set the mouse cursor to a new cursor 函数有几个参数。所有这些参数都存储在一个元组中,您可以这样调用:

>>> pygame.mouse.set_cursor(*pygame.cursors.arrow)

可以将以下变量传递给 pygame.mouse.set_cursor 功能:

  • pygame.cursors.arrow

  • pygame.cursors.diamond

  • pygame.cursors.broken_x

  • pygame.cursors.tri_left

  • pygame.cursors.tri_right

该模块还包含一些作为格式化字符串的游标。你需要把这些传给 pygame.cursors.compile() 函数,然后才能使用它们。示例调用如下所示:

>>> cursor = pygame.cursors.compile(pygame.cursors.textmarker_strings)
>>> pygame.mouse.set_cursor((8, 16), (0, 0), *cursor)

可以使用以下命令将以下字符串转换为光标位图 pygame.cursors.compile()

  • pygame.cursors.thickarrow_strings

  • pygame.cursors.sizer_x_strings

  • pygame.cursors.sizer_y_strings

  • pygame.cursors.sizer_xy_strings

  • pygame.cursor.textmarker_strings

pygame.cursors.compile()
create binary cursor data from simple strings
compile(strings, black='X', white='.', xor='o') -> data, mask

字符串序列可用于为系统游标创建二进制游标数据。这将以两个元组的形式返回二进制数据。这些参数可以分别作为 pygame.mouse.set_cursor()set the mouse cursor to a new cursor 功能。

如果您正在创建自己的光标字符串,则可以使用表示黑白像素的任何值。有些系统允许您为系统颜色设置特殊的切换颜色,这也称为XOR颜色。如果系统不支持XOR光标,则该颜色将直接为黑色。

高度必须可被8整除。字符串的宽度必须全部相等且可被8整除。如果不满足这两个条件, ValueError 都被养大了。一组示例游标字符串如下所示

thickarrow_strings = (               #sized 24x24
  "XX                      ",
  "XXX                     ",
  "XXXX                    ",
  "XX.XX                   ",
  "XX..XX                  ",
  "XX...XX                 ",
  "XX....XX                ",
  "XX.....XX               ",
  "XX......XX              ",
  "XX.......XX             ",
  "XX........XX            ",
  "XX........XXX           ",
  "XX......XXXXX           ",
  "XX.XXX..XX              ",
  "XXXX XX..XX             ",
  "XX   XX..XX             ",
  "     XX..XX             ",
  "      XX..XX            ",
  "      XX..XX            ",
  "       XXXX             ",
  "       XX               ",
  "                        ",
  "                        ",
  "                        ")
pygame.cursors.load_xbm()
load cursor data from an XBM file
load_xbm(cursorfile) -> cursor_args
load_xbm(cursorfile, maskfile) -> cursor_args

这将为一个简单的子集加载游标 XBM 文件。 XBM 在UNIX系统上,文件传统上用于存储光标,它们是一种用于表示简单图像的ASCII格式。

有时,黑色和白色的颜色值会被分为两个单独的 XBM 档案。您可以传递第二个maskfile参数以将两个图像加载到单个游标中。

Cursorfile和maskfile参数可以是文件名,也可以是使用READLINS方法的类文件对象。

返回值CURSOR_ARGS可以直接传递给 pygame.mouse.set_cursor() 功能。

pygame.cursors.Cursor
pygame object representing a cursor
Cursor(size, hotspot, xormasks, andmasks) -> Cursor
Cursor(hotspot, surface) -> Cursor
Cursor(constant) -> Cursor
Cursor(Cursor) -> Cursor
Cursor() -> Cursor
Gets the cursor type
Gets the cursor data

在PYGAME 2中,你可以创建三种类型的光标来给你的游戏增添一点额外的光泽。有 位图 类型游标,它存在于pyGame 1.x中,从字符串编译或从xbm文件加载。然后还有 系统 键入光标,您可以在其中选择一个预设,该预设将传达相同的含义,但在不同的操作系统上看起来是原生的。最后,您可以创建一个 颜色 光标,它显示一个电子游戏表面作为光标。

创建系统游标

从该列表中选择一个常量,将其传递到 pygame.cursors.Cursor(constant) ,你可以走了。请注意,并非所有系统都支持每个系统游标,您可能会得到一个替代游标。例如,在MacOS上,WAIT/WAITARROW应显示为箭头,而SIZENWSE/SIZENESW/SIZEALL应显示为闭合的手。在Wayland上,任何大小的光标都应该显示为一只手。

Pygame Cursor Constant           Description
--------------------------------------------
pygame.SYSTEM_CURSOR_ARROW       arrow
pygame.SYSTEM_CURSOR_IBEAM       i-beam
pygame.SYSTEM_CURSOR_WAIT        wait
pygame.SYSTEM_CURSOR_CROSSHAIR   crosshair
pygame.SYSTEM_CURSOR_WAITARROW   small wait cursor
                                 (or wait if not available)
pygame.SYSTEM_CURSOR_SIZENWSE    double arrow pointing
                                 northwest and southeast
pygame.SYSTEM_CURSOR_SIZENESW    double arrow pointing
                                 northeast and southwest
pygame.SYSTEM_CURSOR_SIZEWE      double arrow pointing
                                 west and east
pygame.SYSTEM_CURSOR_SIZENS      double arrow pointing
                                 north and south
pygame.SYSTEM_CURSOR_SIZEALL     four pointed arrow pointing
                                 north, south, east, and west
pygame.SYSTEM_CURSOR_NO          slashed circle or crossbones
pygame.SYSTEM_CURSOR_HAND        hand

创建不传递参数的游标

除了上述可用游标常量外,还可以调用 pygame.cursors.Cursor() ,并且您的游标已准备好(执行此操作与调用 pygame.cursors.Cursor(pygame.SYSTEM_CURSOR_ARROW) 。执行其中一个调用实际上会使用默认的本机映像创建系统游标。

创建颜色光标

若要创建颜色光标,请创建一个 Cursor 从一个 hotspot 和一个 surfacehotspot 是一个(x,y)坐标,用于确定该点在光标中的确切位置。热点位置必须在 surface

创建位图光标

当鼠标光标可见时,它将显示为使用给定位掩码数组的黑白位图。这个 size 是包含光标宽度和高度的序列。 hotspot 是包含光标热点位置的序列。

光标有宽度和高度,但鼠标位置由一组点坐标表示。因此传递到游标中的值 hotspot 变量帮助pyGame实际确定光标所在的确切位置。

xormasks 是包含游标XOR数据掩码的字节序列。最后, andmasks ,包含光标位掩码数据的字节序列。要创建这些变量,我们可以使用 pygame.cursors.compile()create binary cursor data from simple strings 功能。

宽度和高度必须是8的倍数,并且遮罩数组的大小必须与给定的宽度和高度相匹配。否则将引发异常。

copy()
| :sl:`copy the current cursor`
| :sg:`copy() -> Cursor`

返回与原始光标对象具有相同数据和热点的新光标对象。

type
Gets the cursor type
type -> string

类型将为 "system""bitmap" ,或 "color"

data
Gets the cursor data
data -> tuple

返回用于创建此游标对象的数据,包装在一个元组中。

New in pygame 2.0.1.

创建和设置游标的示例代码。(点击鼠标切换光标)

# pygame setup
import pygame as pg

pg.init()
screen = pg.display.set_mode([600, 400])
pg.display.set_caption("Example code for the cursors module")

# create a system cursor
system = pg.cursors.Cursor(pg.SYSTEM_CURSOR_NO)

# create bitmap cursors
bitmap_1 = pg.cursors.Cursor(*pg.cursors.arrow)
bitmap_2 = pg.cursors.Cursor(
    (24, 24), (0, 0), *pg.cursors.compile(pg.cursors.thickarrow_strings)
)

# create a color cursor
surf = pg.Surface((40, 40)) # you could also load an image 
surf.fill((120, 50, 50))        # and use that as your surface
color = pg.cursors.Cursor((20, 20), surf)

cursors = [system, bitmap_1, bitmap_2, color]
cursor_index = 0

pg.mouse.set_cursor(cursors[cursor_index])

clock = pg.time.Clock()
going = True
while going:
    clock.tick(60)
    screen.fill((0, 75, 30))
    pg.display.flip()

    for event in pg.event.get():
        if event.type == pg.QUIT or (event.type == pg.KEYDOWN and event.key == pg.K_ESCAPE):
            going = False

        # if the mouse is clicked it will switch to a new cursor
        if event.type == pg.MOUSEBUTTONDOWN:
            cursor_index += 1
            cursor_index %= len(cursors)
            pg.mouse.set_cursor(cursors[cursor_index])

pg.quit()



Edit on GitHub