1
I have a question related to Pygame.
I’m starting to move and I found a problem here. When I press the button A(left) I make the image walk to the left, until then everything ok. But if I hold down the key it does not repeat the movement. I have to drop and press again the key.
Another thing, as I put a Sleep of 0.07, if I press the X key times in this time interval, it will repeat this movement X times, even if not with the key pressed.
Link to the Github: https://github.com/EmanoelFraguas/GitProject/tree/master/Python/Pok%C3%A9%20Engine
I imagine it must be a very silly problem, but forgive me. I’m just learning and I’m curious about these things. I even learned how to work Github haha :d
Code of my class:
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)
Code of implementation:
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)
About the
time.sleep
is because I make an animation, that every 0.07 I move 8 pixels to the side the coordinates of the image, and to each one I change the.png
to move the character’s legs giving movement.get_pressed
I’ll do a search here. .– Manolloz
I just got here with the
get_pressed()
:press = pygame.key.get_pressed()
 if press[119] == 1:
 arca.moveUp()
when I leave pressed this way it keeps moving.– Manolloz
it is, but do not use "119", use the same constants you use when checking the event: K_w, and etc... -- as someone, or yourself, seeing your code in two weeks, you will know that the "119" is corresponding to the "w" key"?
– jsbueno