14

我正在使用 pygame 制作图像编辑器,我想知道是否可以将鼠标光标更改为更适合画笔的东西(例如圆形或矩形)。Pygame 有一种非常奇怪的方法,我不确定它是否能很好地工作。有没有办法可以写入位图然后使用它?

如果有一种方法可以用 Python 来做,我想那也可以。

4

4 回答 4

18

另一种选择是简单地隐藏光标,加载您喜欢的任意位图并在光标所在的每一帧中绘制它。

于 2010-03-01T09:53:29.363 回答
6

您可以使用 pygame.cursors.load_xbm 在PyGame中加载游标——当然,这只是黑白游标,这是 PyGame 的一个有据可查的限制,根据其文档(我引用):

Pygame 仅支持系统的黑白光标。

对于 XBM 格式文档,请参见此处您可以根据其文档准备此类文件,例如使用 PIL 库。

于 2010-02-27T04:25:38.353 回答
3

由于@SapphireSun 在评论中询问了另一种方式,我想提出一个不同的答案,这是手动绘制光标。我想这就是@Mizipzor 在他的回答中所建议的,所以也许这只是一个阐述。

首先隐藏鼠标光标,然后每次更新屏幕框架时,“手动”绘制光标:

pygame.mouse.set_visible(False)  # hide the cursor

# Image for "manual" cursor
MANUAL_CURSOR = pygame.image.load('finger_cursor_16.png').convert_alpha()

# In main loop ~
    ...

    # paint cursor at mouse the current location
    screen.blit( MANUAL_CURSOR, ( pygame.mouse.get_pos() ) ) 

这个方法允许 PyGame 程序有任何类型的光标位图。在正确的位置获得“点击”热点可能有些棘手,但这可以通过设置透明光标来实现,热点位于与自定义位图匹配的位置。有关详细信息,请参阅手册

# transparent 8x8 cursor with the hot-spot at (4,4)
pygame.mouse.set_cursor((8,8),(4,4),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0))

我不确定我对多色光标的感觉如何,但至少这是可能的。

于 2019-01-25T00:33:09.593 回答
1

您也可以简单地加载图像来替换绘制的东西而不是它。例如:

cursor = pygame.image.load('CURSOR IMAGE FILE HERE')
pygame.mouse.set_visible(False)  # hide the cursor

#write this in the loop
coord = pygame.mouse.get_pos()
screen.blit(cursor, coord)

如果你只想用一个形状替换它,你可以这样做:

pygame.mouse.set_visible(False)  # hide the cursor
coord = pygame.mouse.get_pos()
pygame.draw.(shape here)(screen, (color), (coord, width, height))

希望这有帮助!

于 2019-11-09T07:19:23.880 回答