关于为什么它不会将图像更改为IMG_1的任何想法?是因为变量是在主函数中声明的吗?
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()
当它循环时,您可以重置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
您的游戏循环从将 0 分配给 imageCount
开始,因此在每次迭代中,您都会加载 0 索引图像。将 imageCount = 0 放在循环开始上方:
def main():
imageCount = 0 # Sets Image count to 0
while 1:
image_name = "IMG_" + str(imageCount) + ".jpg"