Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pygame Mouse movement

Tags:

python

pygame

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]
like image 429
user3599484 Avatar asked Oct 29 '25 16:10

user3599484


1 Answers

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])
like image 176
Elias Avatar answered Oct 31 '25 07:10

Elias



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!