summaryrefslogtreecommitdiff
path: root/GameView.py
diff options
context:
space:
mode:
authorJoel Kronqvist <joel.kronqvist@iki.fi>2025-11-03 23:10:04 +0200
committerJoel Kronqvist <joel.kronqvist@iki.fi>2025-11-03 23:10:04 +0200
commitd1c404fe8eac3c743004a9a48a683e9361c8f7b3 (patch)
treef5df16492fd5cfc3a2915c678306b53c212edb5e /GameView.py
parentef6abc27cec35e32acef66c5077ffcc6bedde983 (diff)
downloadSnakePuzzle-d1c404fe8eac3c743004a9a48a683e9361c8f7b3.tar.gz
SnakePuzzle-d1c404fe8eac3c743004a9a48a683e9361c8f7b3.zip
fix: added typing
Diffstat (limited to 'GameView.py')
-rw-r--r--GameView.py61
1 files changed, 42 insertions, 19 deletions
diff --git a/GameView.py b/GameView.py
index 10fba7b..0303445 100644
--- a/GameView.py
+++ b/GameView.py
@@ -1,16 +1,19 @@
+from typing import Optional
+from Vec import Vec2
from Game import Game
from Box import Box
from Door import Door
from PressurePlate import PressurePlate
from Trail import Trail
+from pygame import Surface
import pygame
class GameView:
- def __init__(self, timeBased = False):
+ def __init__(self, timeBased: bool = True):
self.timeBased = timeBased
self.game = Game([
@@ -91,21 +94,24 @@ class GameView:
self._tickTime = 700
- self._previousTick = None
+ self._previousTick = -10*self._tickTime
- self.nextControlDirection = None
+ self.nextControlDirection: Optional[Vec2] = None
- def isRunning(self): return not self.game.isLost()
+ def isRunning(self) -> bool:
+ return not self.game.isLost()
- def width(self): return self.game.width() * self.cellWidth
+ def width(self) -> int:
+ return self.game.width() * self.cellWidth
- def height(self): return self.game.height() * self.cellWidth
+ def height(self) -> int:
+ return self.game.height() * self.cellWidth
- def render(self, surface):
+ def render(self, surface: Surface) -> None:
surface.fill("black")
@@ -130,10 +136,13 @@ class GameView:
for box in self.game.boxes:
self._drawBoxOfColor(surface, "brown", box.pos.x, box.pos.y)
- for cell in self.game.snake.cells:
- self._drawBoxOfColor(surface, "red", cell.x, cell.y)
+ match self.game.snake:
+ case None: raise ValueError("snake not found")
+ case _:
+ for cell in self.game.snake.cells:
+ self._drawBoxOfColor(surface, "red", cell.x, cell.y)
- def _drawBoxOfColor(self, surface, color, x, y):
+ def _drawBoxOfColor(self, surface: Surface, color: str, x: int, y: int) -> None:
pygame.draw.rect(surface, color, pygame.Rect(
x*self.cellWidth,
y*self.cellWidth,
@@ -142,14 +151,28 @@ class GameView:
))
- def update(self, time):
- if self.timeBased and ((self._previousTick == None) or (self._previousTick + self._tickTime <= time)):
+ def update(self, time: int) -> None:
+ if self.timeBased and (self._previousTick + self._tickTime <= time):
self._previousTick = time
- if self.nextControlDirection != None:
- self.game.snake.heading = self.nextControlDirection
- self.nextControlDirection = None
- self.game.tick()
- elif (not self.timeBased) and self.nextControlDirection != None:
- self.game.snake.heading = self.nextControlDirection
- self.nextControlDirection = None
+ match self.nextControlDirection:
+ case None: return None
+ case _:
+ match self.game.snake:
+ case None: return None
+ case _:
+ self.game.snake.heading = self.nextControlDirection
+ self.nextControlDirection = None
self.game.tick()
+ else:
+ match self.nextControlDirection:
+ case None: return None
+ case _:
+ self._previousTick = time
+ match self.game.snake:
+ case None: return None
+ case _:
+ self.game.snake.heading = self.nextControlDirection
+ self.nextControlDirection = None
+ self.game.tick()
+
+