繁体   English   中英

pygame.mouse.get_pressed() 没有响应

[英]pygame.mouse.get_pressed() not responding

代码如下

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 window 本身在我运行程序并单击它时没有响应,甚至没有打印 X 或 Y 我尝试添加一些延迟,认为可能 pygame 不喜欢它有多快

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

对于游戏的每一帧,您都需要对事件队列进行某种调用。 这确保您的程序可以在内部与操作系统的 rest 交互。 如果您没有在游戏中使用其他事件函数,则应调用pygame.event.pump()以允许 pygame 处理内部动作。

由于pygame.mouse.get_pressed()返回一系列布尔值,您必须使用订阅来评估按钮的 state:

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()

这里有几个问题。

首先是pygame.mouse.get_pressed()返回按钮状态的元组,例如( True, False, False ) 但它只在pygame.event.get()被调用返回一个有效的 state 。 参考: https://www.pygame.org/docs/ref/mouse.html#pygame.mouse.get_pressed

实现您想要做的最简单的方法是首先等待MOUSEBUTTONDOWN (或MOUSEBUTTONUP )事件,然后检查单击的位置和按钮的 state 的位置。

# Main loop
while not done:

    # Handle user-input
    for event in pygame.event.get():
        if ( event.type == pygame.QUIT ):
            done = True
        elif ( event.type == pygame.MOUSEBUTTONUP ):
            # On mouse-click
            mouse_x, mouse_y = event.pos
            mouse_buttons    = pygame.mouse.get_pressed()

            if ( mouse_buttons[0] ):         # ( True, ... )
                X = ( mouse_x // 10 ) * 10
                Y = ( mouse_y // 10 ) * 10
                print( "Mouse-click at %d, %d -> %d, %d" % ( mouse_x, mouse_y, X, Y ) )
                pygame.draw.rect( win, (255,255,255), (X,Y,10,10) )

https://www.pygame.org/docs/ref/mouse.html#pygame.mouse.get_pressed

如您所见,按下 mouse.get 返回 boolean 变量序列,而不是您评估它的整数元组。

在运行 while 循环之前,打印pygame.mouse.get_pressed()以查看它是如何工作的以及它在您的程序中具体返回的内容。

The pygame.mouse.get_pressed() returns Boolean values such as True or False but doesn't store actual mouse cursor press position. 所以试着在你的主循环中使用它:

for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_q:
            pygame.quit()
            sys.exit()
    elif event.type == pygame.MOUSEBUTTONDOWN:
        mouse_x, mouse_y = pygame.mouse.get_pos()

希望这可以帮助。 更多关于pygame.mouse命令在这里

pygame.mouse.get_pressed() 返回一个 boolean 的元组,类似于 (False,True,False)。 因此,例如,如果您想检查是否按下了左键,您会这样做

    mouse=pygame.mouse.get_pressed() #(True,False,False)
    if mouse[0]: #If first mouse button pressed AKA left click.
        #your code

关于屏幕冻结,您需要在 main while 内有一个事件循环来获取事件消息。

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        running = False

没有它,pygame 屏幕将冻结。

希望这可以帮助您修复代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM