[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] fundamental (stupid) question about blitting part of a background image



Horst F. JENS wrote:
    pygame.init()
    screen = pygame.display.set_mode(WINSIZE)
    pygame.display.set_caption('pygame Stars Example')
    white = 255, 240, 200
    black = 20, 20, 40
        #create the background, tile the bgd image
        bgdtile = load_image('background.jpg', -1)
        # -1 avoid conversion to transparent background
        background = pygame.Surface(SCREENRECT.size)
        for x in range(0, SCREENRECT.width, bgdtile.get_width()):
            for y in range(0, SCREENRECT.height, bgdtile.get_height()):
                background.blit(bgdtile, (x, y))
        screen.blit(background, (0,0))
    #main game loop
    done = 0
    pygame.display.update()
    NoMoreStars = False
    while not done:
        kill_stars(screen, stars, white) # delete
                # this code just make a black rectangle:
        ##oground = pygame.Surface(Rect(MINX
                ##          ,MINY,MAXX-MINX,MAXY-MINY).size)
                ##screen.blit(oground, (MINX,MINY))

MINX, MINY, MAXX, MAXY = move_stars(stars, NoMoreStars)
draw_stars(screen, stars, black) # draw new
pygame.display.update(MINX,MINY, MAXX-MINX, MAXY-MINY)


##pygame.display.update()

Not sure, but I think your problem is that "kill_stars" function. Why would you kill stars? They're shiny! More to the point, it looks like you're drawing the background once, then drawing stars, then trying to delete the stars to restore the background behind them.


What I would do is blit the background _every frame_, and then blit the stars atop that. Hm, it also looks like you're building the background by drawing a bunch of tiles. It would be fastest if you did that once, into a background surface -- as you're doing -- and then blitted that entire, pre-drawn surface to the screen each frame. It's faster than drawing tiles individually, and you can even get smooth scrolling that way if you make a background bigger than the screen and blit only a screen-sized rectangle.

So: blit the background every frame, then the stars atop that. If you really want, you can "kill the stars" by blitting pixels from the background surface, but that's probably slower than blitting the whole thing. Does this help?

Kris