简体   繁体   中英

pygame.mouse.get_pressed() not responding

the code is as follows

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

the Problem is the pygame window itself does not respond when i run the program and click it does not even print X or Y i tried adding some delay thinking maybe pygame does not like how fast it is

You have to implement an event loop and to get the events messages with pygame.event.get() . At least you have to invoke 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.

Since pygame.mouse.get_pressed() returns a sequence of booleans you have to use subscription to evaluate the state of a button:

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

I recommend to add an event loop to the application:

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

There's a couple of issues here.

First is that pygame.mouse.get_pressed() returns a tuple of button states, something like ( True, False, False ) . But it only returns a valid state after pygame.event.get() has been called. Ref: https://www.pygame.org/docs/ref/mouse.html#pygame.mouse.get_pressed

The easiest way to achieve what it looks like you are trying to do, is to first wait for a MOUSEBUTTONDOWN (or MOUSEBUTTONUP ) event, and then check the location of the click and state of the buttons.

# 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

As you can see, mouse.get pressed returns a sequence of boolean variables, not a tuple of integers as you were evaluating it to.

Before running the while loop, print pygame.mouse.get_pressed() to see how it works and what it returns specifically in your program.

The pygame.mouse.get_pressed() returns Boolean values such as True or False but doesn't store actual mouse cursor press position. So try instead to use this in your main loop:

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

Hope this helps. More on pygame.mouse commands here

pygame.mouse.get_pressed() returns a tuple of boolean, something like (False,True,False). So if you want to check whether left click is pressed for example you would do

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

Regarding the screen freezing, you need an event loop inside your main while to get event messages.

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

Without it the pygame screen will freeze.

Hope this helps you fix your code.

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