You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Pygame中将鼠标光标恢复为默认样式?

在Pygame中恢复默认鼠标光标样式

要把鼠标光标恢复成默认的箭头样式,直接调用Pygame提供的系统箭头光标常量即可,有两种简洁写法:

方法一:通过Cursor对象传入常量

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

方法二:直接传入系统光标常量(更简洁)

pygame.mouse.set_cursor(pygame.SYSTEM_CURSOR_ARROW)

结合你的按钮悬停场景,完整的逻辑示例如下:

import pygame

pygame.init()
screen = pygame.display.set_mode((400, 300))
# 定义按钮区域
button_rect = pygame.Rect(150, 100, 100, 50)

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    mouse_pos = pygame.mouse.get_pos()
    # 检测鼠标是否在按钮上
    if button_rect.collidepoint(mouse_pos):
        # 切换为手型光标
        pygame.mouse.set_cursor(pygame.cursors.Cursor(pygame.SYSTEM_CURSOR_HAND))
    else:
        # 恢复默认箭头光标
        pygame.mouse.set_cursor(pygame.SYSTEM_CURSOR_ARROW)

    # 绘制界面
    screen.fill((255, 255, 255))
    pygame.draw.rect(screen, (0, 255, 0), button_rect)
    pygame.display.flip()

pygame.quit()

原理很简单:Pygame内置的SYSTEM_CURSOR_ARROW就是系统默认的箭头光标常量,和你切换手型光标时用的SYSTEM_CURSOR_HAND属于同一类常量,直接切换回去就能恢复初始样式。

内容的提问来源于stack exchange,提问作者Ilay Furst

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 20:01:02