From 1e179598f630bfcfdb8914327185581b31535ffc Mon Sep 17 00:00:00 2001 From: Mateusz Tylka Date: Fri, 3 Apr 2020 19:36:13 +0200 Subject: [PATCH] drawTerrain in Map class, examle map draw --- src/Run.py | 2 +- src/game/Game.py | 9 +++++++-- src/game/Map.py | 36 +++++++++++++++++++++++++++++++++--- src/game/TerrainTile.py | 22 ++++++++++++++++++++-- 4 files changed, 61 insertions(+), 8 deletions(-) diff --git a/src/Run.py b/src/Run.py index 07e0abb..a3e220a 100644 --- a/src/Run.py +++ b/src/Run.py @@ -1,3 +1,3 @@ -from src.game.Game import Game +from game.Game import Game game = Game() diff --git a/src/game/Game.py b/src/game/Game.py index e1c1fea..1df88e4 100644 --- a/src/game/Game.py +++ b/src/game/Game.py @@ -1,9 +1,11 @@ import pygame import json from pathlib import Path +from os import path from game.EventManager import EventManager from game.Screen import Screen +from game.Map import Map class Game: @@ -24,8 +26,8 @@ class Game: print("Initializing pygame...", end=" ") pygame.init() + self.spritesList = pygame.sprite.Group() print("OK") - print("Initializing screen, params: " + str(self.config["window"]) + "...", end=" ") # Vertical rotation is unsupported due to UI layout @@ -38,7 +40,10 @@ class Game: self.eventManager = EventManager(self) - self.spritesList = pygame.sprite.Group() + # Start Map implement + self.mapDataFolder = path.dirname("../data/mapdata/") + self.map = Map(path.join(self.mapDataFolder, 'map.txt'), self, self.screen) + # End Map implement self.mainLoop() diff --git a/src/game/Map.py b/src/game/Map.py index 8a4b1c9..7ed7db2 100644 --- a/src/game/Map.py +++ b/src/game/Map.py @@ -1,4 +1,34 @@ +import pygame +from game.TerrainTile import TerrainTile +from game.Screen import Locations + class Map: - def __init__(self): - self.terrain - self.entites = [] + def __init__(self, filename, game, screen): + self.game = game + self.screen = screen + self.terrain = [] + self.entities = [] + with open(filename, 'rt') as f: + for line in f: + self.terrain.append(line) + + self.tileSize = self.screen.mapSize/20 + + 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() + + def terrainDraw(self): + for row, tiles in enumerate(self.terrain): + for col, tile in enumerate(tiles): + if tile == 'w': + self.screen.draw(TerrainTile(self.game, col, row, 'wall.png', self.screen.mapSize), Locations.MAP, col, row) + if tile == ',': + self.screen.draw(TerrainTile(self.game, col, row, 'floor.png', self.screen.mapSize), Locations.MAP, col, row) + if tile == '.': + self.screen.draw(TerrainTile(self.game, col, row, 'grass.png', self.screen.mapSize), Locations.MAP, col, row) + + diff --git a/src/game/TerrainTile.py b/src/game/TerrainTile.py index ec906e5..dcebdc8 100644 --- a/src/game/TerrainTile.py +++ b/src/game/TerrainTile.py @@ -1,3 +1,21 @@ -class TerrainTile: - def __init__(self): +import pygame +import os +from os import path + + +class TerrainTile(pygame.sprite.Sprite): + def __init__(self, game, x, y, texture, mapSize): self.tiles = [] + self.groups = game.spritesList + pygame.sprite.Sprite.__init__(self, self.groups) + self.game = game + self.imagesFolder = path.dirname("../data/images/") + self.terrainFolder = path.join(self.imagesFolder, 'terrain') + self.image = pygame.image.load(os.path.join(self.terrainFolder, texture)).convert() + self.image.set_colorkey((0,100,0)) + self.rect = self.image.get_rect() + self.x = x + self.y = y + self.tileSize = mapSize/20 + self.rect.x = self.x * self.tileSize + self.rect.y = self.y * self.tileSize