diff options
| author | Joel Kronqvist <joel.kronqvist@iki.fi> | 2025-10-26 12:52:34 +0200 |
|---|---|---|
| committer | Joel Kronqvist <joel.kronqvist@iki.fi> | 2025-10-26 12:52:34 +0200 |
| commit | 57f20a5ef761985b34817846d471a064b180e089 (patch) | |
| tree | 9d0029868b9e170bf07ffab9e0386b06b4189837 /GameView.py | |
| download | SnakePuzzle-57f20a5ef761985b34817846d471a064b180e089.tar.gz SnakePuzzle-57f20a5ef761985b34817846d471a064b180e089.zip | |
feat: implemented simple snake game
Diffstat (limited to 'GameView.py')
| -rw-r--r-- | GameView.py | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/GameView.py b/GameView.py new file mode 100644 index 0000000..241914c --- /dev/null +++ b/GameView.py @@ -0,0 +1,56 @@ + +from Game import Game +import pygame + +class GameView: + + + game = Game() + + cellWidth = 64 + + _tickTime = 700 + + _previousTick = None + + nextControlDirection = None + + + def isRunning(self): return not self.game.isLost() + + + def width(self): return self.game.width() * self.cellWidth + + + def height(self): return self.game.height() * self.cellWidth + + + def render(self, surface): + + surface.fill("black") + + for cell in self.game.snake.cells: + pygame.draw.rect(surface, "red", pygame.Rect( + cell.x*self.cellWidth, + cell.y*self.cellWidth, + self.cellWidth, + self.cellWidth + )) + + for cell in self.game.walls.walls(): + pygame.draw.rect(surface, "white", pygame.Rect( + cell.x*self.cellWidth, + cell.y*self.cellWidth, + self.cellWidth, + self.cellWidth + )) + + + def update(self, time): + if (self._previousTick == None) or (self._previousTick + self._tickTime <= time): + self._previousTick = time + if self.nextControlDirection != None: + self.game.snake.heading = self.nextControlDirection + self.nextControlDirection = None + self.game.tick() + |
