-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgenlevel.py
56 lines (47 loc) · 1.4 KB
/
genlevel.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
from PIL import Image
import json
from pathlib import Path
PARENT_DIR = Path(__file__).resolve().parent # directory of the main.py file
LVL = input("LVL name? (./levelSprites/{?}.png): ") # level to convert
im = Image.open(PARENT_DIR.joinpath(f"levelSprites/{LVL}.png"), 'r') # image object
# Colours
RED = (255,0,0)
WHITE = (255,255,255)
BLUE = (0,0,255)
YELLOW = (255,255,0)
GREEN = (0,255,0)
BLACK = (0,0,0)
PURPLE = (93, 63, 211)
GREY = (128, 128, 128)
LIGHT_BLUE = (155, 255, 255)
levelData = {}
level = []
for rownum in range(64): # iterates through pixels in file, appends their cooresponding tile id
row = []
for pixelnum in range(64):
col = im.getpixel((pixelnum, rownum))
col = col[0:3]
if col == WHITE:
row.append(0)
elif col == BLACK:
row.append(2)
elif col == RED:
row.append(3)
elif col == BLUE:
levelData["playerSpawn"] = (pixelnum, rownum)
row.append(0)
elif col == PURPLE:
row.append(4)
elif col == GREEN:
row.append(5)
elif col == GREY:
row.append(6)
elif col == LIGHT_BLUE:
row.append(7)
else:
row.append(0)
level.append(row)
levelData["levelMap"] = level
with open(PARENT_DIR.joinpath(f"levelFiles/{LVL}.json"), "w") as f: # store as JSON
f.write(json.dumps(levelData))
f.close()