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