1

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)
2
  • ... how does the function get called?
    – SamB
    Mar 27, 2012 at 19:02
  • it gets called using the event handler inside the main while loop.
    – Max Hudson
    Mar 27, 2012 at 19:19

2 Answers 2

5

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.

1

You need to call pygame.display.flip() at the end of your function -- otherwise, your drawing never gets copied from the backbuffer to the live display.

1
  • hmm didn't seem to do anything :/ why would I need to do this? I don't need to do this when I use screen.blit() inside my while loop. just when i use it in the function..
    – Max Hudson
    Mar 27, 2012 at 19:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.