From d1c404fe8eac3c743004a9a48a683e9361c8f7b3 Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Mon, 3 Nov 2025 23:10:04 +0200 Subject: fix: added typing --- GameView.py | 61 ++++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 19 deletions(-) (limited to 'GameView.py') 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() + + -- cgit v1.2.3