I'm using pygame in order to create a sort of animation. What I have in mind is to have a series of background images change in relation to the time that has passed once I initiate the game. I came up with this code to do so:
while True:
DISPLAYSURF.fill(black) #fills the displaysurf with black
for time in pygame.time.get_ticks():
if time == 2:
img_1 = img_2
DISPLAYSURF.blit(img_2, (0, 0)) #copy, pastes the surface object to fixed point
if time == 4:
img_2 = img_3
DISPLAYSURF.blit(img_3, (0, 0))
if time == 8:
img_3 = img_4
DISPLAYSURF.blit(img_4, (0, 0))
if time == 10:
img_4 = img_5
DISPLAYSURF.blit(img_5, (0, 0))
if time == 12:
img_5 = img_6
DISPLAYSURF.blit(img_6, (0, 0))
if time == 14:
img_6 = img_7
DISPLAYSURF.blit(img_7, (0, 0))
if time == 16:
img_7 = img_8
DISPLAYSURF.blit(img_8, (0, 0))
pygame.display.flip()
clock.tick(FPS)
What I received back when I ran the program was "'int' object is not iterable" which made me think that I may not be able to do what I had in mind, because the images I have are classified in Pygame as surface objects. I'm thinking of two things:
-->Is it possible to create a function that can re-upload the images in relation to time by somehow converting the surface objects' type?
-->Is my code even reflecting what I want it to do?
Please let me know and fire away with the critiques! I'm very new to coding, so any feedback is helpful!
@Aggragoth has covered the error message already, so I wont go into that.
One way to periodically change the background is to keep a timer, and adjust the background based on a predefined period.
The main part of the code is to keep the time we last changed the background. If the elapsed time since then (from
pygame.time.get_ticks()
) is greater than the last-change-time plus a delay, then change to the next background.In this example I've just used colours, but the
backgrounds[]
list could also hold images, and be used withwindow.blit()
.