Pygame 鼠标位置获取教程:使用 pygame.mouse.get_pos() 函数
在 Pygame 中,可以使用 'pygame.mouse.get_pos()' 函数来获取鼠标的当前位置。
以下是一个示例代码,展示了如何使用 'pygame.mouse.get_pos()' 函数来获取鼠标位置:
import pygame
# 初始化 Pygame
pygame.init()
# 设置窗口尺寸
window_size = (800, 600)
window = pygame.display.set_mode(window_size)
# 游戏主循环
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 获取鼠标位置
mouse_pos = pygame.mouse.get_pos()
print(f'鼠标位置: {mouse_pos}')
pygame.display.update()
# 退出 Pygame
pygame.quit()
在上述代码中,我们在游戏的主循环中使用 'pygame.mouse.get_pos()' 函数获取鼠标的当前位置,并将其存储在 'mouse_pos' 变量中。
通过运行以上代码,应该可以在控制台输出鼠标的当前位置。
请注意,'pygame.mouse.get_pos()' 函数将返回一个包含鼠标当前位置的元组,元组中的第一个元素表示横坐标,第二个元素表示纵坐标。
希望这个示例对您有所帮助!如有其他问题,请随时提问。
原文地址: http://www.cveoy.top/t/topic/nJM 著作权归作者所有。请勿转载和采集!