Pygame mouse cursor load/unload

Ian Kelly ian.g.kelly at gmail.com
Sat Mar 2 22:08:18 EST 2013


On Sat, Mar 2, 2013 at 6:56 PM, Alex Gardner <agardner210 at gmail.com> wrote:
> I am in the process of making a pong game in python using the pygame library.  My current problem is that when I move the mouse, it turns off as soon as the mouse stops moving.  The way I am doing this is by making the default cursor invisible and using .png files as replacements for the cursor.  Perhaps my code would best explain my problem.  I will take help in any way that I can.  Here are the links that contain my code:

Your mouse motion code draws the paddle in the new position, waits
1/10th of a second, and then draws over it again with the "invisible"
paddle.  Thus, approximately 1/10th of a second after you stop moving
the mouse, it disappears.

Mouse motion events are probably not the best way to do this.  You can
instead just capture the current position of the mouse on every frame
and use that instead.  I replaced your main loop with the following:

paddle_pos = (0, 0)
clock = pygame.time.Clock()

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()

    # Erase the paddle from the old mouse position.
    screen.blit(bpaddle, paddle_pos)
    # Redraw the net before the paddle so that the paddle can appear over it.
    pygame.draw.line(screen, game.lineColor, game.net1, game.net2,
                     game.netWidth)
    # Get the new mouse position.
    paddle_pos = pygame.mouse.get_pos()
    # Draw the paddle at the new mouse position.
    screen.blit(beeper, paddle_pos)
    # Update the screen if it's double-buffered.
    pygame.display.update()
    # Finally, let the CPU idle until it's time for the next frame.
    # 50 here means that it will sleep long enough to achieve 50 FPS.
    clock.tick(50)

And I think you will find that this does what you want.

A couple more observations while I'm at it.  Generally there is no
need to be calling pygame.display.update() multiple times per frame.
Just draw everything that you need, and then call it once at the end
of the loop, as I have shown above.  Also, the shebang line only does
anything if it's the very first line in the file, so it would need to
appear before the module docstring to do anything useful.



More information about the Python-list mailing list