-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblocks.py
More file actions
331 lines (276 loc) · 10.1 KB
/
blocks.py
File metadata and controls
331 lines (276 loc) · 10.1 KB
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
324
325
326
327
328
329
330
331
import os
import re
import random
import time
from functools import partial, lru_cache
import pygame
import logging
from constants import DEFAULT_BLOCK_SIZE, LEVELS_GRAPHICAL_FOLDER, ASSETS
from config import get_lava_sheets
from entities import Brochette
from helper import classproperty
from physics import Pos
LOGGER = logging.getLogger(__name__)
@lru_cache()
def get_boom_img(i):
path = os.path.join(LEVELS_GRAPHICAL_FOLDER, 'boom', f'BOOOM{i}.png')
img = pygame.image.load(path).convert()
img.set_colorkey((0, 0, 0))
return img
def re_compile(matrix):
"""Compile the matrix of pattern and replace the " " by dots to match the air."""
not_solid = "".join([b().character for b in BLOCKS if not b().solid]) + Block.character + EndBlock.character
solid = "".join([b().character for b in BLOCKS if b().solid and b is not EndBlock])
not_solid = f"[{re.escape(not_solid)}]"
solid = f"[{re.escape(solid)}]"
compiled = []
for line in matrix:
compiled_line = []
for pattern in line:
pattern = pattern.replace(" ", not_solid)
pattern = pattern.replace("?", solid)
pattern = re.compile(pattern)
compiled_line.append(pattern)
compiled.append(compiled_line)
return compiled
class Block:
DEFAULT_BLOCK_SIZE = DEFAULT_BLOCK_SIZE
IGNORE_IMG_CACHE = False
character = '.'
_sheet = None
sheet = None # type: pygame.Surface
default_sprite_pos = (0, 0)
solid = False
visible = False
deadly = False
rotation = 0
sheet_pattern = [[]]
def __init__(self, pos=(0, 0)):
self.pos = pos
self.exploded = False
self.explode_frame = 1
@staticmethod
def new(character='.', pos=(0, 0)):
dic = {
"D": Dirt,
"S": Stone,
"H": Bush,
"F": Cloud,
"C": CatCloud,
"L": Lava,
"B": Barbecue,
"E": EndBlock,
"V": partial(FieryBarbecue, "V"),
"^": partial(FieryBarbecue, "^"),
"<": partial(FieryBarbecue, "<"),
">": partial(FieryBarbecue, ">")
}
return dic.get(character, Block)(pos)
def get_img(self, neighbourg="", rotation=0):
"""Neighbourg is a string of nine chars of the 9 blocks next to this one."""
for y, line in enumerate(self.sheet_pattern):
for x, pattern in enumerate(line):
if pattern.match(neighbourg):
return self.img_at(x, y, rotation)
return self.img_at(*self.default_sprite_pos, rotation=rotation)
def explode(self):
self.exploded = True
@classmethod
# @lru_cache()
def img_at(cls, x, y, rotation=0):
return pygame.transform.rotate(cls.sheet.subsurface((x * cls.DEFAULT_BLOCK_SIZE, y * cls.DEFAULT_BLOCK_SIZE,
cls.DEFAULT_BLOCK_SIZE, cls.DEFAULT_BLOCK_SIZE)), rotation)
def internal_logic(self, level):
pass
def on_collision(self, level):
pass
class Dirt(Block):
character = "D"
solid = True
visible = True
deadly = False
default_sprite_pos = 1, 1
@classproperty
@lru_cache()
def sheet_pattern(cls):
# . is any block
# ? is any solid
# " " is any non solid block
return re_compile([
[". . ??.?.", ". .??????", ". .?? .?.", ". . ? .?.", "???????? ", "?????? ??"],
[".?. ??.?.", "?????????", ".?.?? .?.", ".?. ? .?.", ".? .?????", " ?.??.???"],
[".?? ??. .", ".?.???. .", "??.?? . .", ".?. ? . .", ". . ??.? ", ". .?? ?."],
[". . ??. .", ". .???. .", ". .?? . .", ". . ? . .", ".? ??. .", " ?.?? . ."],
[". .??? ??", ". .??? ? ", ". .????? ", ".?. ?? ? ", ".?.?? ? "]
])
@classproperty
def sheet(cls):
if cls._sheet is None:
LOGGER.info("Dirt - Setting sheet")
sheet = pygame.image.load(os.path.join(LEVELS_GRAPHICAL_FOLDER, "dirt_sheet.png")).convert()
sheet.set_colorkey((255, 0, 255))
sheet = pygame.transform.scale(sheet, (Pos(sheet.get_size()) * DEFAULT_BLOCK_SIZE / 16).i)
cls._sheet = sheet
return cls._sheet
class Stone(Block):
character = "S"
solid = True
visible = True
deadly = False
default_sprite_pos = 1, 1
@classproperty
def sheet(cls):
if cls._sheet is None:
LOGGER.info("Stone - Setting sheet")
sheet = pygame.image.load(os.path.join(LEVELS_GRAPHICAL_FOLDER, "stone_sheet.png")).convert()
sheet.set_colorkey((255, 0, 255))
sheet = pygame.transform.scale(sheet, (Pos(sheet.get_size()) * DEFAULT_BLOCK_SIZE / 16).i)
cls._sheet = sheet
return cls._sheet
@classproperty
@lru_cache()
def sheet_pattern(cls):
return re_compile([
[". . ??.?.", ". .??????", ". .?? .?.", ". . ? .?."], # "???.??.? ", "?????. ?."],
[".?. ??.?.", "?????????", ".?.?? .?.", ".?. ? .?."], # ".? .?????", " ?.??.???"],
[".?. ??. .", ".?.???. .", ".?.?? . .", ".?. ? . ."], # ". . ??.? ", ". .?? ?."],
[". . ??. .", ". .???. .", ". .?? . .", ". . ? . ."], # ".? ??. .", " ?.?? . ."]
])
class Bush(Block):
character = "H"
solid = False
visible = True
deadly = False
@classproperty
def sheet(cls):
if cls._sheet is None:
LOGGER.info("Bush - Setting sheet")
sheet = pygame.image.load(os.path.join(LEVELS_GRAPHICAL_FOLDER, "bush.png")).convert()
sheet.set_colorkey((255, 0, 255))
sheet = pygame.transform.scale(sheet, (DEFAULT_BLOCK_SIZE, DEFAULT_BLOCK_SIZE))
cls._sheet = sheet
return cls._sheet
class Cloud(Block):
character = "F"
solid = False
visible = True
deadly = False
@classproperty
def sheet(cls):
if cls._sheet is None:
LOGGER.info("Cloud - Setting sheet")
sheet = pygame.image.load(os.path.join(LEVELS_GRAPHICAL_FOLDER, "cloud.png")).convert()
sheet.set_colorkey((255, 0, 255))
sheet = pygame.transform.scale(sheet, (DEFAULT_BLOCK_SIZE, DEFAULT_BLOCK_SIZE))
cls._sheet = sheet
return cls._sheet
class CatCloud(Block):
character = "C"
solid = False
visible = True
deadly = False
@classproperty
def sheet(cls):
if cls._sheet is None:
LOGGER.info("CatCloud - Setting sheet")
sheet = pygame.image.load(os.path.join(LEVELS_GRAPHICAL_FOLDER, "cat_cloud.png")).convert()
sheet.set_colorkey((255, 0, 255))
sheet = pygame.transform.scale(sheet, (DEFAULT_BLOCK_SIZE, DEFAULT_BLOCK_SIZE))
cls._sheet = sheet
return cls._sheet
class Lava(Block):
character = "L"
solid = True
visible = True
deadly = True
current_index = 0
IGNORE_IMG_CACHE = True
@classproperty
def sheet(cls):
if cls._sheet is None:
sheets = [pygame.image.load(os.path.join(os.path.join(LEVELS_GRAPHICAL_FOLDER, "lava"),
path.lower())).convert()
for path in get_lava_sheets]
sheets.reverse()
for i in range(len(sheets)):
sheets[i] = pygame.transform.scale(sheets[i], (DEFAULT_BLOCK_SIZE, DEFAULT_BLOCK_SIZE))
cls._sheet = sheets
cls.current_index = (time.time() * 15) % len(cls._sheet)
return cls._sheet[int(cls.current_index)]
class Barbecue(Block):
character = "B"
solid = True
visible = True
deadly = True
@classproperty
def sheet(cls):
if cls._sheet is None:
LOGGER.info("Barbecue - Setting sheet")
sheet = pygame.image.load(os.path.join(LEVELS_GRAPHICAL_FOLDER, "barbecue.png")).convert()
sheet.set_colorkey((255, 0, 255))
sheet = pygame.transform.scale(sheet, (DEFAULT_BLOCK_SIZE, DEFAULT_BLOCK_SIZE))
cls._sheet = sheet
return cls._sheet
class FieryBarbecue(Block):
character = "^"
solid = False
visible = True
deadly = True
char_dic = {
"^": (0, Pos(0, -1)), # Rotation and direction of brochettes
"V": (180, Pos(0, 1)),
"<": (90, Pos(-1, 0)),
">": (-90, Pos(1, 0))
}
@classproperty
def sheet(cls):
if cls._sheet is None:
LOGGER.info("FieryBarbecue - Setting sheet")
sheet = pygame.image.load(os.path.join(LEVELS_GRAPHICAL_FOLDER, "fiery_barbecue.png")).convert()
sheet.set_colorkey((255, 0, 255))
sheet = pygame.transform.scale(sheet, (DEFAULT_BLOCK_SIZE, DEFAULT_BLOCK_SIZE))
cls._sheet = sheet
return cls._sheet
def __init__(self, character="^", pos=(0, 0)):
super().__init__(pos)
self.character = character
self.rotation = FieryBarbecue.char_dic[self.character][0]
self.next_spawn = 15
self.brochettes = []
def internal_logic(self, level):
self.next_spawn -= 1
if self.next_spawn <= 0:
level.spawn(Brochette(level.map_to_world(self.pos) + Pos(DEFAULT_BLOCK_SIZE, DEFAULT_BLOCK_SIZE) / 2,
FieryBarbecue.char_dic[self.character]))
self.next_spawn = 75
class EndBlock(Block):
character = "E"
solid = True
visible = True
deadly = False
@classproperty
def sheet(cls):
if cls._sheet is None:
LOGGER.info("EndBlock - Setting sheet")
sheet = pygame.image.load(os.path.join(ASSETS, "logo.png")).convert()
sheet.set_colorkey((255, 0, 255))
sheet = pygame.transform.scale(sheet, (DEFAULT_BLOCK_SIZE, DEFAULT_BLOCK_SIZE))
cls._sheet = sheet
return cls._sheet
# This isn't called every frame. Instead, it is called when the player touches it
def on_collision(self, level):
level.explode()
BLOCKS = [
Dirt,
Stone,
Cloud,
CatCloud,
Bush,
Lava,
Barbecue,
FieryBarbecue,
partial(FieryBarbecue, ">"),
partial(FieryBarbecue, "<"),
partial(FieryBarbecue, "V"),
EndBlock,
]