gpt4 book ai didi

python - pygame.mouse.get_pressed() 没有响应

转载 作者:行者123 更新时间:2023-12-04 01:16:56 24 4
gpt4 key购买 nike

代码如下

import pygame
pygame.init()
info = pygame.display.Info()
win = pygame.display.set_mode(((info.current_h//10)*10,(info.current_w//10)*10))
running = True
while running:
if pygame.mouse.get_pressed() == (1,0,0):
Mouse = pygame.mouse.get_pos()
X = (Mouse[0]//10)*10
Y = (Mouse[1]//10)*10
print(X)
print(Y)
pygame.draw.rect(win,(255,255,255),(X,Y,10,10))
pygame.display.update()

问题是当我运行程序并单击它时 pygame 窗口本身没有响应甚至不打印 X 或 Y我尝试添加一些延迟,我想也许 pygame 不喜欢它有多快

最佳答案

你必须实现一个事件循环并使用 pygame.event.get() 获取事件消息.至少你必须调用 pygame.event.pump() :

For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system. If you are not using other event functions in your game, you should call pygame.event.pump() to allow pygame to handle internal actions.

pygame.mouse.get_pressed()返回一系列 bool 值,您必须使用订阅来评估按钮的状态:

buttons = pygame.mouse.get_pressed()
if buttons[0]:
# [...]

我建议向应用程序添加一个事件循环:

import pygame
pygame.init()
info = pygame.display.Info()
win = pygame.display.set_mode(((info.current_h//10)*10,(info.current_w//10)*10))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
buttons = pygame.mouse.get_pressed()
if buttons[0]:
Mouse = pygame.mouse.get_pos()
X = (Mouse[0]//10)*10
Y = (Mouse[1]//10)*10
print(X)
print(Y)
pygame.draw.rect(win,(255,255,255),(X,Y,10,10))
pygame.display.update()

关于python - pygame.mouse.get_pressed() 没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63197182/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com