rework loops to return at least once per fps

This commit is contained in:
Jan Klopper 2016-03-31 16:31:51 +02:00
parent da2f29c38a
commit c9c45df928

View file

@ -67,18 +67,18 @@ class Canvas(object):
lasttime = time.time() lasttime = time.time()
changed = False changed = False
while True: while True:
currenttime = time.time() nextdraw = time.time() + (1 / self.fps)
changed = self.Draw() or changed changed = self.Draw(nextdraw) or changed
if currenttime - lasttime >= 1 / self.fps: if time.time() - lasttime >= 1 / self.fps:
pygame.display.flip() pygame.display.flip()
changed = False changed = False
lasttime = time.time() lasttime = time.time()
def Draw(self): def Draw(self, returntime):
"""Draws pixels specified in the received packages in the queue""" """Draws pixels specified in the received packages in the queue"""
if self.queue.empty(): if self.queue.empty():
return False return False
while not self.queue.empty(): while not self.queue.empty() and time.time() < returntime:
data = self.queue.get() data = self.queue.get()
preamble = struct.unpack_from("<?", data)[0] preamble = struct.unpack_from("<?", data)[0]
protocol = struct.unpack_from("<B", data, 1)[0] protocol = struct.unpack_from("<B", data, 1)[0]