Pygame 从位图设置鼠标光标

发布于 2024-08-22 18:41:56 字数 152 浏览 9 评论 0原文

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

如果有一种方法可以用 Python 来实现,我想那也是可行的。

I'm making an image editor using pygame and I was wondering if it was possible to change the mouse cursor to something more suitable (for instance a circle or rectangle) for brushes.
Pygame has a really weird way of doing it that I'm not sure would work very well. Is there a way I can write to a bitmap and then use that?

If there is a way to do it with Python generally, that would work too I suppose.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

左岸枫 2024-08-29 18:41:56

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

Another option is to simply hide the cursor, load any arbitrary bitmap that you like and draw it every frame where the cursor is.

叫嚣ゝ 2024-08-29 18:41:56

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

Pygame仅支持黑白
系统光标。

有关 XBM 格式文档,请参阅此处。您可以根据其文档。

You can load cursors in PyGame with pygame.cursors.load_xbm -- that's black and white cursors only, of course, a well-documented limitation of PyGame, per its docs (and I quote):

Pygame only supports black and white
cursors for the system.

For XBM format docs, see e.g. here. You can prep such files e.g. with the PIL library, per its docs.

又爬满兰若 2024-08-29 18:41:56

由于@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))

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

Since @SapphireSun asked in a comment about another way, I'd like to present a different answer, and this is drawing the cursor manually. I guess this is what @Mizipzor suggests in his answer, so maybe this is just an elaboration.

First Hide the mouse-cursor, then every time the screen-frame is updated, "manually" paint the cursor:

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() ) ) 

This method allows the PyGame program to have any sort of bitmap for the cursor. There may be some trickiness around getting the "click" hot-spot in the right location, but this could be achieved by setting a transparent cursor, with the hot-spot at the position to match the custom bitmap. See the manual for details.

# 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))

I am not sure how I feel about multi-coloured cursors, but at least it's possible.

山色无中 2024-08-29 18:41:56

您也可以简单地加载图像来替换或绘制一些东西来代替它。
举个例子:

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))

希望这有帮助!

you could also just simply load a image to replace of draw something instead of it.
For an example:

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)

If you just want to replace it with a shape you could just do this:

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

Hope this helped!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文