[英]Pygame Set Mouse Cursor from Bitmap
我正在使用 pygame 制作圖像編輯器,我想知道是否可以將鼠標光標更改為更適合畫筆的內容(例如圓形或矩形)。 Pygame 有一種非常奇怪的方法,我不確定它是否能很好地工作。 有沒有辦法可以寫入位圖然后使用它?
如果有一種方法可以用 Python 來做到這一點,我想那也行。
由於@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))
我不確定我對多色光標的感覺,但至少這是可能的。
你也可以簡單地加載一個圖像來代替繪制一些東西而不是它。 例如:
cursor = pygame.image.load('CURSOR IMAGE FILE HERE')
pygame.mouse.set_visible(False) # hide the cursor
coord = pygame.mouse.get_pos()
#write this in the loop
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))
希望這有幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.