-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.py
69 lines (52 loc) · 2 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
import math
from vector2 import *
class Map:
def __init__(self, tile_size):
self.tile_size = tile_size
def load(self, filename, tileset):
file = open(filename, "r")
if (file == None):
print(f"Error opening {filename}!")
return False
self.map = []
self.size = Vector2()
for line in file:
row = []
x = 0
for c in line:
if ((c == '\n') or (c == '\r')):
c = None
elif (c not in tileset):
if ("default" in tileset):
c = "default"
else:
print(f"Unknown tile {c}!")
c = None
if (c != None):
t = tileset[c]
row.append(t["tile"])
if ("func" in t):
p = None
if ("func_param" in t):
p = t["func_param"]
t["func"](c, x , self.size.y, p)
x += 1
self.size.x = max(self.size.x, len(row))
self.size.y += 1
self.map.append(row)
file.close()
return True
def get_tile(self, p):
return self.map[int(p.y)][int(p.x)]
def draw(self, screen_pos, screen_size, offset, screen):
msx = math.ceil(screen_size.x / self.tile_size.x)
msy = math.ceil(screen_size.y / self.tile_size.y)
offset.x = int(offset.x)
offset.y = int(offset.y)
for y in range(offset.y, min(self.size.y, offset.y + msy)):
map_row = self.map[y]
py = screen_pos.y + (y - offset.y) * self.tile_size.y
for x in range(offset.x, min(min(len(map_row), self.size.x), offset.x + msx)):
px = screen_pos.x + (x - offset.x) * self.tile_size.x
if (map_row[x].image != None):
screen.blit(map_row[x].image, (px,py))