I\'ve been working on a small project in pygame. It\'s an overhead 2D game, but if a player is behind an object, I need the player to be one layer behind it. If the player is in
Stop working with Surfaces
directly. Use the pygame's Sprite
class.
Then you can simply give your Sprites a _layer
attribute and use a LayeredUpdates group for managing them, and the group will take care of the order of blitting.
Here's an example:
import pygame
pygame.init()
screen = pygame.display.set_mode((300, 300))
class Actor(pygame.sprite.Sprite):
def __init__(self, group, color, layer, pos):
self.image = pygame.Surface((30, 30))
self.image.fill(color)
self.rect = self.image.get_rect(center=pos)
self._layer = layer
pygame.sprite.Sprite.__init__(self, group)
group = pygame.sprite.LayeredUpdates()
Actor(group, (255, 255, 255), 0, (100, 100))
Actor(group, (255, 0, 255), 1, (110, 110))
Actor(group, (0, 255, 255), 0, (120, 120))
Actor(group, (255, 255, 0), 3, (130, 130))
Actor(group, (0, 0, 255), 2, (140, 140))
run = True
while run:
for e in pygame.event.get():
if e.type ==pygame.QUIT:
run = False
screen.fill((0,0,0))
group.update()
group.draw(screen)
pygame.display.flip()
Note how the order of the sprites is determined by the value of the _layer
attribute.