2014-02-23 22 views
-1

任何想法爲什麼它不會將圖像更改爲IMG_1?是否因爲變量是在主函數中聲明的?Pygame不會切換到下一個圖像

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

回答

0

您在循環時重置imageCountpygame不會切換到其他圖像,因爲它會立即被替換。

此外,您檢查當前圖像是否存在,然後嘗試移動到下一個,而不檢查是否存在。

相反,嘗試:

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 
0

你的遊戲循環開始一起imageCount分配0,所以在每次迭代要裝入的0索引圖像。將imageCount = 0放在while循環開始之上:

def main(): 
    imageCount = 0 # Sets Image count to 0 
    while 1: 
     image_name = "IMG_" + str(imageCount) + ".jpg"