尝试创建一个简单的代码,在窗口中以及全屏时注册鼠标位置。
当前处于窗口模式(提供光标在窗口上方)时,将返回正确的值,但是无论鼠标位置如何,全屏都会返回值800,480(屏幕分辨率)。这是我的代码:
import pygame
windowwidth = 800
windowheight = 480
pygame.init()
pygame.display.init()
screen = pygame.display.set_mode((windowwidth, windowheight))
pygame.display.flip()
running = 1
while running:
LEFT = 1
event1 = pygame.event.get()
for event in event1:
if event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
print(event.pos)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_f:
pygame.display.set_mode((800,480), pygame.FULLSCREEN)
if event.key == pygame.K_g:
pygame.display.set_mode((800, 480))
else:
break
pygame.display.flip()
答案 0 :(得分:1)
虽然@Michael O' Dwyer的回答足够了,但除了澄清预先确定PyGame窗口的画布尺寸之外,您还可以使用自动检测系统的宽度和高度pygame.display.Info()
方法可以更清晰。这将是您自动检测宽度和高度的代码:
import pygame
# Initialize Pygame
pygame.init()
# Get system display information
displayInfo = pygame.display.Info()
windowwidth = displayInfo.current_w
windowheight = displayInfo.current_h
# Commented out - No need to reinitialize other Pygame modules
# after pygame.init() is called
# pygame.display.init()
screen = pygame.display.set_mode((windowwidth, windowheight))
pygame.display.flip()
running = 1
while running:
LEFT = 1
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
print(event.pos)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_f:
pygame.display.set_mode((windowwidth, windowheight), pygame.FULLSCREEN)
if event.key == pygame.K_g:
pygame.display.set_mode((800, 480))
else:
break
pygame.display.flip()
this document是pygame.display.Info()
方法的PyGame文档。
希望这有帮助!
答案 1 :(得分:0)
问题在于pygame正在以像素为单位获取鼠标点击坐标,该坐标与pygame.display.set_mode()
中传递的分辨率相关联。因此,当它全屏显示时,你将分辨率设置为(800,480),它不会给你准确的像素坐标。
解决方案?
全屏时传递真实的显示器分辨率。在pygame中,如果你传入(0,0)作为分辨率,pygame将为你创建一个全屏分辨率。
所以传递这个特殊的屏幕尺寸和全屏旗帜就是这样!代码现在有效!
我希望这个答案对你有所帮助!如果您有任何其他问题,请在下面发表评论!
import pygame
windowwidth = 0
windowheight = 0
pygame.init()
pygame.display.init()
screen = pygame.display.set_mode((windowwidth, windowheight))
pygame.display.flip()
running = 1
while running:
LEFT = 1
event1 = pygame.event.get()
for event in event1:
if event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
print(event.pos)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_f:
pygame.display.set_mode((0,0), pygame.FULLSCREEN)
if event.key == pygame.K_g:
pygame.display.set_mode((800, 480))
else:
break
pygame.display.flip()