I have got a json file and I am trying to load the single sprites from that file but I gives this error after a few loops and also the animation is not smooth(gets stuck) error:
Traceback (most recent call last):
File "test.py", line 18, in <module>
screen.blit(image, (0, 50))
TypeError: argument 1 must be pygame.Surface, not None
This is the json file I am using:
{
"textureAtlas": {
"texture": "Run (32x32).png",
"regionWidth": 32,
"regionHeight": 32
},
"cycles": {
"animation0": {
"frames": [0,1,2,3,4,5,6,7,8,9,10,11]
}
}
}
The script for the animation:
import json
pygame.init()
clock=pygame.time.Clock()
c=0
def lcAnim(sfile):
global c
with open(sfile) as f:
data=json.load(f)
img=pygame.image.load(data['textureAtlas']['texture'])
while c<len(data['cycles']['animation0']['frames']):
img.set_clip(pygame.Rect(data['cycles']['animation0']['frames'][c-1]*32,0,32,32)) # Locate the sprite you want
draw_me = img.subsurface(img.get_clip()) # Extract the sprite you want
c =c+1
print(c)
return draw_me
c=0
and the main script:
import pygame
import lcanim #name of the animation script
(width,height)=(300,200)
clock=pygame.time.Clock()
screen=pygame.display.set_mode((width,height))
pygame.display.flip()
running=True
while running:
image = lcanim.lcAnim('run.sf') #name of json file
for event in pygame.event.get():
if event.type==pygame.QUIT:
running=False
screen.blit(image, (0, 50))
#screen.blit(alimg,(-10,10))
pygame.display.update()
screen.fill((255,255,255))
clock.tick(10)
pygame.quit()
Here's a video showing the problem:
https://youtu.be/8IeaqVCxvms (sorry other video sharing services were not working)