[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Substantial Lag

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?


On Feb 12, 2012 8:02 PM, "Ryan Strunk" <ryan.strunk@xxxxxxxxx> wrote:
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:
               if self.x + dir < 1 or self.x + dir > 20:
               self.x += dir
               if dir > 0:
                       self.step.pan += 0.1
                       self.step.pan -= 0.1
               self.last_step_time = time.time()

def main():
       clock = pygame.time.Clock()
       o = Output()
       guy = Player()
       screen = pygame.display.set_mode((640, 400))
               keys = pygame.key.get_pressed()
               if keys[pygame.K_LEFT]:
               if keys[pygame.K_RIGHT]:
               for event in pygame.event.get(): pass

if __name__ == '__main__':

Thanks much,