'Why is my layered updates not working on my sprites?
I haver tried updating and drawing the layered_group but still deos not work. I have read a couple of tutorials and as far as I can tell my following code should work but I am missing something...
these are the groups and instances...
self.layered_group = pygame.sprite.LayeredUpdates()
self.visible_sprites = CameraGroup(self.current_level)
self.active_sprites = pygame.sprite.Group()
self.obstacle_sprites = pygame.sprite.Group()
self.exit_sprite_1 = pygame.sprite.Group()
for style, layout in layouts.items():
for row_index, row in enumerate(layout):
for col_index, col in enumerate(row):
if col != '-1':
x = col_index * TILESIZE
y = row_index * TILESIZE
if style == 'blocks':
Tile((x,y), [self.visible_sprites, self.obstacle_sprites], 'tile')
if style == 'entrances':
if col == str(self.entry_pos):
self.player = Player((x, y), [self.visible_sprites, self.active_sprites, self.layered_group], self.obstacle_sprites, self.layered_group, 1)
if style == 'exits':
if col == '1':
sprite = ExitDoor((x,y - TILESIZE // 2), [self.visible_sprites, self.active_sprites], self.layered_group,0, 'door')
self.exit_sprite_1.add(sprite)
And in the class....
class Player(pygame.sprite.Sprite):
def __init__(self, pos, groups, obstacle_sprites, layered_group, layer):
self._layer = layer,
super().__init__(groups)
run method only updates the visible sprite group as below, is this why? My print troubleshooting shows all ordered correctly when spawned on each level and it runs. Do I have to incorporate this self.layered_group into my run method? If so I am struggling as everything is in visible group!!
def run(self):
self.layers_group.update()
print(self.layers_group.layers())
if self.game_paused:
self.pause.run()
else:
self.input()
self.active_sprites.update()
self.visible_sprites.offset_draw(self.player)
Solution 1:[1]
You can the layer the Sprite in a LayeredUpdates Group is associated with change_layer change:
layered_group = pygame.sprite.LayeredUpdates()
layered_group.change_layer(sprite, layer)
However, the Sprites are only drawn in the correct order if you call draw on the LayeredUpdates Group_ object. Adding Sprites to a LayeredUpdates Group, don't magically change the order of the Spirtes in another Group. You have to call
layered_group.draw(screen)
See also pygame.sprite.LayeredUpdates.move_to_front() does not work
Solution 2:[2]
Solved. As the player is passed into the draw method of the camera group class, I could seperate the player from all other sprites and blit the player after the others as follows... let me know if this can be tidied!
for sprite in self.sprites():
if sprite != player:
offset = sprite.rect.topleft - self.offset
self.display_surf.blit(sprite.image, offset)
for sprite in self.sprites():
if sprite == player:
offset = sprite.rect.topleft - self.offset
self.display_surf.blit(sprite.image, offset)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Matt |
