如何在pygame中更改光标?

Sal*_*ead 3 python pygame

我的 pygame 程序中有一个按钮。
每当我的光标悬停在按钮上时,我希望光标更改为:在此处输入图片说明

我已经知道如何获取鼠标的位置以及按下鼠标的时间。我只需要知道如何更改光标。

小智 8

pygame.mouse.set_cursor()

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

for example:
pygame.mouse.set_cursor(pygame.SYSTEM_CURSOR_HAND)
Run Code Online (Sandbox Code Playgroud)

  • 这个答案不符合问题。但对于希望更改为指定光标之一的其他人来说可能很有用。 (3认同)

Cri*_*ber 6

如果您想从图像创建和使用自己的彩色自定义光标(并根据您在游戏中的位置显示不同的光标,例如在菜单或“游戏”中)。

这是程序:

  • 将鼠标可见性设置为 false
  • 创建要用作光标的自定义图像
  • 从该自定义光标图像创建一个矩形
  • 将 rects 位置更新为您的(不可见的)鼠标位置
  • 在矩形的位置绘制图像

这是一个简短的代码示例:

pygame.mouse.set_visible(False)
cursor_img_rect = cursor_img.get_rect()

while True:
    # in your main loop update the position every frame and blit the image    
    cursor_img_rect.center = pygame.mouse.get_pos()  # update position 
    gameDisplay.blit(cursor, cursor_rect) # draw the cursor
Run Code Online (Sandbox Code Playgroud)