[英]How to change cursor in pygame?
I have a button in my pygame program.我的 pygame 程序中有一个按钮。
Whenever my cursor is hovering over the button, I want the cursor to change to this:每当我的 cursor 悬停在按钮上时,我希望 cursor 更改为:
I already know how to get the position of the mouse and when it is pressed.我已经知道如何获取鼠标的 position 以及何时按下它。 I just need to know how to change the cursor.
我只需要知道如何更改 cursor。
In case you want to create and use your own colorfully customized cursor from an image (and display different cursors depending on where you are in the game, eg in the menu or "ingame").如果您想从图像中创建和使用您自己的彩色定制 cursor(并根据您在游戏中的位置显示不同的光标,例如在菜单或“游戏”中)。
Here is the procedure:这是程序:
Here is a short code example:这是一个简短的代码示例:
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
You know: Pygame only supports black and white cursors for the system.要知道:Pygame 系统只支持黑白光标。 You can load cursors in PyGame with pygame.cursors.load_xbm, Read the full article here , for more
您可以使用 pygame.cursors.load_xbm 在 PyGame 中加载游标,请在此处阅读全文,了解更多信息
If you wanna detect cursor hovering the item, which (100,100) to (200,200) is the item position, this is the code:如果您想检测 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)
I recommend colliding the point into the rect that the "button" is in.(collidepoint function).我建议将点碰撞到“按钮”所在的矩形中。(碰撞点函数)。 Or alternatively use if x in range(button x range) and x in range (button y range).
或者使用 if x in range(button x range) 和 x in range (button y range)。
Once you do collidepoint you can set cursor visibility to False and then draw an image/ rect/ circle at the coordinates of the cursor.完成碰撞点后,您可以将 cursor 可见性设置为 False,然后在 cursor 的坐标处绘制图像/矩形/圆圈。 It will create the change the cursor effect.
它将创建 cursor 效果的更改。
Use pygame.org.使用 pygame.org。 It is a helpful site for pygame.
它是 pygame 的有用站点。
I'll show you my code for this to help you out:我将向您展示我的代码以帮助您:
# --- 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.
One thing about blitting a custom cursor to the screen of every frame is that its position will be affected by the frame rate however small that delay is.将自定义 cursor 传送到每一帧的屏幕的一件事是,它的 position 将受到帧速率的影响,无论延迟有多小。 So the exact xy coordinates of the mouse click might not line up exactly with the drawn image.
所以鼠标点击的确切 xy 坐标可能与绘制的图像不完全一致。 If all you want is the hand cursor another option might be to add this line
如果您想要的只是手 cursor 另一个选项可能是添加此行
pygame.mouse.set_cursor(pygame.cursors.Cursor(pygame.SYSTEM_CURSOR_HAND))
For anyone in the future finding this question who is looking to have the cursor change to indicate a clickable button on any operating system, the best option is to simply use this:对于将来发现此问题并希望将 cursor 更改以指示任何操作系统上的可单击按钮的任何人,最好的选择是简单地使用它:
pygame.mouse.set_cursor(pygame.SYSTEM_CURSOR_HAND)
This will set the cursor to the system hand cursor for any operating system until you set it to something else again.这将为任何操作系统将 cursor 设置为系统手 cursor,直到您再次将其设置为其他值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.