summaryrefslogtreecommitdiff
path: root/GameView.py
blob: 50785a713d5a5de66bda41af328fe58b9714d75b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

from Game import Game
from Box import Box
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.walls.walls():
            pygame.draw.rect(surface, "white", pygame.Rect(
                cell.x*self.cellWidth,
                cell.y*self.cellWidth,
                self.cellWidth,
                self.cellWidth
            ))

        for box in self.game.boxes:
            pygame.draw.rect(surface, "brown", pygame.Rect(
                box.pos.x*self.cellWidth,
                box.pos.y*self.cellWidth,
                self.cellWidth,
                self.cellWidth
            ))

        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
            ))



    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()