简体   繁体   中英

pygame won't blit inside function?

I have a block of code that worked outside of a function, but not inside.

I also made sure the necessary variables were global.

I can get the width of the surface using chair.get_width() inside the function and it works fine, but I cannot execute screen.blit(chair, (10,10)) for some reason. I don't get an error, it just doesn't do anything...

Here is my script (it creates a window, then calls a function every 100 milliseconds that gets the mouse position, rotates an image x degrees and then blits (or should blit) the image to the window):

    cif = "images/chair3.png"

    import pygame, sys
    from pygame.locals import *

    pygame.init()

    screen = pygame.display.set_mode((640,480),0,32)

    chair = pygame.image.load(cif).convert_alpha()

    pygame.time.set_timer(USEREVENT + 1, 100)

    def cursor(speed):
        global i
        global chair
                    global screen
        x,y = pygame.mouse.get_pos()
        x -= chair.get_width()/2
        y -= chair.get_height()/2

        if i < 360:
            i = i + 360/(1000/speed)
        else:
            i = 0

        orig_chair_rect = chair.get_rect()
        chair1 = pygame.transform.rotate(chair, i);
        rot_chair_rect = orig_chair_rect.copy()
        rot_chair_rect.center = chair1.get_rect().center
        chair1 = chair1.subsurface(rot_chair_rect).copy()

        print chair.get_width()

        screen.blit(chair1,(x,y))

    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == USEREVENT + 1:
                cursor(50)

You do need to use pygame.display.flip() or pygame.display.update() , the screen will never update if you don't, and therefore your chair will not be blitted onto the screen.

Tried it, it works. If it doesn't work for you, reinstall Pygame, and try again.

您需要在函数末尾调用pygame.display.flip() ,否则,图形就不会从后缓冲区复制到实时显示中。

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