python - pygame I can move my character but not greatly -
i making game pygame, when change direction, stop , have press arrow key again. doesn't seem big problem, annoying easily.
this code (it's written in game's loop, not problem):
if event.type == pygame.keydown: if event.key == k_left: coordsx = coordsx - 0.1 elif event.key == k_right: coordsx = coordsx + 0.1 elif event.key == k_up: coordsy = coordsy - 0.1 elif event.key == k_down: coordsy = coordsy + 0.1 elif event.key == k_left , event.key == k_up: coordsx = coordsx - 0.1 coordsy = coordsy - 0.1 elif event.key == k_left , event.key == k_down: coordsx = coordsx - 0.1 coordsy = coordsy + 0.1 elif event.key == k_right , event.key == k_up: coordsx = coordsx + 0.1 coordsy = coordsy - 0.1 elif event.key == k_right , event.key == k_down: coordsx = coordsx + 0.1 coordsy = coordsy + 0.1
for case, don't care when keys pressed or order, want know keys pressed. instead, might want @ pygame.key.get_pressed.
the main loop more this
pygame.event.pump() keys = pygame.key.get_pressed() if keys[k_left]: coordsx -= 0.1 if keys[k_right]: coordsx += 0.1 ...
also, skip using elif here , conditions take care of themselves.
Comments
Post a Comment