-
Notifications
You must be signed in to change notification settings - Fork 0
/
level.py
323 lines (260 loc) · 10.6 KB
/
level.py
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
from classes.player import Player
from classes.twin import Twin
from classes.exit import Exit
from classes.boundary import Boundary
from classes.trap import Trap
from classes.key import Key
from classes.text import Text
from classes.push_block import PushBlock
from utils import *
# level constants
# the width and height must be also in blankLevelGenerator.py
LevelWidth = 30
LevelHeight = 20
class Level(object):
"""
The Level is used to store information about the level XD
for the tuples the entities that are stored in them will only appear once in the game.
and the entites stored in list appear more than once
( tile_width, tile_height )
its the location on the grid from the origin
the origin is the pygame's origin eg top left corner since we read the file this way
the level entities are :
b : block
T : trap
p : player spawnpoint
t : twin spawnpoint
e : exit spawnpoint
k : key
"""
def __init__(self, LevelStruct:list = None ) -> None:
if LevelStruct != None:
self.boundaries:list = LevelStruct["boundaries"]
self.traps:list = LevelStruct["traps"]
self.player:Player = LevelStruct["player"]
self.twin:Twin = LevelStruct["twin"]
self.exit:Exit = LevelStruct["exit"]
self.keys:list = LevelStruct["keys"]
self.text:list = LevelStruct["text"]
else:
self.traps:list = []
self.exit:Exit = Exit(0, 0)
self.player:Player = Player(0, 0)
self.twin:Twin = Twin(0, 0)
self.boundaries:list = []
self.keys:list = []
self.text:list = []
self.requestNextLevel = False
self.requestRestartLevel = False
self.maxMoves = 0
self._generateListOfBlocks()
def load_level( self, levelName:str ) -> None:
self.ClearLevel()
with open(f"levels/{levelName}.txt", "r") as file:
data = file.read().split("\n")
map_data = data[:LevelHeight]
script_data = data[LevelHeight+1:]
_map = []
for row in map_data:
_map.append(list(row))
level = Level()
for y, row in enumerate(_map):
for x, block in enumerate(row):
match block:
case "p":
self.SetPlayer(x, y)
case "t":
self.SetTwin(x, y)
case "b":
self.addBoundaries(x, y)
case "T":
self.addTrap(x, y)
case "e":
self.SetExit(x, y)
case "k":
self.addKey(x, y)
case "d":
self.addText( x,y )
for script in script_data:
if script.startswith( "!maxMoves" ):
self.maxMoves = int( script.split("=")[1] )
elif script.startswith( "!ExitNeedsKey" ):
if "False" == script.split("=")[1]: self.exit.DisableKeyNeed()
elif script.startswith( "!AddText" ):
posNtext = script.split("=")[1].split(",")
self.addText( int( posNtext[0] ) * 32, int( posNtext[1] ) * 32 )
self.text[ len(self.text) - 1 ].SetText( posNtext[2] )
return level
def loadLevelByName(self, Name:str) -> None:
self.load_level(Name)
def loadLevelByIndex( self, Index:int ) -> None:
self.load_level( f"level{Index}" )
def move(self, direction):
player_going_to = [0, 0]
match direction:
case "left":
player_going_to[0] = self.player.x - 1
player_going_to[1] = self.player.y
case "right":
player_going_to[0] = self.player.x + 1
player_going_to[1] = self.player.y
case "up":
player_going_to[0] = self.player.x
player_going_to[1] = self.player.y - 1
case "down":
player_going_to[0] = self.player.x
player_going_to[1] = self.player.y + 1
resetBlockList = False
for block in self.blocks:
if block.collide(player_going_to[0], player_going_to[1]) and block != self.player:
if IsKey( block ):
self.player.PickupBlock( block )
resetBlockList = True
self.RemoveBlockByPosition( player_going_to )
continue
elif IsTrap( block ):
self.requestRestartLevel = True
elif IsExit( block ):
if block.CanExit( self.player ):
self.requestNextLevel = True
continue
break
else:
self.maxMoves -= 1
if self.maxMoves <= 0:
self.requestRestartLevel = True
self.player.move(direction)
if resetBlockList:
self._generateListOfBlocks()
twin_direction = self.twin.move_toward(self.player.x, self.player.y)
twin_going_to = [0, 0]
match twin_direction:
case "left":
twin_going_to[0] = self.twin.x - 1
twin_going_to[1] = self.twin.y
case "right":
twin_going_to[0] = self.twin.x + 1
twin_going_to[1] = self.twin.y
case "up":
twin_going_to[0] = self.twin.x
twin_going_to[1] = self.twin.y - 1
case "down":
twin_going_to[0] = self.twin.x
twin_going_to[1] = self.twin.y + 1
for block in self.blocks:
if block.collide(twin_going_to[0], twin_going_to[1]) and block != self.twin:
if IsKey( block ):
self.keys.remove(block)
continue
if IsTrap( block ):
self.requestRestartLevel = True
continue
if IsPlayer( block ):
self.requestRestartLevel = True
continue
break
else:
self.twin.move(twin_direction)
def render(self, surface) -> None:
self.player.render(surface)
self.twin.render(surface)
self.exit.render(surface)
for boundary in self.boundaries:
boundary.render(surface)
for trap in self.traps:
trap.render(surface)
for key in self.keys:
key.render(surface)
for text in self.text:
text.render(surface)
def ClearLevel( self ) -> None:
self.player.move_to(0, 0)
self.player.ClearInventory()
self.twin.move_to(0, 0)
self.exit.move_to(0, 0)
self.exit.EnableKey()
self.boundaries.clear()
self.traps.clear()
self.keys.clear()
self.text.clear()
def _generateListOfBlocks( self ) -> None:
self.blocks = [self.player, self.twin, self.exit]
self.blocks += [boundary for boundary in self.boundaries]
self.blocks += [trap for trap in self.traps]
self.blocks += [key for key in self.keys]
def AddToblockList( self, entity ) -> None:
self.blocks.append( entity )
def addBoundaries( self, x:int, y:int ) -> None:
self.boundaries.append(Boundary(x, y))
self.AddToblockList( self.boundaries[len(self.boundaries)-1] )
def addTrap( self, x:int, y:int ) -> None:
self.traps.append(Trap(x, y))
self.AddToblockList( self.traps[len(self.traps)-1] )
def addKey( self, x:int, y:int ) -> None:
self.keys.append(Key(x, y))
self.AddToblockList( self.keys[len(self.keys)-1] )
def SetPlayer( self, x:int, y:int ) -> None:
self.player.move_to(x, y)
def SetTwin( self, x:int, y:int ) -> None:
self.twin.move_to(x, y)
def SetExit( self, x:int, y:int ) -> None:
self.exit.move_to(x, y)
self._generateListOfBlocks() # here I am calling the function to override the blocks list
# because I am lazy and I would have to search trouhgt the list to find the exit entity
def addText( self, x:int, y:int ) -> None:
self.text.append( Text(x, y) )
self.AddToblockList( self.text[len(self.text)-1] )
def RemoveBlockByPosition( self, TargetPosition:tuple ):
for block in self.blocks:
if block.collide(TargetPosition[0], TargetPosition[1]):
if IsKey( block ):
self._findBlockInListAndDestroyIt( block, self.keys )
elif IsTrap( block ):
self._findBlockInListAndDestroyIt( block, self.traps )
elif IsBoundary( block ):
self._findBlockInListAndDestroyIt( block, self.boundaries )
elif IsTwin( block ):
self.SetTwin( -1,-1 )
elif IsExit( block ):
self.SetExit( -7,-1 )
elif IsPlayer( block ):
self.SetPlayer( -4,-1 )
def _findBlockInListAndDestroyIt( self, block, WorkingList:list ):
for block2 in WorkingList:
if block == block2:
WorkingList.pop( WorkingList.index( block ) )
return
def FindBlockByPosition( self, TargetPosition:tuple ):
for block in self.blocks:
if block.collide(TargetPosition[0], TargetPosition[1]):
return block
def GetAsDict( self ) -> dict:
LevelStruct = {}
LevelStruct["boundaries"] = self.boundaries
LevelStruct["traps"] = self.traps
LevelStruct["player"] = self.player
LevelStruct["twin"] = self.twin
LevelStruct["exit"] = self.exit
LevelStruct["keys"] = self.keys
LevelStruct["text"] = self.text
return LevelStruct
def GetAsList( self ) -> list:
return self.blocks
def GetBoundaries( self ) -> list:
return self.boundaries
def GetBoundaryByIndex( self, Index:int ) -> Boundary:
return self.boundaries[Index]
def GetTraps( self ) -> list:
return self.traps
def GetTrapsByIndex( self, Index:int ) -> tuple:
return self.traps[Index]
def GetKeys( self ) -> list:
return self.keys
def GetKeyByIndex( self, Index:int ) -> tuple:
return self.keys[Index]
def GetPlayer( self ) -> Player:
return self.player
def GetTwin( self ) -> Twin:
return self.twin
def GetExit( self ) -> Exit:
return self.exit