You're only updating 5 frames a second, so yu should expect delays of up to 0.2s, and if you tap the key fast enough it won't register. Why not change the 5 to 50 or something?
-Christopher
Hi everyone,
I apologize in advance for posting 43 lines of code, but I can't figure out
where on Earth the trouble is coming from.
When I run this code, the keys do exactly what I'd like, but I'm noticing a
delay of a few tenths of a second between when I press the key and when the
sound of the step is played. Further, sometimes when I tap an arrow key
quickly, the player won't even take a step.
Can anyone please tell me what I'm doing wrong? This looks sound to me.
import time
import pygame
from sound_lib.stream import FileStream
from sound_lib.output import Output
class Player(object):
def __init__(self):
self.x = 10
self.step = FileStream(file="sounds/step.ogg")
self.step.pan = 0
self.step_time = 0.25
self.last_step_time = 0.0
def move(self, dir):
if time.time() - self.last_step_time <= self.step_time:
return
if self.x + dir < 1 or self.x + dir > 20:
return
self.x += dir
if dir > 0:
self.step.pan += 0.1
else:
self.step.pan -= 0.1
self.step.play(True)
self.last_step_time = time.time()
def main():
clock = pygame.time.Clock()
o = Output()
guy = Player()
screen = pygame.display.set_mode((640, 400))
while(True):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
guy.move(-1)
if keys[pygame.K_RIGHT]:
guy.move(1)
for event in pygame.event.get(): pass
clock.tick(5)
if __name__ == '__main__':
main()
Thanks much,
Ryan