Pygame mouse cursor load/unload

Ian Kelly ian.g.kelly at gmail.com
Sun Mar 3 18:47:08 EST 2013


On Sun, Mar 3, 2013 at 3:09 PM, Alex Gardner <agardner210 at gmail.com> wrote:
>     if (0,0) <= paddle_pos <= (300,300):

This doesn't do what you think it does.  Tuples are compared
lexicographically, not element-wise.  So (250, 350) < (300, 300), but
(350, 250) > (300, 300).

>          paddle_pos = pygame.mouse.get_pos()
>          screen.blit(beeper, paddle_pos)
>          pygame.display.update()
>          clock.tick(50)

You're updating paddle_pos inside the if block.  Once paddle_pos falls
outside that range, the if block won't trigger, and paddle_pos will no
longer be updated, so it will never fall inside that range again.

pygame has a Rect class for rectangle logic that can solve both of
these problems for you.  Given:

paddle_rect = beeper.get_rect()
bounds_rect = pygame.Rect(0, 0, 300, 300)

your position update code then becomes:

paddle_rect.center = pygame.mouse.get_pos()
paddle_rect.clamp_ip(bounds_rect)

Note that paddle_rect can replace paddle_pos entirely.  The code:

screen.blit(beeper, paddle_pos)

simply becomes:

screen.blit(beeper, paddle_rect)



More information about the Python-list mailing list