I was working on making a animated background for my game. So I have the frames for it there are 174 ans store them in a list in a class. when I run the animate function in the class it only displays one image then dosnt do anything. I figured it is because of the for loop that is putting the images on the screen. How can I improve this so it can work properly?
basePath = os.path.dirname(sys.argv[0])+"\\"
pygame.init()
#setting fps lock for game and startign clock (Both are needed for fps)
fps = 30
fClock = pygame.time.Clock()
#Setting up pygame display size and window
res = pygame.display.Info()
tv = pygame.display.set_mode((res.current_w, res.current_h), pygame.SCALED)
class Background:
def __init__(self):
self.imFrames = os.listdir(basePath+"Background Frames/")
self.nTime = 0
def animate(self):
index = 0
tNow = pygame.time.get_ticks()
if (tNow > self.nTime):
fDelay = 0.1
self.nTime = tNow + fDelay
index += 1
if index >= len(self.imFrames):
index = 0
for item in self.imFrames:
tv.blit(pygame.image.load(basePath+"Background Frames/"+item).convert_alpha(), (0,0))
back = Background()
go = True
while go:
#event checks
for event in pygame.event.get():
if event.type == QUIT:
go = False
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
go = False
back.animate()
pygame.display.update()
fClock.tick(fps)
#Closing pygame and closing all functions
pygame.quit()
sys.exit()