3
votes

I have an event which makes me able to move a sqaure(a_block) with the mouse. However, I can't seem to change it to move if the mousebutton is constantly pressed down

if event.type == pygame.MOUSEMOTION:
    mouse_position = pygame.mouse.get_pos()
    a_block.set_position(mouse_position[0],mouse_position[1]
1
You should also give what is already happening with your code. Like is the block always being drawn at the mouse even if the button has not been held down? - SimonT
Right now the block moves as it should, where the cursor is, no problems. I just wish to change it to only move to the cursors location if the mouse button is constantly pressed down. My first attempt resulted in only changing loc when clicking. - user3599484

1 Answers

3
votes

Make sure that you are going through all pygame events, because i think that you are only looking at the first event which turns out to be the mouse position when the button isn't pressed down, but when the mouse button is pressed down the first event is the button press. Here is the code snippet I used to work:

for events in pygame.event.get(): #look at all events
    if events.type == pygame.MOUSEMOTION:
        mouse_position = pygame.mouse.get_pos()
        a_block.set_position(mouse_position[0],mouse_position[1])