From a60a1403b7335719ceca18c745ff663bd6852e94 Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Tue, 4 Nov 2025 19:16:14 +0200 Subject: feat: implemented get in OptionalUtils --- GameView.py | 28 ++++++++++++++-------------- OptionalUtils.py | 6 ++++++ 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/GameView.py b/GameView.py index 0303445..a670d7c 100644 --- a/GameView.py +++ b/GameView.py @@ -1,4 +1,5 @@ +from OptionalUtils import get from typing import Optional from Vec import Vec2 from Game import Game @@ -13,7 +14,7 @@ import pygame class GameView: - def __init__(self, timeBased: bool = True): + def __init__(self, timeBased: bool = False): self.timeBased = timeBased self.game = Game([ @@ -155,24 +156,23 @@ class GameView: if self.timeBased and (self._previousTick + self._tickTime <= time): self._previousTick = time match self.nextControlDirection: - case None: return None + case None: None case _: - match self.game.snake: - case None: return None - case _: - self.game.snake.heading = self.nextControlDirection - self.nextControlDirection = None + get(self.game.snake).heading = self.nextControlDirection + self.nextControlDirection = None +# match self.game.snake: +# case None: return None +# case _: +# self.game.snake.heading = self.nextControlDirection +# self.nextControlDirection = None self.game.tick() - else: + elif not self.timeBased: match self.nextControlDirection: - case None: return None + case None: None case _: self._previousTick = time - match self.game.snake: - case None: return None - case _: - self.game.snake.heading = self.nextControlDirection - self.nextControlDirection = None + get(self.game.snake).heading = self.nextControlDirection + self.nextControlDirection = None self.game.tick() diff --git a/OptionalUtils.py b/OptionalUtils.py index 51828bb..6541208 100644 --- a/OptionalUtils.py +++ b/OptionalUtils.py @@ -17,3 +17,9 @@ def foreach(opt: Optional[T], call: Callable[[T], None]) -> None: return None case _: return call(opt) + +def get(opt: Optional[T]) -> T: + match opt: + case None: raise ValueError("get on None") + case _: + return opt -- cgit v1.2.3