-
Notifications
You must be signed in to change notification settings - Fork 3
/
map.py
executable file
·146 lines (138 loc) · 5.43 KB
/
map.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
#Handles the map parsing
import configparser
import pygame
import time
import gzip
import random
import os
import io
from configobj import ConfigObj
class Level(object):
def spawntree(self):
x = random.randint(5, self.width-5)
y = random.randint(5, self.height-5)
self.setTile(x,y,"w")
self.setTile(x,y-1,"w")
self.setTile(x,y-2,"le")
self.setTile(x,y-3,"le")
self.setTile(x,y-4,"le")
self.setTile(x,y-5,"le")
self.setTile(x+1,y-2,"le")
self.setTile(x+1,y-3,"le")
self.setTile(x+1,y-4,"le")
self.setTile(x+1,y-5,"le")
self.setTile(x-1,y-2,"le")
self.setTile(x-1,y-3,"le")
self.setTile(x-1,y-4,"le")
self.setTile(x-1,y-5,"le")
self.setTile(x-2,y-3,"le")
self.setTile(x-2,y-4,"le")
self.setTile(x+2,y-3,"le")
self.setTile(x+2,y-4,"le")
return x
def loadFile(self, filename="level.map"):
self.map = []
self.key = {}
self.monsters = {}
parser = configparser.ConfigParser()
f = io.TextIOWrapper(gzip.open(filename, 'rb'))
parser.read_file(f, filename)
self.map = parser.get("level", "map").replace('""', "").replace("'''","").split("\n")
tmpMap = []
for line in self.map:
tmpMap.append(line.split(";"))
self.map = tmpMap
self.map=self.map[:-1]
for section in parser.sections():
if section.startswith("mon"):
desc = dict(parser.items(section))
self.monsters[section] = desc
self.key={'wf':{'tile':'3, 16', 'name':'floor'},'le':{'tile':'2, 13', 'name':'wall'}, 'w':{'tile':'1, 9', 'name':'wall'}, 'tr':{'tile':'4, 16', 'name':'trap'},'g':{'tile':'2, 5', 'name':'floor'},'q': {'tile': '4, 8', 'name': 'pool'}, 'b': {'tile': '3, 4', 'name': 'wall'}, 'd': {'tile': '4, 14', 'name': 'floor'}, 'cd': {'tile': '4, 13', 'name': 'floor'}, 'i': {'tile': '1, 13', 'name': 'ice'}, '1': {'tile': '1, 1', 'name': 'wall'}, '0': {'tile': '2, 3', 'name': 'wall'}, '3': {'tile': '3, 1', 'name': 'wall'}, '2': {'tile': '2, 1', 'name': 'wall'}, '5': {'tile': '1, 2', 'name': 'wall'}, 't': {'tile': '2, 8', 'name': 'floor'}, '7': {'tile': '3, 2', 'name': 'floor'}, '6': {'tile': '2, 2', 'name': 'wall'}, '9': {'tile': '1, 3', 'name': 'wall'}, '8': {'tile': '4, 2', 'name': 'wall'}, '4': {'tile': '4, 1', 'name': 'wall'}}
try:
self.inventory=parser.get("player", "inventory")
self.inventory = self.inventory.split(", ")
tmpinv = []
for xyz in self.inventory:
tmpinv.append(int(xyz))
self.inventory=tmpinv
del(tmpinv)
except:
self.inventory=[100, 100,100, 100,100, 100, 100,100, 100,100, 10000,20000, 100, 100, 100, 100, 100]
try:
self.playerx=parser.get("player", "x")
self.playery=parser.get("player", "y")
print("Found player")
except:
self.playerx, self.playery=400, 300
self.width = len(self.map[0])
self.height = len(self.map)
def getTile(self, x, y, a=True):
if a:
try:
char = self.map[y][x]
except IndexError:
return {}
try:
return self.key[char]
except KeyError:
return {}
else:
try:
return self.map[y][x]
except IndexError:
return {}
def is_wall(self, x, y):
if self.getTile(x, y)["name"]!="floor":
return True
else:
return False
def renderRow(self, row, spritesheet):
index=0
image = pygame.Surface((self.width*32, 32))
for column in row:
coord = self.key[column]["tile"].split(", ")
tile = spritesheet.image_at(pygame.rect.Rect((int(coord[0])-1)*32, (int(coord[1])-1)*32, 32, 32))
image.blit(tile, (index*32, 0))
index+=1
return image
def render(self, spritesheet):
index = 0
image = pygame.Surface((self.width*32, self.height*32))
for y in self.map:
image.blit(self.renderRow(y, spritesheet), (0, index*32))
index+=1
return image
def setTile(self, x, y, id):
if x<30 and x>=0 and y<41 and y>=0:
self.map[y][x]=id
def writeConfig(self, filename, monsters, savemon=True, invcount=None, player=None):
config = ConfigObj()
config.filename=filename
map1=""
index=0
config["player"]={}
if invcount!=None:
print("inv:",invcount)
print("player:",player)
config["player"]={"inventory":invcount}
if player!=None:
config["player"]=dict(config["player"],**{"x":player[0]})
config["player"]=dict(config["player"],**{"y":player[1]})
if savemon:
for mon in monsters:
config["mon"+str(index)]={"x":mon.rect.left, "y":mon.rect.top}
index+=1
yfinal = ""
for y in self.map:
for x in y:
yfinal+=x+";"
map1=map1+yfinal[:-1]+"\n "
yfinal=''
config["level"]={"tileset":"abc", "map":map1}
config.filename="temp.text"
config.write()
level124=open("temp.text", "r")
f_out = gzip.open(filename, 'w')
f_out.writelines(level124)
f_out.close()
level124.close()