forked from dsimon96/PyWars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.py
380 lines (353 loc) · 12.9 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# map.py
# by David Simon ([email protected])
# Dec 2014
# Sprites from Advance Wars (Intelligent Systems, Nintendo)
import os
import pygame
from pygame.locals import *
from pygameBaseClass import PygameBaseClass
class Tile(pygame.sprite.Sprite):
"""
Represents a single tile
Each tile has a terrainType, an integer corresponding to a certain terrain
"""
size = 64 # size of each tile in pixels
defaultType = 1 # default type for blank maps
terrainTypeNames = {
0: 'Sea',
1: 'Plain',
2: 'Road',
3: 'Forest',
4: 'Mountain',
5: 'River',
6: 'Bridge'
}
defenseValues = {
0: 0,
1: 1,
2: 0,
3: 2,
4: 3,
5: 0,
6: 0
} # defense factor for each terrain type. Used in damage calculations
dynamicSpriteTypes = {0, 2, 5, 6} # terrain types w/ dynamic sprites
staticSpriteFiles = {
0: "WaterOpen.png",
1: "Grass.png",
2: "RoadHoriz.png",
3: "Forest.png",
4: "Mountain.png",
5: "RiverHoriz.png",
6: "BridgeHoriz.png"
} # static terrain types and their corresponding terrain types
roadFiles = {
'0000': "RoadHoriz.png",
'0001': "RoadHoriz.png",
'0010': "RoadVert.png",
'0011': "RoadCorner2.png",
'0100': "RoadHoriz.png",
'0101': "RoadHoriz.png",
'0110': "RoadCorner1.png",
'0111': "RoadT1.png",
'1000': "RoadVert.png",
'1001': "RoadCorner3.png",
'1010': "RoadVert.png",
'1011': "RoadT2.png",
'1100': "RoadCorner4.png",
'1101': "RoadT3.png",
'1110': "RoadT4.png",
'1111': "RoadInter.png"
} # Maps cardinal directions surrounding a road to the corr. sprite file
waterFiles = {
'0000': "WaterIsolated.png",
'0001': "WaterU2.png",
'0010': "WaterU1.png",
'0011': "WaterCorner2.png",
'0100': "WaterU4.png",
'0101': "WaterHoriz.png",
'0110': "WaterCorner1.png",
'0111': "WaterBorder1.png",
'1000': "WaterU3.png",
'1001': "WaterCorner3.png",
'1010': "WaterVert.png",
'1011': "WaterBorder2.png",
'1100': "WaterCorner4.png",
'1101': "WaterBorder3.png",
'1110': "WaterBorder4.png",
'1111': "WaterOpen.png"
} # Maps cardinal directions surrounding water to the corr. sprite file
riverFiles = {
'0000': "RiverHoriz.png",
'0001': "RiverHoriz.png",
'0010': "RiverVert.png",
'0011': "RiverCorner2.png",
'0100': "RiverHoriz.png",
'0101': "RiverHoriz.png",
'0110': "RiverCorner1.png",
'0111': "RiverHoriz.png",
'1000': "RiverVert.png",
'1001': "RiverCorner3.png",
'1010': "RiverVert.png",
'1011': "RiverVert.png",
'1100': "RiverCorner4.png",
'1101': "RiverHoriz.png",
'1110': "RiverVert.png",
'1111': "RiverInter.png"
} # Maps cardinal directions surrounding water to the corr. sprite file
def __init__(self, terrainType, surroundings):
super(Tile, self).__init__()
self.terrainType = terrainType
self.surroundings = surroundings
self.image = self.getImage()
self.name = self.terrainTypeNames[terrainType]
self.staticImage = self.getStaticImage()
self.height = self.image.get_height()
self.overflow = self.height - Tile.size
self.defense = Tile.defenseValues[terrainType]
def getBridgeImage(self, cardinalsIdentifier):
cardinalsIdentifier = self.getCardinalsIdentifier(5)
n = int(cardinalsIdentifier[0])
e = int(cardinalsIdentifier[1])
s = int(cardinalsIdentifier[2])
w = int(cardinalsIdentifier[3])
if (e == 1 or w == 1) and not (n == 1 or s == 1):
return 'BridgeVert.png'
else:
return 'BridgeHoriz.png'
def getCardinalsIdentifier(self, terrType):
neswIndex = [1, 4, 6, 3] # Indices for cardinal directions
cardinalsIdentifier = ''
for index in neswIndex:
tileType = self.surroundings[index]
if tileType == terrType or tileType == None:
cardinalsIdentifier += '1'
else:
cardinalsIdentifier += '0'
return cardinalsIdentifier
def getDynamicImage(self, terrType):
"""Get the appropriate filename for the terrain based on the
surroundings"""
cardinalsIdentifier = self.getCardinalsIdentifier(terrType)
if terrType == 0:
return self.waterFiles[cardinalsIdentifier]
elif terrType == 2:
return self.roadFiles[cardinalsIdentifier]
elif terrType == 5:
return self.riverFiles[cardinalsIdentifier]
elif terrType == 6:
return self.getBridgeImage(cardinalsIdentifier)
def getImage(self):
"""Gets the appropriate sprite for this tile"""
if self.terrainType in Tile.dynamicSpriteTypes:
filename = self.getDynamicImage(self.terrainType)
else:
filename = Tile.staticSpriteFiles[self.terrainType]
path = os.path.join('tiles', filename)
image = pygame.image.load(path)
return image
def getStaticImage(self):
filename = Tile.staticSpriteFiles[self.terrainType]
path = os.path.join('tiles', filename)
image = pygame.image.load(path)
return image
class Objective(Tile):
teams = {
0: 'Red',
1: 'Blue',
2: 'Green',
3: 'Yellow',
4: 'Empty'
}
types = {
0: 'HQ',
1: 'City',
2: 'Factory'
}
defenseValues = {
0: 5,
1: 4,
2: 4
}
baseHealth = 20
def __init__(self, teamAndType):
self.terrainType = 7
self.teamNum, self.typeNum = teamAndType
self.team = Objective.teams[self.teamNum]
self.type = Objective.types[self.typeNum]
self.name = self.type
self.health = Objective.baseHealth
self.image = self.getImage()
self.staticImage = self.image
self.height = self.image.get_height()
self.overflow = self.height - Tile.size
self.defense = Objective.defenseValues[self.typeNum]
def getImage(self):
filename = self.team + self.type + '.png'
path = os.path.join('tiles', filename)
image = pygame.image.load(path)
return image
class Map(pygame.sprite.Sprite):
"""
Represents an in-game map
"""
def __init__(self, contents=None):
super(Map, self).__init__()
if type(contents) == tuple:
contents = self.blankMap(contents)
else:
contents = self.loadContents(contents)
self.rows = len(contents)
self.cols = len(contents[0])
self.width = self.cols * Tile.size
self.height = self.rows * Tile.size
self.contents = contents
self.map = self.getMap(contents)
self.defense = self.getDefense()
self.image = self.getImage()
self.objectives = self.getObjectives()
def refreshImage(self):
self.image = self.getImage()
def getObjectives(self):
objectives = []
for row in xrange(self.rows):
for col in xrange(self.cols):
tile = self.map[row][col]
if isinstance(tile, Objective):
objectives.append((row, col, tile))
return objectives
@staticmethod
def blankMap(dimensions):
"""Generates a blank map"""
rows, cols = dimensions
contents = []
for row in xrange(rows):
contents += [[Tile.defaultType] * cols]
return contents
def getMap(self, contents):
"""Translates the list of contents into a map"""
map = []
for row in xrange(self.rows):
thisRow = []
for col in xrange(self.cols):
terrainType = contents[row][col]
if type(terrainType) == int:
tile = Tile(terrainType,
self.getSurroundingTiles(row, col))
else:
tile = Objective(terrainType)
thisRow += [tile]
map.append(thisRow)
return map
def deleteHQ(self, team):
for row in xrange(self.rows):
for col in xrange(self.cols):
tile = self.map[row][col]
if (isinstance(tile, Objective) and tile.typeNum == 0 and
tile.teamNum == team):
self.changeTile(Tile.defaultType, (row, col))
def changeTile(self, terrType, coords):
row, col = coords
if type(terrType) == int:
self.contents[row][col] = terrType
self.map[row][col] = Tile(terrType,
self.getSurroundingTiles(row, col))
self.updateSurroundings(coords)
else:
self.contents[row][col] = terrType
if terrType[1] == 0:
self.deleteHQ(terrType[0])
self.map[row][col] = Objective(terrType)
self.updateSurroundings(coords)
self.refreshImage()
def getSurroundingTiles(self, row, col):
"""Get a list of all of the tiles surrounding (row, col)"""
range = [-1, 0, 1]
surroundings = []
for dRow in range:
for dCol in range:
if (dRow, dCol) != (0, 0): # Ignore center tile
newRow = row + dRow
newCol = col + dCol
if (newRow < 0 or newRow >= self.rows or
newCol < 0 or newCol >= self.cols):
# Ensure that the checked tile is on the map
terrainType = None
else:
terrainType = self.contents[newRow][newCol]
surroundings.append(terrainType)
return surroundings
def getDefense(self):
"""Creates and populates a 2D list representing defense factor for
each tile in the map"""
defenseValues = []
# create an empty, appropriately sized 2D list
for row in xrange(self.rows):
defenseValues += [[None] * self.cols]
# populate the list with restrictions
for row in xrange(self.rows):
for col in xrange(self.cols):
tile = self.map[row][col]
defenseValues[row][col] = tile.defense
return defenseValues
def updateSurroundings(self, coords):
dirs = [(0, 1), (0, -1), (-1, 0), (1, 0)]
cRow, cCol = coords
for (dRow, dCol) in dirs:
newRow, newCol = cRow + dRow, cCol + dCol
if (0 <= newRow < self.rows and 0 <= newCol < self.cols):
tile = self.map[newRow][newCol]
if not isinstance(tile, Objective):
terrType = tile.terrainType
self.map[newRow][newCol] = Tile(terrType,
self.getSurroundingTiles(newRow, newCol))
def getImage(self):
"""Creates a surface with the appearance of the map"""
image = pygame.Surface((self.width, self.height))
for row in xrange(self.rows):
for col in xrange(self.cols):
tile = self.map[row][col]
top = row * Tile.size - tile.overflow
left = col * Tile.size
width = height = Tile.size
dest = (left, top, width, height)
image.blit(tile.image, dest)
return image
@staticmethod
def loadContents(contentString):
rows = contentString.splitlines()
map = []
for row in rows:
cols = row.split()
mapRow = []
for tile in cols:
if len(tile) == 2:
team = int(tile[0])
type = int(tile[1])
mapRow.append((team, type))
else:
terrType = int(tile)
mapRow.append(terrType)
map.append(mapRow)
return map
# class MapTest(PygameBaseClass):
# """A 15x10 map to debug the map and tile classes"""
# def __init__(self):
# super(MapTest, self).__init__('Map Test',15*64,10*64)
#
# def initGame(self):
# testMap = [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
# [0,0,(0,2),(0,1),1,1,1,1,0,0,0,0,0,0,0],
# [0,(0,0),2,2,3,3,3,1,1,1,1,0,0,0,0],
# [0,1,1,2,1,3,3,3,3,1,1,1,1,0,0],
# [0,1,1,2,1,1,1,3,3,3,1,1,1,0,0],
# [0,0,1,2,2,2,2,2,2,2,2,2,1,0,0],
# [0,0,1,1,1,3,3,3,1,2,0,2,1,1,0],
# [0,0,1,1,4,4,3,1,1,2,2,2,2,(1,0),0],
# [0,0,0,1,1,1,1,1,1,1,1,(1,1),(1,2),0,0],
# [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]
# a = Map((15,10), testMap)
# self.screen.blit(a.image, (0,0))
#
# if __name__ == '__main__':
# MapTest().run()