I am hitting an issue after writing a small program with pygame. The program takes split .GIF animations and loads the images(frames of the gif) via:
pygame.image.load(filename)
This returns a pygame surface object which is then appended to an array. The program loads about 15 frames to an array, using 6 arrays in total.
The problem I am having is when accepting input through my while loop. The loop plays its idle animation and running animation fine, however, on accepting input from the keyboard (grabbing input from pygame's event list...)
for event in pygame.event.get():
via pygame.KEYDOWN
There is very noticeable lag, making for unresponsive animation set switching. If I am to make a game using this method, it will have to be fixed. I'm sure my code is inefficient, but it should suffice not to create pauses. Any help would be fantastic.
My guess? pygame.clock.tick()
is creating some sort of event lag, but I am unsure as to how to go about fixing this, if event lag is even the case.
Here is the loop that I suspect to be problematic:
while running == 2:
pygame.display.flip()
#mouse = pygame.mouse.get_pos()
#events = pygame.event.get()
#(pygame.QUIT, pygame.KEYDOWN, pygame.KEYUP)
for event in pygame.event.get():
#event = pygame.event.wait()
if event.type == pygame.QUIT:
sys.exit(0)
elif event.type == pygame.KEYDOWN:
print event.key
wait = 0
if event.key == pygame.K_d:
tmpcache = wr
lastkey = "wr"
elif event.key == pygame.K_a:
tmpcache = wl
lastkey = "wl"
elif event.type == pygame.KEYUP:
wait = 1
if lastkey == "wr":
tmpcache = sr
elif lastkey == "wl":
tmpcache = sl
if wait == 1:
for frame in tmpcache:
screen.blit(test, (0,0))
screen.blit(frame, (currentchar.posx, currentchar.posy))
pygame.display.flip()
clock.tick(charfps)
else:
for frame in tmpcache:
screen.blit(test, (0,0))
screen.blit(frame, (currentchar.posx, currentchar.posy))
pygame.display.flip()
clock.tick(charfps)
some variables not shown here, but are used:
charfps = 30
currentchar.posx, currentchar.posy
are both tuples set at (300, 240)