I have a question regarding Pygame.
I'm starting to fiddle and I found a problem here. When I press the A (left) button, I move the image to the left, until everything is ok. But if you keep the key pressed it does not repeat the movement. I have to depress and press the key again.
Another thing, as I put a sleep of 0.07, if I press the X key in that time interval, it will repeat this movement X times, even if the key is not pressed.
GitHub link: link
I suppose it must be a pretty silly problem, but forgive me. I'm just learning and I'm curious about these things. I already learned how to play GitHub haha: d
My class code:
class Arcanine(object):
def __init__(self, name):
self.name = name
def moveLeft(self):
for i in range(4):
if i % 2 == 0:
pygame.display.flip()
screen.fill(0)
screen.blit(mapa, mappos)
arcapos[0] = arcapos[0] - 8
screen.blit(arca1, arcapos)
time.sleep(0.07)
else:
pygame.display.flip()
screen.fill(0)
screen.blit(mapa, mappos)
arcapos[0] = arcapos[0] - 8
screen.blit(arca2, arcapos)
time.sleep(0.07)
Execution code:
while True:
pygame.display.flip()
screen.fill(0)
screen.blit(mapa, mappos)
screen.blit(arca2, arcapos)
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == K_w:
keys[0] = True
elif event.key == K_s:
keys[1] = True
elif event.key == K_a:
keys[2] = True
elif event.key == K_d:
keys[3] = True
if event.type == pygame.KEYUP:
if event.key == K_w:
keys[0] = False
elif event.key == K_s:
keys[1] = False
elif event.key == K_a:
keys[2] = False
elif event.key == K_d:
keys[3] = False
if keys[0]:
pass
elif keys[1]:
pass
if keys[2]:
arca.moveLeft()
elif keys[3]:
pass
if event.type == pygame.QUIT:
pygame.quit()
exit(0)