Question

Any ideas as to why it won't change image to IMG_1? Is it because the variable is declared in the main function?

from pygame import *
from pygame.locals import *
import pygame
import time
import os

def main():
   while 1:
      #search for image
      imageCount = 0 # Sets Image count to 0
      image_name = "IMG_" + str(imageCount) + ".jpg" #Generates Imagename using imageCount
      picture = pygame.image.load(image_name) #Loads the image name into pygame
      pygame.display.set_mode((1280,720),FULLSCREEN) #sets the display output
      main_surface = pygame.display.get_surface() #Sets the mainsurface to the display
      main_surface.blit(picture, (0, 0)) #Copies the picture to the surface
      pygame.display.update() #Updates the display
      time.sleep(6); # waits 6 seconds
      if os.path.exists(image_name): #If new image exists
         #new name = IMG + imagecount
         imageCount += 1
         new_image = "IMG_" + str(imageCount) + ".jpg"
         picture = pygame.image.load(new_image)


if __name__ == "__main__":
    main()      
Was it helpful?

Solution

You reset imageCount when it loops. pygame doesn't switch to the other image because it's immediately replaced.

Also, you check if the current image exists, then try to move to the next without checking whether that exists.

Instead, try:

def main(imageCount=0): # allow override of start image
    while True:
        image_name = "IMG_{0}.jpg".format(imageCount)
        ...
        if os.path.exists("IMG_{0}.jpg".format(imageCount+1)):
            imageCount += 1

OTHER TIPS

Your game loop starts with assigning 0 to imageCount, so on every iteration you are loading the 0 index image. Put imageCount = 0 above your while loop start:

def main():
   imageCount = 0 # Sets Image count to 0
   while 1:
      image_name = "IMG_" + str(imageCount) + ".jpg"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top