-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameboard.py
127 lines (111 loc) · 3.67 KB
/
gameboard.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
from constants import *
from engine_class import data
class Board(object):
""" map board object """
def __init__(self):
self.load_map_file("testmap.map")
self.blocked_squares = []
self.update_blocked_square_list()
def get_square(self, (x,y)):
if 0 <= x < self.board_size[0] and 0 <= y < self.board_size[1]:
square = self.grid[y][x]
return square
def get_rect(self):
width = self.board_size[0] * data.square_size
height = self.board_size[1] * data.square_size
return pygame.rect.Rect(0, 0, width, height)
def update_blocked_square_list(self):
self.blocked = []
for row in self.grid:
for square in row:
if square.blocked:
self.blocked_squares.append(square)
def get_blocked_squares(self):
blocklist = []
for row in self.grid:
for square in row:
if square.blocked:
blocklist.append(square)
return blocklist
def get_unblocked_squares(self):
unblocked = []
for row in self.grid:
for square in row:
if square.blocked == False:
unblocked.append(square)
return unblocked
def block_square(self, (x, y)):
square = self.get_square((x, y))
square.blocked = True
def get_occupied_squares(self):
occupied = []
for row in self.grid:
for square in row:
if square.unit:
occupied.append(square)
return occupied
def load_map_file(self, mapfile):
self.grid = []
self.blocked_squares = []
f = open(mapfile)
y = 0
for line in f:
line = line.strip()
row = []
x = 0
for square in line:
row.append(Square((x,y)))
x += 1
self.grid.append(row)
y += 1
self.board_size = (x, y)
y = 0
f.seek(0)
for row in f:
x = 0
for square in row:
if square == '#':
sq = self.get_square((x,y))
sq.blocked = True
sq.image = '#'
elif square == 't':
sq = self.get_square((x,y))
sq.blocked = True
sq.image = 't'
elif square == '.':
sq = self.get_square((x,y))
sq.image = '.'
elif square == 'o':
sq = self.get_square((x,y))
sq.blocked = True
sq.image = 'o'
x += 1
y += 1
f.close()
class Square(object):
""" map square object """
def __init__(self, xy):
self.xy = xy
self.unit = None
self.blocked = False
self.path_parent = None
self.path_g = None
self.path_h = None
self.path_f = None
self.ap_cost = 1
def __str__(self):
return str(self.xy)
def __getitem__(self, index):
return self.xy[index]
def get_rect(self):
left = self.xy[0] * data.square_size + data.camera_offset[0]
top = self.xy[1] * data.square_size + data.camera_offset[1]
width = data.square_size
height = data.square_size
return pygame.rect.Rect(left, top, width, height)
def get_distance_to_square(self, square):
x1, y1 = self.xy
x2, y2 = square.xy
dist = abs(x1 - x2) + abs(y1 - y2)
return dist
board = Board()