-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateWorld.py
More file actions
34 lines (28 loc) · 1.21 KB
/
GenerateWorld.py
File metadata and controls
34 lines (28 loc) · 1.21 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
import re
def cleanFile(file):
with open(file, 'r') as f:
lines = f.readlines()
top = max([len(i.rstrip(' \n')) for i in lines])
for i in range(len(lines)):
lines[i] = lines[i].rstrip(' \n')
if len(lines[i]) < top:
lines[i] = lines[i] + " " * ((top - len(lines[i]))) + '\n'
with open(file, 'w') as f:
f.writelines(lines)
# To be implemented when we have multiple world-based projects:
# inp = input("Enter game to update world of ('Platformer'): ")
# if inp == "Platformer":
assign = " <assign variable=\"world1[{1}]\" expression=\""{0}"\"/>\n"
newdata = ""
with open('data/level1.txt', 'r') as f:
data = f.readlines()
for i in range(len(data[:12])):
newdata += assign.format(data[i].strip('\n'), str(i))
cleanFile('data/level1.txt')
with open('Platformer.fprg', 'r') as f:
data = f.read()
wrapper = '<comment text="Auto generated code below"/>{0} <comment text="Auto generated code above"/>'
for i in re.findall('(%s)' % wrapper.replace('/', '\\/').format("(.|\n)+?"), data):
data = data.replace(i[0], wrapper.format('\n'+newdata))
with open('Platformer.fprg', 'w') as f:
f.write(data)