我的 pygame 程序中有一个按钮。
每当我的光标悬停在按钮上时,我希望光标变为:
我已经知道如何获取鼠标的位置以及按下鼠标的时间。我只需要知道如何更改光标。
如果您想从图像中创建和使用您自己的彩色自定义光标(并根据您在游戏中的位置显示不同的光标,例如在菜单或“游戏”中)。
这是程序:
这是一个简短的代码示例:
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
要知道:Pygame 只支持系统的黑白光标。
您可以使用 pygame.cursors.load_xbm 在PyGame中加载光标,请在此处阅读全文,了解更多信息
if event.type == pygame.MOUSEMOTION:
x, y = event.pos
if ( x in range(100,100)) and (y in range(200,200)):
print("Hovering over the item!")
new_cursor()
else: default_cursor()
我建议将点碰撞到“按钮”所在的矩形中。(碰撞点函数)。或者使用 if x in range(button x range) 和 x in range (button y range)。
使用 pygame.org。这是一个对 pygame 很有帮助的网站。