Seb St Johnston
Seb St Johnston

Reputation: 25

Python pygame blit the same image multiple times

Why cant my program blit the same image that it loaded multiple times?

Imagea = pygame.image.load('imagea.png')
Imageb = pygame.image.load('imageb.png')

Inside gameloop

deck = abaa
for i in deck:
     for position in positions:
      if(i) == deck[0]:
           if(i == a):
           gamedisplay.blit(imagea, positions[0])
           elif(i == b):
           gamedisplay.blit(imageb, positions[0])
      if(i) == deck[1]:
           if(i == a):
           gamedisplay.blit(imagea, positions[1])
           elif(i == b):
           gamedisplay.blit(imageb, positions[1])
      if(i) == deck[2]:
           if(i == a):
           gamedisplay.blit(imagea, positions[2])
           elif(i == b):
           gamedisplay.blit(imageb, positions[2])
      if(i) == deck[3]:
           if(i == a):
           gamedisplay.blit(imagea, positions[3])
           elif(i == b):
           gamedisplay.blit(imageb, positions[3])

What seems to occur is only deck 0 and deck 1 show imagea and imageb. However, deck 2 and 3 does not show up at position[2] or position[3].

Upvotes: 0

Views: 853

Answers (1)

Rabbid76
Rabbid76

Reputation: 210928

deck is a list and i is an element of the list. It is not necessary to evaluated if is an element of the list, of course it is. This evaluation is the issue. Note, if i is multiple times in deck (e.g. at index 0 and 2), then if i == deck[2]: is never evaluated, because if i == deck[o]: is evaluated True first.

In the following, I assume that deck and position have the same number of elements.

Either use enumerate to traverse deck and get a tuple contaning the index of the element and the element itself:

for i, d in enumerate(deck):
    p = positions[i]
    if d == a:
       gamedisplay.blit(imagea, p)
    elif d == b:
       gamedisplay.blit(imageb, p)

Or use zip to traverse deck and positions simultaneously:

for d, p in zip(deck, positions):
    if d == a:
       gamedisplay.blit(imagea, p)
    elif d == b:
       gamedisplay.blit(imageb, p)

Upvotes: 0

Related Questions