drawTerrain in Map class, examle map draw

This commit is contained in:
Mateusz Tylka 2020-04-03 19:36:13 +02:00
parent 46712749b6
commit 1e179598f6
4 changed files with 61 additions and 8 deletions

View File

@ -1,3 +1,3 @@
from src.game.Game import Game from game.Game import Game
game = Game() game = Game()

View File

@ -1,9 +1,11 @@
import pygame import pygame
import json import json
from pathlib import Path from pathlib import Path
from os import path
from game.EventManager import EventManager from game.EventManager import EventManager
from game.Screen import Screen from game.Screen import Screen
from game.Map import Map
class Game: class Game:
@ -24,8 +26,8 @@ class Game:
print("Initializing pygame...", end=" ") print("Initializing pygame...", end=" ")
pygame.init() pygame.init()
self.spritesList = pygame.sprite.Group()
print("OK") print("OK")
print("Initializing screen, params: " + str(self.config["window"]) + "...", end=" ") print("Initializing screen, params: " + str(self.config["window"]) + "...", end=" ")
# Vertical rotation is unsupported due to UI layout # Vertical rotation is unsupported due to UI layout
@ -38,7 +40,10 @@ class Game:
self.eventManager = EventManager(self) 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() self.mainLoop()

View File

@ -1,4 +1,34 @@
import pygame
from game.TerrainTile import TerrainTile
from game.Screen import Locations
class Map: class Map:
def __init__(self): def __init__(self, filename, game, screen):
self.terrain self.game = game
self.entites = [] 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)

View File

@ -1,3 +1,21 @@
class TerrainTile: import pygame
def __init__(self): import os
from os import path
class TerrainTile(pygame.sprite.Sprite):
def __init__(self, game, x, y, texture, mapSize):
self.tiles = [] 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