Elizabeth Frazier
Elizabeth Frazier

Reputation: 13

PyGame - Unable to blit one surface onto another

I am simply trying to create two Surfaces, fill them and then blit one onto the other. However, the second Surface never renders on top of the first. If I blit the second surface onto the display Surface, it renders fine. Not sure if there is a restriction on layering surfaces (other than the display) on top of each other. Here is my code:

import pygame, sys
from pygame.locals import *

pygame.init()

windowSurface = pygame.display.set_mode((500, 400), 0, 32)
pygame.display.set_caption('Hello world!')
windowSurface.fill((255,255,255))

layer1 = pygame.Surface((100,100))
layer1.fill((0,255,0))

layer2 = pygame.Surface((50,50))
layer2.fill((255, 0, 0))

windowSurface.blit(layer1, (0,0))
layer1.blit(layer2, (0,0))

pygame.display.flip()

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

Thanks for any suggestions!

Upvotes: 1

Views: 1511

Answers (1)

cyman
cyman

Reputation: 70

Your problem is that you have layer1.blit(layer2, (0,0)) after windowSurface.blit(layer1, (0,0)) which means you're blitting layer2 to layer1 after layer1 is already done blitting to the window. All you need to do is cut layer1.blit(layer2, (0,0)) and paste it ABOVE windowSurface.blit(layer1, (0,0)) so that it executes first.

Upvotes: 3

Related Questions