cursors模块主要用于定义pygame中的Cursor类,也就是我们常说的游标(光标)。在pygame中,游标主要分为三类,分别是系统的游标、pygame中预定义的游标以及自定义的游标。下面用例子来说明这三种游标的创建和使用。
游标的创建
# 使用系统的游标
cursor1 = pygame.cursors.Cursor(pygame.SYSTEM_CURSOR_ARROW)
系统游标:
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中的游标
# 预定义的游标
cursor2 = pygame.cursors.arrow
# 编译预定义的字符串来创建游标
cursor3 = pygame.cursors.compile(pygame.cursors.textmarker_strings)
pygame中预定义的游标和字符串:
Pygame Cursor | Pygame Cursor String |
---|---|
pygame.cursors.arrow | pygame.cursors.thickarrow_strings |
pygame.cursors.diamond | pygame.cursors.sizer_x_strings |
pygame.cursors.broken_x | pygame.cursors.sizer_y_strings |
pygame.cursors.tri_left | pygame.cursors.sizer_xy_strings |
pygame.cursors.tri_right | pygame.cursor.textmarker_strings |
# 使用自定义的游标
cursor4 = pygame.Cursor(
(16, 16),
(0, 0),
(
0x00,
0x00,
0x40,
0x00,
0x60,
0x00,
0x70,
0x00,
0x78,
0x00,
0x7C,
0x00,
0x7E,
0x00,
0x7F,
0x00,
0x7F,
0x80,
0x7C,
0x00,
0x6C,
0x00,
0x46,
0x00,
0x06,
0x00,
0x03,
0x00,
0x03,
0x00,
0x00,
0x00,
),
(
0x40,
0x00,
0xE0,
0x00,
0xF0,
0x00,
0xF8,
0x00,
0xFC,
0x00,
0xFE,
0x00,
0xFF,
0x00,
0xFF,
0x80,
0xFF,
0xC0,
0xFF,
0x80,
0xFE,
0x00,
0xEF,
0x00,
0x4F,
0x00,
0x07,
0x80,
0x07,
0x80,
0x03,
0x00,
),
)
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 ",
" ",
" ",
" ")
cursor5 = pygame.cursors.compile(thickarrow_strings)
游标的使用
游标的使用非常简单,只需要通过调用mouse模块中的set_cursor()函数来设置不同的游标。
# 游标的使用
pygame.mouse.set_cursor(cursor1)