2020-04-03 19:36:13 +02:00
|
|
|
import pygame
|
|
|
|
from game.TerrainTile import TerrainTile
|
|
|
|
from game.Screen import Locations
|
|
|
|
|
2020-03-31 21:47:09 +02:00
|
|
|
class Map:
|
2020-04-03 23:09:00 +02:00
|
|
|
def __init__(self, filename, screen):
|
2020-04-03 19:36:13 +02:00
|
|
|
self.screen = screen
|
|
|
|
self.terrain = []
|
|
|
|
self.entities = []
|
2020-04-05 15:41:42 +02:00
|
|
|
self.collidableTerrain = []
|
2020-04-05 19:08:08 +02:00
|
|
|
self.collidables = pygame.sprite.Group()
|
2020-04-05 15:41:42 +02:00
|
|
|
|
2020-04-03 19:36:13 +02:00
|
|
|
with open(filename, 'rt') as f:
|
|
|
|
for line in f:
|
|
|
|
self.terrain.append(line)
|
|
|
|
|
2020-04-03 23:07:12 +02:00
|
|
|
self.tileSize = int(self.screen.mapSize/20)
|
2020-04-03 19:36:13 +02:00
|
|
|
|
|
|
|
self.tileWidth = len(self.terrain[0])
|
|
|
|
self.tileHeight = len(self.terrain)
|
|
|
|
self.width = self.tileWidth * self.tileSize
|
|
|
|
self.height = self.tileHeight * self.tileSize
|
|
|
|
|
|
|
|
self.terrainDraw()
|
2020-04-05 14:53:54 +02:00
|
|
|
|
2020-04-03 19:36:13 +02:00
|
|
|
def terrainDraw(self):
|
|
|
|
for row, tiles in enumerate(self.terrain):
|
|
|
|
for col, tile in enumerate(tiles):
|
|
|
|
if tile == 'w':
|
2020-04-05 19:08:08 +02:00
|
|
|
object = TerrainTile(col, row, 'wall.png', self.tileSize)
|
|
|
|
self.screen.draw(object, Locations.MAP, 0, 0)
|
|
|
|
self.collidables.add(object)
|
2020-04-03 23:07:12 +02:00
|
|
|
elif tile == ',':
|
2020-04-05 19:08:08 +02:00
|
|
|
self.screen.draw(TerrainTile(col, row, 'floor.png', self.tileSize), Locations.MAP, 0, 0)
|
2020-04-03 23:07:12 +02:00
|
|
|
elif tile == '.':
|
2020-04-05 19:08:08 +02:00
|
|
|
self.screen.draw(TerrainTile(col, row, 'grass.png', self.tileSize), Locations.MAP, 0, 0)
|
2020-04-05 14:53:54 +02:00
|
|
|
|
|
|
|
def addEntity(self, entity):
|
|
|
|
self.entities.append(entity)
|
|
|
|
self.screen.draw(entity, Locations.MAP, 0, 0)
|
2020-04-03 19:36:13 +02:00
|
|
|
|