簡體   English   中英

如何在 pygame 中更改 cursor?

[英]How to change cursor in pygame?

我的 pygame 程序中有一個按鈕。
每當我的 cursor 懸停在按鈕上時,我希望 cursor 更改為: 在此處輸入圖像描述

我已經知道如何獲取鼠標的 position 以及何時按下它。 我只需要知道如何更改 cursor。

如果您想從圖像中創建和使用您自己的彩色定制 cursor(並根據您在游戲中的位置顯示不同的光標,例如在菜單或“游戲”中)。

這是程序:

  • 將鼠標可見性設置為 false
  • 創建要用作 cursor 的自定義映像
  • 從該定制的 cursor 圖像創建一個矩形
  • 將矩形 position 更新到您的(隱形)鼠標 position
  • 在矩形的位置繪制圖像

這是一個簡短的代碼示例:

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 中加載游標,請在此處閱讀全文,了解更多信息
如果您想檢測 cursor 懸停在項目上,其中 (100,100) 到 (200,200) 是項目 position,這是代碼:

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

我建議將點碰撞到“按鈕”所在的矩形中。(碰撞點函數)。 或者使用 if x in range(button x range) 和 x in range (button y range)。

完成碰撞點后,您可以將 cursor 可見性設置為 False,然后在 cursor 的坐標處繪制圖像/矩形/圓圈。 它將創建 cursor 效果的更改。

使用 pygame.org。 它是 pygame 的有用站點。

我將向您展示我的代碼以幫助您:

# --- Libraries --- #

# Import pygame
import pygame

# INITialize pygame


pygame.init() 
# --- Window & Cursor --- #

# Open a WINDOW of size [500, 300]
window=pygame.display.set_mode([500,300]) 

# SET_VISIBLE of cursor to False
pygame.mouse.set_visible(False) 
# Set the variable cursor_size to 10
cursor_size=10


#### ---- MAIN LOOP ---- ####
running=True 
# Create a variable called running with value True
while running: 

# Loop while running



    # --- Event Loop --- #

    # Create an EVENT LOOP
    for event in pygame.event.get(): 

        # Check for the QUIT event
        if event.type==pygame.QUIT: 
            running=False 

            # Set running to False
            # ---> TEST AFTER THIS LINE <--- #



        # --- The cursor increases size while clicking --- #

        # Check if the event TYPE is MOUSEBUTTONDOWN

        if event.type==pygame.MOUSEBUTTONDOWN: 
            cursor_size=20
            # Set cursor_size to 20
            # ---> TEST AFTER THIS LINE <--- #


        # Otherwise if the event TYPE is MOUSEBUTTONUP
        elif event.type==pygame.MOUSEBUTTONUP: 
            cursor_size=10
        
 


    # GET_POSition of mouse and store it in x, y
    x,y=pygame.mouse.get_pos() 
    # ---> TEST AFTER THIS LINE <--- #



    # --- Draw --- #

    # FILL the window with WHITE
    window.fill((215,158,222)) 


    # Draw a CIRCLE of any COLOR at position (x, y)
    # with cursor_size
    pygame.draw.circle(window,(255,255,255),(x,y),cursor_size,10) 


    # FLIP the display
    pygame.display.flip() 
    # ---> TEST AFTER THIS LINE <--- #




# Turn in your Coding Exercise.

將自定義 cursor 傳送到每一幀的屏幕的一件事是,它的 position 將受到幀速率的影響,無論延遲有多小。 所以鼠標點擊的確切 xy 坐標可能與繪制的圖像不完全一致。 如果您想要的只是手 cursor 另一個選項可能是添加此行

pygame.mouse.set_cursor(pygame.cursors.Cursor(pygame.SYSTEM_CURSOR_HAND))

對於將來發現此問題並希望將 cursor 更改以指示任何操作系統上的可單擊按鈕的任何人,最好的選擇是簡單地使用它:

pygame.mouse.set_cursor(pygame.SYSTEM_CURSOR_HAND)

這將為任何操作系統將 cursor 設置為系統手 cursor,直到您再次將其設置為其他值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM