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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
from Game import Game
from Box import Box
from Door import Door
from PressurePlate import PressurePlate
from Trail import Trail
import pygame
class GameView:
def __init__(self, timeBased = False):
self.timeBased = timeBased
self.game = Game([
"""#####O#####
# # # #
# # # #
# # # #
# #
# #
# #
# #
# #
# #
#####I#####""",
"""#####D#####
# + #
# _ #
# #
# #
# #
# #
# #
# #
# #
#####I#####""",
"""#####D#####
# + #
# + #
# + #
# + #
# + #
# b _ #
# #
# #
# #
#####I#####""",
"""#####D#####
# #+# #
# #+# #
# #+# #
# #+# #
# b _ #
# #
# #
# #
# #
#####I#####""",
"""#####D#####
#+++++ #
#+ #
#+ #
#_ #
#####D#####
# + #
# b + #
# + #
# _ #
#####I#####""",
"""###########
# #
# #
# #
# #
# #
# #
# #
# #
# #
#####I#####""",
])
self.cellWidth = 64
self._tickTime = 700
self._previousTick = None
self.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():
self._drawBoxOfColor(surface, "white", cell.x, cell.y)
for static in self.game.statics:
match static:
case Door():
if static.isOpen():
self._drawBoxOfColor(surface, "lightgray", static.pos.x, static.pos.y)
else:
self._drawBoxOfColor(surface, "gray", static.pos.x, static.pos.y)
case PressurePlate():
self._drawBoxOfColor(surface, "yellow", static.pos.x, static.pos.y)
case Trail():
if static.isOn():
self._drawBoxOfColor(surface, "pink", static.pos.x, static.pos.y)
else:
self._drawBoxOfColor(surface, "purple", static.pos.x, static.pos.y)
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)
def _drawBoxOfColor(self, surface, color, x, y):
pygame.draw.rect(surface, color, pygame.Rect(
x*self.cellWidth,
y*self.cellWidth,
self.cellWidth,
self.cellWidth
))
def update(self, time):
if self.timeBased and ((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()
elif (not self.timeBased) and self.nextControlDirection != None:
self.game.snake.heading = self.nextControlDirection
self.nextControlDirection = None
self.game.tick()
|