简体   繁体   中英

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

For anyone that helped with my previous program, thank you so much!

I am trying to run a command in pygame if the user left-clicks. Here is my code:

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

I have inserted check_pressed() into my main loop (Within mainscreen() ). The index value 0 of click is only one while I hold it down. (therefore running the program only when I hold left click). Is there any command that I can use so that the string of commands after my if statements is always true unless told otherwise? School project again pleasssse help. Thank you -Piero

I am pretty sure you want your program to pick up when the mouse button goes down. Here is code that can sense it:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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