Sandman
Sandman

Reputation: 73

How do I get a random image from a group of images?

How do I get an image from a group of images in pygame sprites? So far I got to this:

for i in range(1,3):
    img = pygame.image.load("house"+str(i)+".png").convert()
    img.set_colorkey(white)
    self.images.append(img)

But I'm not quite sure how to randomly collect an image from a list of images. How do I do this? And if possible how do I make it a chance of an image from that list of images like 1/10 of a chance that image will appear on the pygame window? Python 2.6, Pygame, windows 7

Upvotes: 2

Views: 2961

Answers (2)

Neel
Neel

Reputation: 21243

Use random module of python and do your choice from the available options.

Upvotes: 0

wim
wim

Reputation: 362756

You seem to have a list of images bound to the name self.images.
If you want to randomly select one of them:

import random

random_image = random.choice(self.images)

Upvotes: 3

Related Questions