I have a png file containing 4 frames of a sprite animation sequence, aligned horizontally.
By checking the forum I found out how to display each frame separately from the same image, by using subtexture(rect), so this isn’t the problem here.
I want to run a loop during the game screen, which iterates through each frame in the list of image textures taken from the sprite sheet, every 0.125 seconds, so that the sprite is animated at 8 frames per second. This should not prevent other parts of the program from running at the default 60 FPS.
I do not want to use any functions which pause the entire program, especially not the sleep function from the time module! This is because the sleep function pauses my program and makes everything stop working every few seconds, which is annoying and pointless.
My sprite’s position is constantly at the centre of the screen, so animation by number of pixels moved (like in the Tutorial Part 3 file in the Game Tutorial folder) will not help.
Here is my code so far:
from scene import *
sprite_sheet = [
Texture('IMG_0227.png').subtexture(Rect(0,0,0.25,1)),
Texture('IMG_0227.png').subtexture(Rect(0.25,0,0.25,1)),
Texture('IMG_0227.png').subtexture(Rect(0.5,0,0.25,1)),
Texture('IMG_0227.png').subtexture(Rect(0.75,0,.25,1)),
]
class MyScene (Scene):
def setup(self):
self.screen="game"
self.background_color = 'black'
self.sprite = SpriteNode(sprite_sheet[0],
scale = 1,
position = self.size / 2,
parent = self)
self.add_child(self.sprite)
n=0
for sprite in sprite_sheet: #i want this to loop infinitely but only during the game screen. i don't want to use while because it crashes the app, probably because the images keep loading and appearing infinitely?
self.sprite.texture = sprite_sheet[n] #my attempt at iterating through the list of sprite frames.
n=n+1
'''wait 0.125 seconds to make sprite change image at 8 frames per second, to make an animation'''
run(MyScene())
I would appreciate any help, since this is a key part of making a game with working animations.