Change image AND image position by using loops [python]
Question:
I’m trying to print out images that correspond to a list of names.
I have the following code already:
for item in player_hand:
screen.blit(pygame.image.load(os.path.join('Images',item.name+'.png')), (100, 100))
I want to update the tuple (100,100) so that each new image gets printed to the right of the previous image.
What’s the simplest way to accomplish this?
Answers:
Use enumerate
to introduce an increasing variable to the loop. For instance
for n, item in enumerate(player_hand):
screen.blit(pygame.image.load(os.path.join('Images',item.name+'.png')), (100+n*10, 100))
like this?
count=0
for item in player_hand:
screen.blit(pygame.image.load(os.path.join('Images',item.name+'.png')), (100+count, 100))
count=count+distance
where distance is how much on the right you need it
this way is the best because you can change how much to move the image each time the loop is repeated by adding the value of an array! (e.g: if you need different distances or have imges of different size)
I’m trying to print out images that correspond to a list of names.
I have the following code already:
for item in player_hand:
screen.blit(pygame.image.load(os.path.join('Images',item.name+'.png')), (100, 100))
I want to update the tuple (100,100) so that each new image gets printed to the right of the previous image.
What’s the simplest way to accomplish this?
Use enumerate
to introduce an increasing variable to the loop. For instance
for n, item in enumerate(player_hand):
screen.blit(pygame.image.load(os.path.join('Images',item.name+'.png')), (100+n*10, 100))
like this?
count=0
for item in player_hand:
screen.blit(pygame.image.load(os.path.join('Images',item.name+'.png')), (100+count, 100))
count=count+distance
where distance is how much on the right you need it
this way is the best because you can change how much to move the image each time the loop is repeated by adding the value of an array! (e.g: if you need different distances or have imges of different size)