Speed up snake
All checks were successful
docker-image / docker (push) Successful in 10m9s

We cannot speed up updating the display, but we can limit the updates to once a second, and take two steps per second.
This commit is contained in:
Stefan Bethke 2025-06-17 19:11:40 +02:00
commit 8aeeac62ef

View file

@ -1,4 +1,5 @@
import random import random
from datetime import datetime, timedelta
from time import sleep from time import sleep
from buba.bubaanimation import BubaAnimation from buba.bubaanimation import BubaAnimation
@ -46,6 +47,7 @@ class SnakeAnimation(BubaAnimation):
self.body = [(x, y)] self.body = [(x, y)]
self.render() self.render()
iterations = 0 iterations = 0
last_update = datetime.now()
while True: while True:
if self.is_blocked(x, y, d): if self.is_blocked(x, y, d):
end = True end = True
@ -71,8 +73,11 @@ class SnakeAnimation(BubaAnimation):
self.grid[ty][tx] = 0 self.grid[ty][tx] = 0
(tx, ty) = self.body[0] (tx, ty) = self.body[0]
self.grid[ty][tx] = 11 self.grid[ty][tx] = 11
if datetime.now() - last_update > timedelta(seconds=1):
last_update = datetime.now()
self.render() self.render()
sleep(1) else:
sleep(.5)
sleep(5) sleep(5)
@staticmethod @staticmethod