How to resolve character overlays (sprites) on screen with Pygame

Asked

Viewed 274 times

-1

I am developing a TOP-DOWN game with several enemies, however, due to the order in which they are plotted on the screen, no matter if one is behind the other, always the one that was plotted last will have their Sprite drawn over the others.

I’d like to know if there’s a kind of third axis beyond the X and Y, so I can adjust in case the further south the enemy is, means the closer he is, and therefore the Prits should be above the others, as well as the further north, farther away you will be and your Sprite should be below the others.

It follows an excerpt in which through a loop I create 20 instances of the Enemies class.

i = 0
waves = 0
enemies = []
for x in range(20):
      inimigo = "e"+str(i)
      inimigo = Inimigo(player)
      enemies.append(inimigo)
      i += 1

And below follows excerpt I use inside the game’s "While True":

  for e in enemies:
        e.draw()

I think because of the above, enemies are plotted completely independently of each other. So they have no "consciousness" of each other, and end up having the superscripted sprites by the ones that were drawn last.

What I need is for there to be a way to control the depth of the screen, so that regardless of the order in which they were drawn, the one further south is ahead.

I hope I explained it well.

The execution of the game can be seen in https://www.instagram.com/p/B7j37-Bh_Jn/ and the complete code is in https://github.com/Nathanbahia/Exemplo-Pygame

  • One of the espcialized classes of Pygame Group allows a control like this - but without you putting a minimal example and working of how your code is, no one will answer - since you would have to write a whole program from scratch just to demonstrate this.

  • In other words: please edit the question and include a minimum and complete (working alone) example of how it is, or the question will be closed.

2 answers

1

def desenharSobre(sobreposicao, character, terrp, pos_x, pos_y):
    if sobreposicao == True:
        screen.blit(spritesCenario['home'], (0, 0))
        screen.blit(terrp, (100, 100))
        screen.blit(character, (pos_x, pos_y))
    else:
        screen.blit(spritesCenario['home'], (0, 0))
        screen.blit(character, (pos_x, pos_y))
        screen.blit(terrp, (100, 100))

def sobreposicao(pos_x, pos_y):
    for i in range(100, 165):
        if pos_x == 100 and pos_y == i:
            return True
    return False 

and before the update or flip on the main loop you put:

desenharSobre(sobreposicao(pos_x, pos_y), character, terrp, pos_x, pos_y)

pos_x and pos_y is the x and y of the character

Character is the controllable character

terrp the character not controllable

range is up to what position in y the controllable character overlaps with each other

0

Ah - with code it is easier. Although it is incomplete.

The class Inimigo must inherit from pygame.sprite.Sprite, and instead of a common list, you should add the Inimigo in a group - a class derived from pygame.Sprite.Group - but in case, use specifically the class LayeredUpdates (https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.LayeredUpdates )

If everything else is right, when you call the method draw group (and not each Prite separately), it will draw in order. If it is not the desired order, this group allows you to change the sprites of "layers" and you will have the desired control.

  • I fixed the link, sorry. And thank you so much for your help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.