要获取鼠标位置,你可以使用 pygame.mouse.get_pos() 函数。这个函数将返回一个包含鼠标位置的元组,其中第一个元素是鼠标的 X 坐标,第二个元素是鼠标的 Y 坐标。

以下是一个示例代码,演示了如何获取鼠标位置并在屏幕上显示出来:

import pygame

# 初始化 pygame
pygame.init()

# 创建屏幕
screen = pygame.display.set_mode((800, 600))

# 游戏循环
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    
    # 获取鼠标位置
    mouse_pos = pygame.mouse.get_pos()
    
    # 在屏幕上显示鼠标位置
    screen.fill((0, 0, 0))  # 清空屏幕
    font = pygame.font.Font(None, 36)
    text = font.render(f"Mouse Position: {mouse_pos}", True, (255, 255, 255))
    screen.blit(text, (10, 10))
    pygame.display.flip()

# 退出游戏
pygame.quit()

这个示例代码创建了一个窗口,然后在窗口中循环获取鼠标位置,并将其显示在屏幕上。当用户关闭窗口时,游戏循环会结束,从而退出游戏。


原文地址: https://www.cveoy.top/t/topic/pSCR 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录