I have a cloud class and I have a variety of cloud shapes so I need to associate each variation to its corresponding shadow in order to display it underneath it.
But I tried creating a dictionary with the clouds' images as keys and their shadows as values, I also created to have the clouds and shadows each in a separate list and then used the for
loop to iterate over them and associate them but none of that worked.
I was facing an error which said TypeError: 'pygame.Surface' object is not iterable
class Cloud(object):
def __init__(self:
imgs = random.choice([pygame.image.load(r'C:\Users\Salem\Documents\MyGame1\testcloud1.png').convert_alpha(), pygame.image.load(r'C:\Users\Salem\Documents\MyGame1\testcloud2.png').convert_alpha(),
pygame.image.load(r'C:\Users\Salem\Documents\MyGame1\testcloud3.png').convert_alpha()])
shadows = [pygame.image.load(r'C:\Users\Salem\Documents\MyGame1\cloud_shadow.png').convert_alpha(), pygame.image.load(r'C:\Users\Salem\Documents\MyGame1\testcloud2shadow.png').convert_alpha(),
pygame.image.load(r'C:\Users\Salem\Documents\MyGame1\testcloud3shadow.png').convert_alpha()]
self.x = random.randrange(-1500, -500)
self.y = random.randrange(-100, 400)
for img in imgs:
for shadow in shadows:
i = random.randrange(len(imgs))
self.img = random.choice(self.imgs[i])
self.shadow = self.shadow[i]
So what can i do instead ?
I just need to associate the clouds' images to their shadows, but I know how to display each object along with its corresponding shadow.
Btw nevermind my messy file names ^^
Thanks in advance!
Create a list of clouds and a list of shadows:
imgs = [pygame.image.load(r'C:\Users\Salem\Documents\MyGame1\testcloud1.png').convert_alpha(),
pygame.image.load(r'C:\Users\Salem\Documents\MyGame1\testcloud2.png').convert_alpha(),
pygame.image.load(r'C:\Users\Salem\Documents\MyGame1\testcloud3.png').convert_alpha()]
shadows = [pygame.image.load(r'C:\Users\Salem\Documents\MyGame1\cloud_shadow.png').convert_alpha(),
pygame.image.load(r'C:\Users\Salem\Documents\MyGame1\testcloud2shadow.png').convert_alpha(),
pygame.image.load(r'C:\Users\Salem\Documents\MyGame1\testcloud3shadow.png').convert_alpha()]
zip
the lists and choose a random element:
self.img, self.shadow = random.choice(list(zip(imgs, shadows)))
zip
takes two collections and merges them into a single collection, where each item is a tuple of an item from each collection.