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

[pygame] Optimization Fun



I use the below iterator to give me a smooth range of values between 0
and 1 over time. I use it to smoothly mooove sprites around. It is
very hard to optimize, because it always runs for a period of time,
and therefore, cannot be timed. Hmmm.

Anyone got any ideas on how to speed this iterator up, or test its
execution speed?

NB: time_func is defined as time.clock or time.time depending on platform.

Sw.

time_func = time.clock

def smooth_normal_range_over_time(duration):
    """
    Provides a smooth (sine wave) progresion of values between 0.0 and
1.0 over a duration of seconds.
    """
    stop_time = time_func() + duration
    now = time_func()
    while now < stop_time:
        time_left = stop_time - now
        yield 1 - ((cos((1.0 - time_left / duration) * pi) + 1) * 0.5)
        now = time_func()