简体   繁体   中英

Python/Pygame mouse position does not update (blit function)

I'm trying to make a simple menu using Pygame but I found that whenever I use pygame.mouse.get_position, it does blit what i want but i have to keep move my mouse to make my picture keep blitting.

import pygame
import sys

pygame.init()

screen = pygame.display.set_mode((800,600))
pygame.display.set_caption('cursor test')

cursorPng = pygame.image.load('resources/images/cursor.png')
start = pygame.image.load('resources/images/menuStart.jpg')
enemy = pygame.image.load('resources/images/enemy-1.png')

white = (255,255,255)
black = (0,0,0)

clock = pygame.time.Clock()
FPS = 60

while True:
    screen.fill(white)
    pygame.mouse.set_visible(False)

    x,y = pygame.mouse.get_pos()
    x = x - cursorPng.get_width()/2
    y = y - cursorPng.get_height()/2
    screen.blit(cursorPng,(x,y))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                pygame.quit()
                sys.exit()

        elif event.type == pygame.MOUSEMOTION:
            if x < 50 and y < 250:
                screen.blit(enemy,(100,100))

    clock.tick(FPS)
    pygame.display.update()

what's wrong?

Take a look at your code:

for event in pygame.event.get():
    ...
    elif event.type == pygame.MOUSEMOTION:
        if x < 50 and y < 250:
            screen.blit(enemy,(100,100))

You check for events, and if you detect that the mouse is being moved (and only then), you draw the image to the screen.

If you want to draw the image even if the mouse is not being moved, just stop checking for the MOUSEMOTION event and simply draw the image always:

while True:
    screen.fill(white)
    pygame.mouse.set_visible(False)

    x,y = pygame.mouse.get_pos()
    x = x - cursorPng.get_width()/2
    y = y - cursorPng.get_height()/2
    screen.blit(cursorPng,(x,y))
    if x < 50 and y < 250:
        screen.blit(enemy,(100,100))

    for event in pygame.event.get():
        ...

You need to blit into the screen a Surface and a Rect.

First, use this snippet I use for loading images. It makes sure the image is loaded correctly:

def loadImage(name, alpha=False):
"Loads given image"

    try:
        surface = pygame.image.load(name)
    except pygame.error:
        raise SystemExit('Could not load image "%s" %s' %
                     (name, pygame.get_error()))
    if alpha:
        corner = surface.get_at((0, 0))
        surface.set_colorkey(corner, pygame.RLEACCEL)

    return surface.convert_alpha()

Second, when you get the Surface, get its rect like this:

cursorSurf = loadImage('resources/images/cursor.png')
cursorRect = cursorSurf.get_rect()

Then, inside the update do the following:

cursorRect.center = pygame.mouse.get_pos()

And finnally, blit to screen like this:

screen.blit(cursorSurf, cursorRect)

Now you will notice your Mouse is being rendered correctly without having to move your mouse.

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