-1

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()      
4

2 回答 2

0

您的游戏循环从将 0 分配给 开始imageCount,因此在每次迭代时,您都在加载 0 索引图像。将 imageCount = 0 放在 while 循环开始的上方:

def main():
   imageCount = 0 # Sets Image count to 0
   while 1:
      image_name = "IMG_" + str(imageCount) + ".jpg"
于 2014-02-23T10:11:48.027 回答
0

imageCount当它循环时你重置。pygame不会切换到其他图像,因为它会立即被替换。

此外,您检查当前图像是否存在,然后尝试移动到下一个而不检查它是否存在。

相反,请尝试:

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
于 2014-02-23T10:14:38.560 回答