Pygame不会切换到下一张图片

时间:2014-02-23 10:08:57

标签: python pygame slideshow raspberry-pi pygame-surface

有关为何不将图像更改为IMG_1的任何想法?是因为变量是在main函数中声明的吗?

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

2 个答案:

答案 0 :(得分:0)

您的游戏循环首先将0分配给imageCount,因此在每次迭代时您都要加载0索引图像。将imageCount = 0置于while循环开始之上:

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

答案 1 :(得分: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