【发布时间】:2021-01-01 02:57:40
【问题描述】:
我正在尝试用 pygame 制作井字游戏。如果您单击任何一个方块,将显示一个 x。问题是需要多次点击才能显示 x。这是代码:
while True:
for event in pygame.event.get():
if event == pygame.QUIT:
pygame.quit()
sys.exit()
mouse_pos = pygame.mouse.get_pos()
event = pygame.event.wait()
screen.fill(bg_color)
if event.type == pygame.MOUSEBUTTONDOWN and 250 < mouse_pos[0] < 300 and 250 > mouse_pos[1] > 199:
mouse_clicked1 = True
if event.type == pygame.MOUSEBUTTONDOWN and 301 < mouse_pos[0] < 351 and 249 > mouse_pos[1] > 201:
mouse_clicked2 = True
if mouse_clicked1:
screen.blit(x, object_top_left)
if mouse_clicked2:
screen.blit(x, object_top)
【问题讨论】:
-
您是否因为在一个紧密的循环中运行而使资源的事件处理程序挨饿?
-
我没听懂你在说什么
-
您的代码运行在一个紧密的循环中。这可能意味着负责从操作系统收集鼠标点击的 Python 运行时和/或 Pygame 代码通常没有时间运行。您可以通过在
while True循环的末尾添加延迟来确认或排除此理论。 Here's an example。如果这导致您描述的行为消失,那么事件处理程序的资源匮乏确实是罪魁祸首。 -
我应该延迟多少
-
这对实验来说并不重要。 10 毫秒、100 毫秒等等。