Is there a way to display the same picture multiple times in different sizes?

Viewed 150
image1 = pygame.image.load('Insel.jpg')
image1 = pygame.transform.scale(image1, (200, 150))

image2 = pygame.image.load('Insel.jpg')
image2 = pygame.transform.scale(image2, (250, 200))

image3 = pygame.image.load('Insel.jpg')
image3 = pygame.transform.scale(image1, (150, 100))

multi_Images = []
multi_Images_pos = []

image1 = pygame.image.load('Insel.jpg')

for i in range(5):
    multi_Images.append(image1)
    pos = [random.randint(0, 480+1), random.randint(0, 480+1)]
    multi_Images_pos.append(pos)

This is my code soo far and I want to create the same image randomly multiple times and in 3 different size but this code doesn't work. I don't want to load the picture over and over again. I'm quite new to programming but I know that this doesn't look nice.

2 Answers

I don't want to load the picture over and over again

Load the image only once and create multiple scaled images:

image = pygame.image.load('Insel.jpg')

image1 = pygame.transform.scale(image, (200, 150))
image2 = pygame.transform.scale(image, (250, 200))
image3 = pygame.transform.scale(image, (150, 100))

I recommend to store the images in a list instead of 3 separate variables:

image = pygame.image.load('Insel.jpg')
image_list = [
    pygame.transform.scale(image, (200, 150))
    pygame.transform.scale(image, (250, 200))
    pygame.transform.scale(image, (150, 100))]

Finally you should put the code in a function:

def load_and_scale(imagename):
    image = pygame.image.load(imagename)
    return [
        pygame.transform.scale(image, (200, 150))
        pygame.transform.scale(image, (250, 200))
        pygame.transform.scale(image, (150, 100))]
image_list = load_and_scale('Insel.jpg')

Use random.choice to select a random image form the list:

image_list = load_and_scale('Insel.jpg')

multi_Images = []
for i in range(5):
    image = random.choice(image_list)
    pos = [random.randint(0, 480+1), random.randint(0, 480+1)]
    multi_Images.append((image, pos)) 

You can draw the images using a loop:

for image, pos in multi_Images:
    screen.blit(image, pos)

If you are new to programming, why start from making a game? Learn the basics first. It is like a first lesson for a doctor would be brain surgery.

multi_Images.append(image1). Your issue is you keep appending the same exact image.
Also, why does image1 = pygame.image.load('Insel.jpg') happen twice?

I am not familiar with pygame but this would probably do what you need.

image_source = []
img = pygame.image.load('Insel.jpg')

image_source.append(pygame.transform.scale(img, (200, 150)))
image_source.append(pygame.transform.scale(img, (250, 200)))
image_source.append(pygame.transform.scale(img, (150, 100)))

multi_Images = []
multi_Images_pos = []

for i in range(5):
    choice = random.randint(0, 2)
    multi_Images.append(image_source[choice])
    pos = [random.randint(0, 480+1), random.randint(0, 480+1)]
    multi_Images_pos.append(pos)
Related