我如何 运行 基于 python 中的单击的命令?

How do I run a command based on a click in python?

对于在我之前的程序中提供帮助的任何人,非常感谢!

如果用户左键单击,我正在尝试 运行 pygame 中的命令。这是我的代码:

def check_pressed():

    mousex, mousey = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    if click[0] == 1:
        if mousex >= 240 and mousex <= 755 and mousey >= 390 and mousey <= 470:
            MENUSCREEN = False
            screen.fill(BLACK)
            drawsimonsquares()

我已将 check_pressed() 插入到我的主循环中(在 mainscreen() 内)。当我按住它时,click 的索引值 0 只有一个。 (因此 运行 仅当我按住左键单击时才启动程序)。是否有任何我可以使用的命令,以便我的 if 语句之后的命令字符串始终为真,除非另有说明?学校项目再次请帮助。 谢谢 -皮耶罗

我很确定您希望您的程序在鼠标按钮按下时启动。这是可以感知它的代码:

for event in pygame.event.get():
    if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1  # left click:
        # do stuff...