2

This is a small section of my pygame code. I am trying to get a random Image to be loaded. In the text file I have 10 different paths to different images.

random_number = random.randint(0,9)
file = open('ImagePath.txt','r')
paths = file.readlines()
path = paths[random_number]
Img = pygame.image.load(path)
Display.blit(Img, (20,20))

When I run the code it says the image cannot be accessed and that the error is found on the line:

Img = pygame.image.load(path)

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
zxckv
  • 29
  • 1
  • 8
    Probably, you are using relative paths and they are probably not relative to your script. We can only guess if you don't provide more info. For example what does `print(path)` give. – nauer Mar 23 '20 at 20:03
  • @zxckv there are many problems, check my answer below to ensure your providing `pygame.image.load()` the correct relative image path. – Tanner Dolby Mar 23 '20 at 20:13
  • I am voting to close this question as it is unclear what is being asked about and there is no [**Minimal**, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – Rabbid76 Jan 03 '21 at 10:21

0 Answers0