[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] Framerate Normalizer
You can define movement speed per second. That way every computer
moves X pixels/sec ( and faster computers still get the benefit of
more FPS )
Ie: velocity = 10 pixels/sec , or rotation = 90degrees/sec
Then calculate how much to move based on the users FPS:
# fps.py
class FPS():
def __init__(self):
# FPS() calc's delta ( AKA: fps_elapsed )
self.ticks_cur = pygame.time.get_ticks()
self.ticks_last = pygame.time.get_ticks()
def tick(self):
"""call once each game-loop to calculate .delta"""
# calculate delta
self.ticks_cur = pygame.time.get_ticks()
self.delta = ( self.ticks_cur - self.ticks_last ) / 1000.0
self.ticks_last = self.ticks_cur
# unit.py
class Unit():
def update(self):
"""update physics / etc. for all units."""
# delta is what you multiple your speed per second by
delta = self.fps.delta
# in my example: .vel, .accel are euclid.Vector2()
self.vel += self.accel * delta
new_loc = self.loc + self.vel * delta
--
Jake